forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
176 lines (149 loc) · 3.29 KB
/
list.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
package tui
import "image"
var _ Widget = &List{}
// List is a widget for displaying and selecting items.
type List struct {
WidgetBase
items []string
selected int
pos int
onItemActivated func(*List)
onSelectionChanged func(*List)
}
// NewList returns a new List with no selection.
func NewList() *List {
return &List{
selected: -1,
}
}
// Draw draws the list.
func (l *List) Draw(p *Painter) {
for i, item := range l.items {
style := "list.item"
if i == l.selected-l.pos {
style += ".selected"
}
p.WithStyle(style, func(p *Painter) {
p.FillRect(0, i, l.Size().X, 1)
p.DrawText(0, i, item)
})
}
}
// SizeHint returns the recommended size for the list.
func (l *List) SizeHint() image.Point {
var width int
for _, item := range l.items {
if w := stringWidth(item); w > width {
width = w
}
}
return image.Point{width, len(l.items)}
}
// OnKeyEvent handles terminal events.
func (l *List) OnKeyEvent(ev KeyEvent) {
if !l.IsFocused() {
return
}
switch ev.Key {
case KeyUp:
l.moveUp()
case KeyDown:
l.moveDown()
case KeyEnter:
if l.onItemActivated != nil {
l.onItemActivated(l)
}
}
switch ev.Rune {
case 'k':
l.moveUp()
case 'j':
l.moveDown()
}
}
func (l *List) moveUp() {
if l.selected > 0 {
l.selected--
if l.selected < l.pos {
l.pos--
}
}
if l.onSelectionChanged != nil {
l.onSelectionChanged(l)
}
}
func (l *List) moveDown() {
if l.selected < len(l.items)-1 {
l.selected++
if l.selected >= l.pos+len(l.items) {
l.pos++
}
}
if l.onSelectionChanged != nil {
l.onSelectionChanged(l)
}
}
// AddItems appends items to the end of the list.
func (l *List) AddItems(items ...string) {
l.items = append(l.items, items...)
}
// RemoveItems clears all the items from the list.
func (l *List) RemoveItems() {
l.items = []string{}
l.pos = 0
l.selected = -1
if l.onSelectionChanged != nil {
l.onSelectionChanged(l)
}
}
// RemoveItem removes the item at the given position.
func (l *List) RemoveItem(i int) {
// Adjust pos and selected before removing.
if l.pos >= len(l.items) {
l.pos--
}
if l.selected == i {
l.selected = -1
} else if l.selected > i {
l.selected--
}
// Copy items following i to position i.
copy(l.items[i:], l.items[i+1:])
// Shrink items by one.
l.items[len(l.items)-1] = ""
l.items = l.items[:len(l.items)-1]
if l.onSelectionChanged != nil {
l.onSelectionChanged(l)
}
}
// Length returns the number of items in the list.
func (l *List) Length() int {
return len(l.items)
}
// SetSelected sets the currently selected item.
func (l *List) SetSelected(i int) {
l.selected = i
}
// Selected returns the index of the currently selected item.
func (l *List) Selected() int {
return l.selected
}
// Select calls SetSelected and the OnSelectionChanged function.
func (l *List) Select(i int) {
l.SetSelected(i)
if l.onSelectionChanged != nil {
l.onSelectionChanged(l)
}
}
// SelectedItem returns the currently selected item.
func (l *List) SelectedItem() string {
return l.items[l.selected]
}
// OnItemActivated gets called when activated (through pressing KeyEnter).
func (l *List) OnItemActivated(fn func(*List)) {
l.onItemActivated = fn
}
// OnSelectionChanged gets called whenever a new item is selected.
func (l *List) OnSelectionChanged(fn func(*List)) {
l.onSelectionChanged = fn
}