-
Notifications
You must be signed in to change notification settings - Fork 26
/
scrollablelist.go
243 lines (212 loc) · 5.08 KB
/
scrollablelist.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package jiraui
import (
ui "gopkg.in/gizak/termui.v2"
)
// A scrollable list with a cursor. To "deactivate" the cursor, just make the
// cursor colors the same as the item colors.
type ScrollableList struct {
ui.Block
// The items in the list
Items []string
// The window's offset relative to the start of `Items`
Offset int
// The foreground color for non-cursor items
ItemFgColor ui.Attribute
// The background color for non-cursor items
ItemBgColor ui.Attribute
// The foreground color for the cursor
CursorFgColor ui.Attribute
// The background color for the cursor
CursorBgColor ui.Attribute
// The position of the cursor relative to the start of `Items`
Cursor int
}
// NewScrollableList returns a new *ScrollableList with current theme.
func NewScrollableList() *ScrollableList {
l := &ScrollableList{Block: *ui.NewBlock()}
l.CursorBgColor = ui.ColorBlue
l.CursorFgColor = ui.ColorWhite
return l
}
// Add an element to the list
func (sl *ScrollableList) Add(s string) {
sl.Items = append(sl.Items, s)
sl.render()
}
func (sl *ScrollableList) render() {
ui.Render(sl)
}
func (sl *ScrollableList) colorsForItem(i int) (fg, bg ui.Attribute) {
if i == sl.Cursor {
return sl.CursorFgColor, sl.CursorBgColor
}
return sl.ItemFgColor, sl.ItemBgColor
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
// Implements the termui.Bufferer interface
func (sl *ScrollableList) Buffer() ui.Buffer {
buf := sl.Block.Buffer()
start := min(sl.Offset, len(sl.Items))
end := min(sl.Offset+sl.InnerHeight(), len(sl.Items))
for i, item := range sl.Items[start:end] {
fg, bg := sl.colorsForItem(start + i)
if item == "" {
item = " "
}
cells := ui.DefaultTxBuilder.Build(item, fg, bg)
cells = ui.DTrimTxCls(cells, sl.InnerWidth())
offsetX := 0
for _, cell := range cells {
width := cell.Width()
buf.Set(
sl.InnerBounds().Min.X+offsetX,
sl.InnerBounds().Min.Y+i,
cell,
)
offsetX += width
}
}
return buf
}
// Move the window up one row
func (sl *ScrollableList) ScrollUp() {
if sl.Offset > 0 {
sl.Offset -= 1
if sl.Cursor >= sl.Offset+sl.InnerHeight() {
sl.Cursor = sl.Offset + sl.InnerHeight() - 1
}
sl.render()
}
}
// Move the window down one row
func (sl *ScrollableList) ScrollDown() {
if sl.Offset < len(sl.Items) {
sl.Offset += 1
if sl.Offset > sl.Cursor {
sl.Cursor = sl.Offset
}
sl.render()
}
}
// Swap current row with previous row, then move
// cursor to previous row
func (sl *ScrollableList) MoveUp(n int) {
if sl.Cursor >= n {
cur := sl.Items[sl.Cursor]
up := sl.Items[sl.Cursor-n]
sl.Items[sl.Cursor] = up
sl.Items[sl.Cursor-n] = cur
}
sl.CursorUpLines(n)
}
// Swap current row with next row, then move
// cursor to next row
func (sl *ScrollableList) MoveDown(n int) {
if sl.Cursor < len(sl.Items)-n {
cur := sl.Items[sl.Cursor]
down := sl.Items[sl.Cursor+n]
sl.Items[sl.Cursor] = down
sl.Items[sl.Cursor+n] = cur
}
sl.CursorDownLines(n)
}
// Move the cursor down one row; moving the cursor out of the window will cause
// scrolling.
func (sl *ScrollableList) CursorDown() {
sl.CursorDownLines(1)
}
func (sl *ScrollableList) CursorDownLines(n int) {
sl.SilentCursorDownLines(n)
sl.render()
}
func (sl *ScrollableList) SilentCursorDownLines(n int) {
if sl.Cursor < len(sl.Items)-n {
sl.Cursor += n
} else {
sl.Cursor = len(sl.Items) - 1
}
if sl.Cursor > sl.Offset+sl.InnerHeight()-n {
sl.Offset += n
}
}
// Move the cursor up one row; moving the cursor out of the window will cause
// scrolling.
func (sl *ScrollableList) CursorUp() {
sl.CursorUpLines(1)
}
func (sl *ScrollableList) CursorUpLines(n int) {
sl.SilentCursorUpLines(n)
sl.render()
}
func (sl *ScrollableList) SilentCursorUpLines(n int) {
if sl.Cursor > n {
sl.Cursor -= n
} else {
sl.Cursor = 0
}
if sl.Cursor < sl.Offset {
sl.Offset = sl.Cursor
}
}
func (sl *ScrollableList) SetCursorLine(n int) {
if n > len(sl.Items) || n < 0 {
return
}
if !(n >= sl.Offset && n < min(sl.Offset+sl.InnerHeight(), len(sl.Items))) {
// not on same page
if n < sl.Cursor {
// scrolling up to new line
if sl.Offset > n {
sl.Offset = n
}
} else {
// scrolling down to new line
if sl.Offset < n {
sl.Offset = n
}
}
}
sl.Cursor = n
sl.render()
}
// Move the window down one frame; this will move the cursor as well.
func (sl *ScrollableList) PageDown() {
if sl.Offset < len(sl.Items)-sl.InnerHeight() {
sl.Offset += sl.InnerHeight()
if sl.Offset > sl.Cursor {
sl.Cursor = sl.Offset
}
sl.render()
}
}
// Move the window up one frame; this will move the cursor as well.
func (sl *ScrollableList) PageUp() {
sl.Offset = max(0, sl.Offset-sl.InnerHeight())
if sl.Cursor >= sl.Offset+sl.InnerHeight() {
sl.Cursor = sl.Offset + sl.InnerHeight() - 1
}
sl.render()
}
// Scroll to the bottom of the list
func (sl *ScrollableList) ScrollToBottom() {
if len(sl.Items) >= sl.InnerHeight() {
sl.Offset = len(sl.Items) - sl.InnerHeight()
sl.render()
}
}
// Scroll to the top of the list
func (sl *ScrollableList) ScrollToTop() {
sl.Offset = 0
sl.render()
}