-
Notifications
You must be signed in to change notification settings - Fork 786
/
tree.go
127 lines (113 loc) · 1.97 KB
/
tree.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
/// +build ignore
package main
import (
"log"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
type nodeValue string
func (nv nodeValue) String() string {
return string(nv)
}
func main() {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
nodes := []*widgets.TreeNode{
{
Value: nodeValue("Key 1"),
Nodes: []*widgets.TreeNode{
{
Value: nodeValue("Key 1.1"),
Nodes: []*widgets.TreeNode{
{
Value: nodeValue("Key 1.1.1"),
Nodes: nil,
},
{
Value: nodeValue("Key 1.1.2"),
Nodes: nil,
},
},
},
{
Value: nodeValue("Key 1.2"),
Nodes: nil,
},
},
},
{
Value: nodeValue("Key 2"),
Nodes: []*widgets.TreeNode{
{
Value: nodeValue("Key 2.1"),
Nodes: nil,
},
{
Value: nodeValue("Key 2.2"),
Nodes: nil,
},
{
Value: nodeValue("Key 2.3"),
Nodes: nil,
},
},
},
{
Value: nodeValue("Key 3"),
Nodes: nil,
},
}
l := widgets.NewTree()
l.TextStyle = ui.NewStyle(ui.ColorYellow)
l.WrapText = false
l.SetNodes(nodes)
x, y := ui.TerminalDimensions()
l.SetRect(0, 0, x, y)
ui.Render(l)
previousKey := ""
uiEvents := ui.PollEvents()
for {
e := <-uiEvents
switch e.ID {
case "q", "<C-c>":
return
case "j", "<Down>":
l.ScrollDown()
case "k", "<Up>":
l.ScrollUp()
case "<C-d>":
l.ScrollHalfPageDown()
case "<C-u>":
l.ScrollHalfPageUp()
case "<C-f>":
l.ScrollPageDown()
case "<C-b>":
l.ScrollPageUp()
case "g":
if previousKey == "g" {
l.ScrollTop()
}
case "<Home>":
l.ScrollTop()
case "<Enter>":
l.ToggleExpand()
case "G", "<End>":
l.ScrollBottom()
case "E":
l.ExpandAll()
case "C":
l.CollapseAll()
case "<Resize>":
x, y := ui.TerminalDimensions()
l.SetRect(0, 0, x, y)
}
if previousKey == "g" {
previousKey = ""
} else {
previousKey = e.ID
}
ui.Render(l)
}
}