-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui.go
More file actions
163 lines (143 loc) · 3.88 KB
/
tui.go
File metadata and controls
163 lines (143 loc) · 3.88 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"math"
"github.com/Kerrigan29a/drawille-go"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
type TUI struct {
app *tview.Application
}
func NewTUI(reader *Reader) *TUI {
/* Config widgets */
app := tview.NewApplication()
logTV := tview.NewTextView().SetDynamicColors(true)
box := tview.NewBox().SetBorder(true).SetTitle("[ " + reader.path + " ]")
flex := tview.NewFlex().SetDirection(tview.FlexRow)
flex.AddItem(box, 0, 3, true)
flex.AddItem(logTV, 0, 1, false)
app.SetRoot(flex, true)
t := &TUI{app: app}
/* Set function to draw canvas */
cc := NewCanvasController(reader, box, logTV)
box.SetDrawFunc(cc.DrawFunc)
box.SetInputCapture(cc.InputCapture)
return t
}
func (t *TUI) Run() error {
if err := t.app.Run(); err != nil {
return err
}
return nil
}
type CanvasController struct {
reader *Reader
box *tview.Box
logTV *tview.TextView
canvas drawille.Canvas
amount int
}
func NewCanvasController(reader *Reader, box *tview.Box, logTV *tview.TextView) *CanvasController {
canvas := drawille.NewCanvas()
canvas.Inverse = true
return &CanvasController{reader: reader, box: box, logTV: logTV, canvas: canvas, amount: -1}
}
func (c *CanvasController) Values(amount int) ([]float64, error) {
prevAmount := c.reader.FrameCap()
if prevAmount != amount {
c.reader.SetFrameCap(amount)
difference := amount - prevAmount
if difference >= 0 {
err := c.reader.ReadNext(uint(difference))
if err != nil {
return nil, err
}
}
}
return c.reader.Values()
}
func (c *CanvasController) DrawFunc(screen tcell.Screen, x int, y int, width int, height int) (int, int, int, int) {
innerX := x + 1
innerY := y + 1
innerWidth := width - 2
innerHeight := height - 2
begin := 0
amount := innerWidth * 2
end := begin + amount
/* Read values */
values, err := c.Values(amount)
if err != nil {
panic(err)
}
logf(c.logTV, "[blue]DEBUG[-] values = %v\n", values)
if len(values) == 0 {
return innerX, innerY, innerWidth, innerHeight
}
/* Create graph */
c.canvas.Clear()
normalizedValues := normalizeFloat64(values)
intValues := floatToInt(normalizedValues[begin:end], (innerHeight-1)*4)
for i := begin; i < end; i++ {
gX := i
gY := intValues[i]
//logf(c.logTV, "[blue]DEBUG[-] %d -> %g -> %g -> %d\n", gX, values[i], normalizedValues[i], gY)
for j := 0; j < gY; j++ {
c.canvas.Set(gX, j)
}
c.canvas.Set(gX, gY)
}
gWidth := c.canvas.MaxX() - c.canvas.MinX()
gHeight := c.canvas.MaxY() - c.canvas.MinY()
logf(c.logTV, "[blue]DEBUG[-] gWidth = %v/2 = %v\n", gWidth, gWidth/2)
logf(c.logTV, "[blue]DEBUG[-] gHeight = %v/4 = %v\n", gHeight, gHeight/4)
currentY := innerY
for _, row := range c.canvas.Rows(c.canvas.MinX(), c.canvas.MinY(), c.canvas.MaxX(), c.canvas.MaxY()) {
tview.Print(screen, row, innerX, currentY, innerWidth, tview.AlignLeft, tcell.ColorGreen)
currentY++
}
return innerX, innerY, innerWidth, innerHeight
}
func (c *CanvasController) InputCapture(event *tcell.EventKey) *tcell.EventKey {
minAmount := uint(math.Round(0.1 * float64(c.reader.FrameCap())))
maxAmount := uint(math.Round(0.5 * float64(c.reader.FrameCap())))
switch event.Key() {
case tcell.KeyRight:
amount := minAmount
if event.Modifiers()&tcell.ModShift != 0 {
amount = maxAmount
}
c.reader.ReadNext(amount)
return nil
case tcell.KeyLeft:
amount := minAmount
if event.Modifiers()&tcell.ModShift != 0 {
amount = maxAmount
}
c.reader.ReadPrev(amount)
return nil
case tcell.KeyRune:
switch event.Rune() {
case 'h':
c.reader.ReadPrev(minAmount)
return nil
case 'H':
c.reader.ReadPrev(maxAmount)
return nil
case 'l':
c.reader.ReadNext(minAmount)
return nil
case 'L':
c.reader.ReadNext(maxAmount)
return nil
default:
return event
}
default:
return event
}
}
func logf(logTV *tview.TextView, format string, v ...interface{}) {
txt := fmt.Sprintf(format, v...)
logTV.Write([]byte(txt))
}