-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
watch.go
102 lines (90 loc) · 2.01 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var version = "1.0.0"
func main() {
// Command to watch can come from stdin or arguments.
var command string
if len(os.Args) <= 1 {
fmt.Fprintf(os.Stdout, "Welcome to watch %v.\nType command to watch.\n> ", version)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
command = scanner.Text()
break
}
} else {
command = strings.Join(os.Args[1:], " ")
}
startTime := time.Now()
sleep := 1 * time.Second
shell := defaultShell
if s, ok := os.LookupEnv("WATCH_COMMAND"); ok {
shell = s
}
sh := strings.Split(shell, " ")
sh = append(sh, command)
screen, err := tcell.NewScreen()
if err != nil {
panic(err)
}
err = screen.Init()
if err != nil {
panic(err)
}
app := tview.NewApplication()
app.SetScreen(screen)
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() {
screen.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)
}
}