forked from antonmedv/watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
84 lines (72 loc) · 1.63 KB
/
watch.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
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
func main() {
if len(os.Args) <= 1 {
fmt.Fprintln(os.Stderr, "usage: watch [command]")
os.Exit(2)
}
startTime := time.Now()
sleep := 1 * time.Second
command := strings.Join(os.Args[1:], " ")
shell := defaultShell
if s, ok := os.LookupEnv("WATCH_COMMAND"); ok {
shell = s
}
sh := strings.Split(shell, " ")
sh = append(sh, command)
app := tview.NewApplication()
viewer := tview.NewTextView().
SetDynamicColors(true).
SetScrollable(true).
SetTextColor(tcell.ColorDefault)
viewer.
SetBackgroundColor(tcell.ColorDefault)
elapsed := tview.NewTextView().
SetTextColor(tcell.ColorBlack).
SetTextAlign(tview.AlignRight).
SetText("0s")
elapsed.
SetBackgroundColor(tcell.ColorLightCyan)
title := tview.NewTextView().
SetTextColor(tcell.ColorBlack).
SetText(command)
title.
SetBackgroundColor(tcell.ColorLightCyan)
statusBar := tview.NewFlex().
AddItem(title, 0, 1, false).
AddItem(elapsed, 7, 1, false)
flex := tview.NewFlex().
SetDirection(tview.FlexRow)
flex.AddItem(viewer, 0, 1, true)
flex.AddItem(statusBar, 1, 1, false)
app.SetRoot(flex, true)
go func() {
for {
cmd := exec.Command(sh[0], sh[1:]...)
var buf bytes.Buffer
err := cmdOutput(cmd, &buf)
if err != nil {
panic(err)
}
app.QueueUpdateDraw(func() {
viewer.Clear()
viewer.SetText(tview.TranslateANSI(buf.String()))
elapsed.SetText(fmt.Sprintf("%v", time.Since(startTime).Round(time.Second)))
})
time.Sleep(sleep)
}
}()
err := app.Run()
if err != nil {
panic(err)
}
}