-
Notifications
You must be signed in to change notification settings - Fork 0
/
tui.go
149 lines (118 loc) · 2.94 KB
/
tui.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"log"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type TUI struct {
app *tview.Application
grid *tview.Grid
primitives []tview.Primitive
tview *tview.TextView
}
func newTextView(text string, maxlines int) tview.Primitive {
return tview.NewTextView().
SetTextAlign(tview.AlignLeft).
SetText(text).
SetWrap(false).SetMaxLines(maxlines)
}
func InitTUI(cfg []CmdConfig, maxlines int) *TUI {
var tui TUI
tui.grid = tview.NewGrid().
SetBorders(true).
SetRows(1).
SetColumns(16)
// Set up cmd textviews
col := 1
for _, c := range cfg {
// Add header
tui.grid.AddItem(newTextView(c.Cmd, maxlines), 0, col, 1, 1, 0, 0, false)
// Add cmd output
p := newTextView("", maxlines)
tui.primitives = append(tui.primitives, p)
tui.grid.AddItem(p, 1, col, 1, 1, 0, 0, false)
col++
}
// Add timestamp + header in first column
timeview := newTextView("", maxlines)
tui.grid.AddItem(newTextView("Time", maxlines), 0, 0, 1, 1, 0, 0, false)
tui.grid.AddItem(timeview, 1, 0, 1, 1, 0, 0, false)
tView, ok := timeview.(*tview.TextView)
if !ok {
log.Panic("Error converting timeview")
}
tui.tview = tView
// Register input handlers to sync scrolling between textviews
allPrimitives := append(tui.primitives, tui.tview)
for _, tv := range allPrimitives {
tv := tv.(*tview.TextView)
inputwrapper := func(event *tcell.EventKey) *tcell.EventKey {
row, _ := tv.GetScrollOffset()
for _, tv2 := range allPrimitives {
tv2 := tv2.(*tview.TextView)
if event.Key() == tcell.KeyEnter {
tv2.ScrollToEnd()
} else if tv != tv2 {
_, col := tv2.GetScrollOffset()
tv2.ScrollTo(row, col)
}
}
return event
}
tv.SetInputCapture(inputwrapper)
mousewrapper := func(action tview.MouseAction, event *tcell.EventMouse) (tview.MouseAction, *tcell.EventMouse) {
row, _ := tv.GetScrollOffset()
switch action {
case tview.MouseScrollDown, tview.MouseScrollUp:
for _, tv2 := range allPrimitives {
tv2 := tv2.(*tview.TextView)
_, col := tv2.GetScrollOffset()
tv2.ScrollTo(row, col)
}
}
return action, event
}
tv.SetMouseCapture(mousewrapper)
}
tui.app = tview.NewApplication().SetRoot(tui.grid, true).EnableMouse(true)
return &tui
}
func (t TUI) AddData(data LogEntry) {
if t.app == nil {
return
}
newlines := ""
for i := 0; i < strings.Count(data.Text, "\n"); i++ {
newlines += "\n"
}
for c, p := range t.primitives {
tv, ok := p.(*tview.TextView)
if !ok {
log.Panic("Can't convert TV")
}
if (c + 1) == data.Col {
tv.Write([]byte(data.Text))
} else {
tv.Write([]byte(newlines))
}
}
t.tview.Write([]byte(data.Ts.Format(time.Stamp)))
t.tview.Write([]byte(newlines))
}
func (t TUI) Update() {
if t.app == nil {
return
}
t.app.Draw()
}
func (t TUI) Run() {
if t.app == nil {
return
}
// Fire up the tview event loop
if err := t.app.Run(); err != nil {
log.Panic(err)
}
}