-
Notifications
You must be signed in to change notification settings - Fork 6
/
list.go
298 lines (259 loc) · 6.6 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) 2016 Company 0, LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ttk
import (
"fmt"
"strings"
"unicode/utf8"
"github.com/nsf/termbox-go"
)
// WidgetList uniquely identifies the list widget.
const (
WidgetList = "list"
)
var (
_ Widgeter = (*List)(nil) // ensure interface is satisfied
)
// init registers the List Widget.
func init() {
registeredWidgets[WidgetList] = NewList
}
// List is a text only widget. It prints the contents of text onto the
// window.
type List struct {
Widget
width int
height int
trueW int
trueH int
trueX int
trueY int
at int // top line being displayed
paging bool // paging in progress?
attr Attributes
content []string
visibility Visibility
}
func (l *List) Visibility(op Visibility) Visibility {
switch op {
case VisibilityGet:
return l.visibility
case VisibilityShow:
l.visibility = op
l.Render()
case VisibilityHide:
l.visibility = op
l.clear()
}
return l.visibility
}
func (l *List) clear() {
s := strings.Repeat(" ", l.trueW)
x := 0
for i := 0; i < l.trueH; i++ {
l.w.printf(x, i+l.trueY, defaultAttributes(), s)
}
}
// Render implements the Render interface. This is called from queue context
// so be careful to not use blocking calls.
func (l *List) Render() {
if len(l.content) == 0 || l.visibility == VisibilityHide {
return
}
l.Display(Current)
}
// KeyHandler implements the interface. This is called from queue context
// so be careful to not use blocking calls.
func (l *List) KeyHandler(ev termbox.Event) bool {
return false // not handled
}
// CanFocus implements the interface. This is called from queue context
// so be careful to not use blocking calls.
func (l *List) CanFocus() bool {
return false // can not be focused
}
// Focus implements the interface. This is called from queue context
// so be careful to not use blocking calls.
func (l *List) Focus() {
// do nothing
}
// Len returns the number of lines in this List widget.
func (l *List) Len() int {
return len(l.content)
}
// Len returns the width of the List widget.
func (l *List) Width() int {
return l.trueW
}
// NewList is the List initializer. This call implements the NewWidget
// convention by taking a *Window and and an anchor point to render the widget.
func NewList(w *Window, x, y int) (Widgeter, error) {
return &List{
Widget: MakeWidget(w, x, y),
}, nil
}
// SetAttributes sets the Attributes. This will not be displayed immediately.
// SetAttributes shall be called from queue context.
func (l *List) SetAttributes(a Attributes) {
l.attr = a
}
func (l *List) Resize() {
l.trueX = l.x
l.trueY = l.y
l.trueW = l.width
l.trueH = l.height
// width < 1 means x - width
if l.width < 1 {
l.trueW = l.w.x + l.width
}
// height < 1 means y - height
if l.height < 1 {
l.trueH = l.w.y + l.height - 1
}
// check if we need to update l.at
if l.at != 0 && l.at+l.trueH >= len(l.content) {
l.at = len(l.content) - l.trueH
if l.at < 0 {
l.at = 0
}
}
}
// AddList is a convenience function to add a new list to a window. It wraps
// the AddWidget call. AddList must be called from queue.
func (w *Window) AddList(x, y, width, height int) *List {
// we can ignore error for builtins
l, _ := w.AddWidget(WidgetList, x, y)
list := l.(*List)
list.width = width
list.height = height
list.Resize()
list.SetAttributes(defaultAttributes())
list.content = make([]string, 0, 1000)
return list
}
// Append adds a line of text to the list. Append must be called from queue.
func (l *List) Append(format string, args ...interface{}) {
s := fmt.Sprintf(format, args...)
l.content = append(l.content, s)
// adjust at if we are not in a paging operation
if l.paging {
return
}
l.at = len(l.content) - l.trueH
if l.at < 0 {
l.at = 0
}
}
// Location are hints for the Display function.
type Location int
const (
Top Location = iota // Display top line of list
Bottom // Display bottom line of list
Up // Render page up on list
Down // Render page down on list
Current // Render current location on list
)
// Display renders the widget. This is called from queue context so be careful
// to not use blocking calls.
func (l *List) Display(where Location) {
if len(l.content) == 0 || l.visibility == VisibilityHide {
return
}
c := l.content
// XXX this isn't 100% correct since it doesn't handle wrapping lines.
switch where {
case Current:
case Top:
if len(c) > l.trueH {
l.at = 0
l.paging = true
}
case Bottom:
l.at = len(c) - l.trueH
if l.at < 0 {
l.at = 0
}
l.paging = false
case Up:
l.at = l.at - l.trueH + 1
if l.at < 0 {
l.at = 0
}
l.paging = true
case Down:
y := l.at + l.trueH - 1
if y+l.trueH > len(c) {
l.Display(Bottom)
return
}
l.at = y
l.paging = true
default:
return
}
c = c[l.at : l.at+l.trueH]
// create a buffer with all lines neatly clipped
buffer := make([][]rune, 0, l.trueH*2)
for _, s := range c {
printWidth := 0
start := 0
var lastColor, leftover string
var cc string // color continuation on next line
for i := 0; i < len(s); {
r, width := utf8.DecodeRuneInString(s[i:])
if r == '\x1b' {
_, skip, err := DecodeColor(s[i:])
if err == nil {
lastColor = s[i : i+skip]
i += skip
leftover = s[start:i]
continue
}
}
i += width
printWidth++
if printWidth > l.trueW-1 {
// clip, so reset start and printWidth
buffer = append(buffer,
[]rune(lastColor+s[start:i]))
start = i
printWidth = 0
cc = lastColor
if start == len(s) {
// we ended exactly with a color on
// term boundary, clear out leftover
// that was set in lastColor check
leftover = ""
break
}
continue
} else if i < len(s) {
// we do this unnecessary song and dance to only
// assign leftover once
continue
}
leftover = s[start:i]
break // will always break but do it anyway for clarity
}
if leftover != "" {
// done clipping, next line
filler := strings.Repeat(" ", l.trueW-printWidth)
buffer = append(buffer, []rune(cc+leftover+filler))
}
}
// now clip buffer to widget l.trueH; we only want to show bottom
// l.trueH lines
if len(buffer) > l.trueH {
buffer = buffer[len(buffer)-l.trueH:]
}
x := 0
for i, v := range buffer {
l.w.printf(x, l.trueY+i, l.attr, "%v", string(v))
}
}
// IsPaging indicates if the widget is displaying bottom line. If the bottom
// line is being displayed it means that the appended text is rendered.
func (l *List) IsPaging() bool {
return l.paging
}