-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (106 loc) · 2.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"log"
"os/exec"
"time"
"github.com/kballard/go-shellquote"
"github.com/rivo/tview"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
verbose = kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool()
path = kingpin.Arg("path", "Path to file.").Required().String()
)
const DEFAULT_TEXT = "Enter an awk expression to get a preview of the results."
/* Oh dear, global variables.. */
var previewer = tview.NewTextView().SetText(DEFAULT_TEXT)
var app = tview.NewApplication()
var setPreview = func(text string) {
app.QueueUpdateDraw(func() {
previewer.SetText(text)
})
}
/// Run awk as a subprocess and collect the text to be shown by the previewer.
func execAwk(text string) {
if len(text) == 0 {
setPreview(DEFAULT_TEXT)
}
args, err := shellquote.Split(text)
if err != nil {
setPreview(fmt.Sprintf("Shellquote Error: %v", err.Error()))
return
}
log.Printf("Trying awk %v %v", text, *path)
args = append(args, *path)
output, err := exec.Command("awk", args...).Output()
if err != nil {
exitErr, isExitErr := err.(*exec.ExitError)
if isExitErr {
setPreview(string(exitErr.Stderr))
} else {
setPreview(err.Error())
}
} else {
setPreview(string(output))
}
}
var dots = []string{".", "..", "..."}
/* Some simple feedback so that you don't have to wonder
* if your program just froze.
*/
func start_dots(ticker *time.Ticker) {
i := 0
for range ticker.C {
app.QueueUpdateDraw(func() {
previewer.SetText(dots[i])
i++
i %= 3
})
}
}
/*
* Debounces the callback for sending the command text
* to awk so we don't potentially overwhelm the system
*
* It's a bit hacky.
*/
func debounced(callback func(text string)) func(text string) {
var handle *time.Timer
ticker := time.NewTicker(time.Millisecond * 200)
ticker.Stop()
go start_dots(ticker)
return func(text string) {
if handle != nil {
handle.Stop()
ticker.Reset(time.Millisecond * 200)
}
handle = time.AfterFunc(time.Millisecond*500, func() {
callback(text)
ticker.Stop()
handle = nil
})
}
}
/*
* Sets up the inputline, preview, and a debug console, and runs the UI.
*/
func main() {
kingpin.Parse()
logOutput := tview.NewTextView()
logOutput.Box.SetBorder(true).SetTitle("DEBUG LOG")
log.SetOutput(logOutput)
readline := tview.NewInputField()
readline.SetLabel("awk: ")
runAwk := debounced(execAwk)
readline.SetChangedFunc(runAwk)
appMain := tview.NewFlex().SetDirection(tview.FlexRow)
appMain.Box.SetBorder(true).SetTitle("Awk Preview")
appMain.
AddItem(readline, 3, 1, true).
AddItem(previewer, 0, 5, false).
AddItem(logOutput, 0, 1, false)
if err := app.SetRoot(appMain, true).SetFocus(appMain).Run(); err != nil {
panic(err)
}
}