-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwidgets_controller.go
85 lines (69 loc) · 1.7 KB
/
widgets_controller.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
package ui
import (
"log"
"math"
ui "github.com/gizak/termui/v3"
)
func init() {
if err := ui.Init(); err != nil {
log.Fatalf("Failed to initialize termui: %v", err)
}
}
func WidgetsController(fileContent *string, rows ...[]UIWidget) {
defer ui.Close()
numWidgets := 0
for _, row := range rows {
numWidgets += len(row)
}
widgets := make([]UIWidget, numWidgets)
drawables := make([]ui.Drawable, numWidgets) // underlying termui drawables
i := 0
for _, row := range rows {
for _, widget := range row {
widget.InitWidget(fileContent)
drawables[i] = widget.GetWidget()
widgets[i] = widget
i++
}
}
ui.Render(drawables...)
// First widget -> default widget
activeWidget := widgets[0]
activeWidget.ToggleActive()
activeRowIndex, activeColumnIndex := 0, 0
e := activeWidget.SetState()
for {
switch e {
case "K":
activeRowIndex--
case "J":
activeRowIndex++
case "H":
activeColumnIndex--
case "L":
activeColumnIndex++
case "<C-c>", "q":
return
}
// Deactivate current active widget
activeWidget.ToggleActive()
if activeRowIndex < 0 {
absActiveRowIndex := int(math.Abs(float64(activeRowIndex)))
activeRowIndex = len(rows) - absActiveRowIndex
} else if activeRowIndex > len(rows)-1 {
activeRowIndex = 0
}
activeRow := rows[activeRowIndex]
if activeColumnIndex < 0 {
absActiveColumnIndex := int(math.Abs(float64(activeColumnIndex)))
activeColumnIndex = len(activeRow) - absActiveColumnIndex
} else if activeColumnIndex >= len(activeRow) {
activeColumnIndex = 0
}
activeColumn := activeRow[activeColumnIndex]
// Set new active widget
activeWidget = activeColumn
activeWidget.ToggleActive()
e = activeWidget.SetState()
}
}