-
Notifications
You must be signed in to change notification settings - Fork 1
/
flex.go
180 lines (149 loc) · 3.16 KB
/
flex.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package tplot
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type Direction int
const (
DirectionVertical Direction = tview.FlexRow
DirectionHorizontal Direction = tview.FlexColumn
)
type Focus int
const (
FocusLeft Focus = iota
FocusDown
FocusUp
FocusRight
)
// Flex is a wrapper around tview.Flex that allows easy keyboard navigation.
type Flex struct {
*tview.Flex
focused tview.Primitive
bindings *FlexBindings
direction Direction
}
type FlexBindings struct {
FocusLeft tcell.Key
FocusDown tcell.Key
FocusUp tcell.Key
FocusRight tcell.Key
}
var DefaultFlexBindings = &FlexBindings{
FocusLeft: tcell.KeyCtrlH,
FocusDown: tcell.KeyCtrlJ,
FocusUp: tcell.KeyCtrlK,
FocusRight: tcell.KeyCtrlL,
}
func NewFlex() *Flex {
return &Flex{
Flex: tview.NewFlex(),
bindings: DefaultFlexBindings,
}
}
func (f *Flex) SetDirection(direction Direction) {
f.direction = direction
f.Flex.SetDirection(int(direction))
}
func (f *Flex) Direction() Direction {
return f.direction
}
func (f *Flex) SetBindings(bindings *FlexBindings) {
f.bindings = bindings
}
func (f *Flex) Bindings() *FlexBindings {
return f.bindings
}
func (f *Flex) FocusedItem() (i int, count int, item tview.Primitive) {
count = f.Flex.GetItemCount()
for i = 0; i < count; i++ {
item := f.Flex.GetItem(i)
if item.HasFocus() {
return i, count, item
}
}
return -1, count, nil
}
func (f *Flex) wrapSetFocus(setFocus func(p tview.Primitive)) func(tview.Primitive) {
return func(p tview.Primitive) {
setFocus(p)
_, _, item := f.FocusedItem()
f.focused = item
}
}
func (f *Flex) move(move Focus, setFocus func(p tview.Primitive)) (consumed bool) {
setFocus = f.wrapSetFocus(setFocus)
i, count, item := f.FocusedItem()
if item == nil {
return false
}
if block, ok := item.(*Tile); ok {
if consumed := block.move(move, setFocus); consumed {
return true
}
}
dec := func() bool {
i--
if i < 0 {
return false
}
setFocus(f.Flex.GetItem(i))
return true
}
inc := func() bool {
i++
if i >= count {
return false
}
setFocus(f.Flex.GetItem(i))
return true
}
if f.direction == DirectionHorizontal {
switch move {
case FocusLeft:
return dec()
case FocusRight:
return inc()
default:
return false
}
} else {
switch move {
case FocusUp:
return dec()
case FocusDown:
return inc()
default:
return false
}
}
}
func (b *Flex) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
return b.Flex.WrapInputHandler(
func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
switch event.Key() {
case b.bindings.FocusLeft:
b.move(FocusLeft, setFocus)
case b.bindings.FocusDown:
b.move(FocusDown, setFocus)
case b.bindings.FocusUp:
b.move(FocusUp, setFocus)
case b.bindings.FocusRight:
b.move(FocusRight, setFocus)
default:
handler := b.Flex.InputHandler()
handler(event, setFocus)
}
},
)
}
func (b *Flex) Focus(setFocus func(p tview.Primitive)) {
count := b.Flex.GetItemCount()
for i := 0; i < count; i++ {
item := b.Flex.GetItem(i)
if item == b.focused {
setFocus(item)
return
}
}
b.Flex.Focus(setFocus)
}