-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
tree.go
124 lines (102 loc) · 2.66 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
package ui
import (
"context"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/tview"
"github.com/gdamore/tcell/v2"
)
// KeyListenerFunc listens to key presses.
type KeyListenerFunc func()
// Tree represents a tree view.
type Tree struct {
*tview.TreeView
actions KeyActions
selectedItem string
cmdBuff *model.FishBuff
expandNodes bool
Count int
keyListener KeyListenerFunc
}
// NewTree returns a new view.
func NewTree() *Tree {
return &Tree{
TreeView: tview.NewTreeView(),
expandNodes: true,
actions: make(KeyActions),
cmdBuff: model.NewFishBuff('/', model.FilterBuffer),
}
}
// Init initializes the view.
func (t *Tree) Init(ctx context.Context) error {
t.BindKeys()
t.SetBorder(true)
t.SetBorderAttributes(tcell.AttrBold)
t.SetBorderPadding(0, 0, 1, 1)
t.SetGraphics(true)
t.SetGraphicsColor(tcell.ColorCadetBlue)
t.SetInputCapture(t.keyboard)
return nil
}
// SetSelectedItem sets the currently selected node.
func (t *Tree) SetSelectedItem(s string) {
t.selectedItem = s
}
// GetSelectedItem returns the currently selected item or blank if none.
func (t *Tree) GetSelectedItem() string {
return t.selectedItem
}
// ExpandNodes returns true if nodes are expanded or false otherwise.
func (t *Tree) ExpandNodes() bool {
return t.expandNodes
}
// CmdBuff returns the filter command.
func (t *Tree) CmdBuff() *model.FishBuff {
return t.cmdBuff
}
// SetKeyListenerFn sets a key entered listener.
func (t *Tree) SetKeyListenerFn(f KeyListenerFunc) {
t.keyListener = f
}
// Actions returns active menu bindings.
func (t *Tree) Actions() KeyActions {
return t.actions
}
// Hints returns the view hints.
func (t *Tree) Hints() model.MenuHints {
return t.actions.Hints()
}
// ExtraHints returns additional hints.
func (t *Tree) ExtraHints() map[string]string {
return nil
}
// BindKeys binds default mnemonics.
func (t *Tree) BindKeys() {
t.Actions().Add(KeyActions{
KeySpace: NewKeyAction("Expand/Collapse", t.noopCmd, true),
KeyX: NewKeyAction("Expand/Collapse All", t.toggleCollapseCmd, true),
})
}
func (t *Tree) keyboard(evt *tcell.EventKey) *tcell.EventKey {
if a, ok := t.actions[AsKey(evt)]; ok {
return a.Action(evt)
}
return evt
}
func (t *Tree) noopCmd(evt *tcell.EventKey) *tcell.EventKey {
return evt
}
func (t *Tree) toggleCollapseCmd(evt *tcell.EventKey) *tcell.EventKey {
t.expandNodes = !t.expandNodes
t.GetRoot().Walk(func(node, parent *tview.TreeNode) bool {
if parent != nil {
node.SetExpanded(t.expandNodes)
}
return true
})
return nil
}
// ClearSelection clears the currently selected node.
func (t *Tree) ClearSelection() {
t.selectedItem = ""
t.SetCurrentNode(nil)
}