-
Notifications
You must be signed in to change notification settings - Fork 50
/
textdisplay.go
278 lines (230 loc) · 5.41 KB
/
textdisplay.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
package clui
import (
xs "github.com/huandu/xstrings"
term "github.com/nsf/termbox-go"
)
type TextDisplay struct {
BaseControl
colorized bool
topLine int
lineCount int
onDrawLine func(int) string
onPositionChanged func(int, int)
}
// TextReader is deprecated due to its confusing name. Use TextDisplay instead.
// In next major library version TextReader will be removed
type TextReader = TextDisplay
func CreateTextReader(parent Control, width, height int, scale int) *TextDisplay {
return CreateTextDisplay(parent, width, height, scale)
}
func CreateTextDisplay(parent Control, width, height int, scale int) *TextDisplay {
l := new(TextDisplay)
l.BaseControl = NewBaseControl()
if height == AutoSize {
height = 10
}
if width == AutoSize {
width = 20
}
l.SetSize(width, height)
l.SetConstraints(width, height)
l.parent = parent
l.SetScale(scale)
if parent != nil {
parent.AddChild(l)
}
l.onDrawLine = nil
l.onPositionChanged = nil
return l
}
func (l *TextDisplay) drawText() {
if l.onDrawLine == nil {
return
}
PushAttributes()
defer PopAttributes()
bg, fg := RealColor(l.bg, l.Style(), ColorEditBack), RealColor(l.fg, l.Style(), ColorEditText)
if l.Active() {
bg, fg = RealColor(l.bg, l.Style(), ColorEditActiveBack), RealColor(l.fg, l.Style(), ColorEditActiveText)
}
SetTextColor(fg)
SetBackColor(bg)
ind := 0
for ind < l.height {
var str string
if ind+l.topLine < l.lineCount {
str = l.onDrawLine(ind + l.topLine)
} else {
if ind+l.topLine == l.lineCount+5 {
str = xs.Center("--- THE END ---", l.width, " ")
} else {
str = ""
}
}
if str != "" {
str = SliceColorized(str, 0, l.width)
DrawText(l.x, l.y+ind, str)
}
ind++
}
}
// Draw repaints the control on its View surface
func (l *TextDisplay) Draw() {
if l.hidden {
return
}
PushAttributes()
defer PopAttributes()
x, y := l.Pos()
w, h := l.Size()
bg, fg := RealColor(l.bg, l.Style(), ColorEditBack), RealColor(l.fg, l.Style(), ColorEditText)
if l.Active() {
bg, fg = RealColor(l.bg, l.Style(), ColorEditActiveBack), RealColor(l.fg, l.Style(), ColorEditActiveText)
}
SetTextColor(fg)
SetBackColor(bg)
FillRect(x, y, w, h, ' ')
l.drawText()
}
func (l *TextDisplay) home() {
if l.topLine != 0 {
l.topLine = 0
if l.onPositionChanged != nil {
l.onPositionChanged(l.topLine, l.lineCount)
}
}
}
func (l *TextDisplay) end() {
if l.lineCount > 0 && l.topLine != l.lineCount-1 {
l.topLine = l.lineCount - 1
if l.onPositionChanged != nil {
l.onPositionChanged(l.topLine, l.lineCount)
}
}
}
func (l *TextDisplay) moveUp(count int) {
if l.topLine != 0 {
l.topLine -= count
if l.topLine < 0 {
l.topLine = 0
}
if l.onPositionChanged != nil {
l.onPositionChanged(l.topLine, l.lineCount)
}
}
}
func (l *TextDisplay) moveDown(count int) {
if l.lineCount > 0 && l.topLine != l.lineCount-1 {
l.topLine += count
if l.topLine > l.lineCount-1 {
l.topLine = l.lineCount - 1
}
if l.onPositionChanged != nil {
l.onPositionChanged(l.topLine, l.lineCount)
}
}
}
func (l *TextDisplay) processMouseClick(ev Event) bool {
if ev.Key != term.MouseLeft {
return false
}
dy := ev.Y - l.y
ww := l.height
if dy < l.height/2 {
l.moveUp(ww - 1)
} else {
l.moveDown(ww - 1)
}
return true
}
/*
ProcessEvent processes all events come from the control parent. If a control
processes an event it should return true. If the method returns false it means
that the control do not want or cannot process the event and the caller sends
the event to the control parent
*/
func (l *TextDisplay) ProcessEvent(event Event) bool {
if !l.Active() || !l.Enabled() {
return false
}
switch event.Type {
case EventKey:
switch event.Key {
case term.KeyHome:
l.home()
return true
case term.KeyEnd:
l.end()
return true
case term.KeyArrowUp:
l.moveUp(1)
return true
case term.KeyArrowDown:
l.moveDown(1)
return true
case term.KeyPgup:
l.moveUp(l.height - 1)
return true
case term.KeyPgdn, term.KeySpace:
l.moveDown(l.height - 1)
return true
}
switch event.Ch {
case 'k', 'K':
l.moveUp(1)
return true
case 'j', 'J':
l.moveDown(1)
return true
case 'u', 'U':
l.moveUp(l.height - 1)
return true
case 'd', 'D':
l.moveDown(l.height - 1)
return true
default:
return false
}
case EventMouse:
return l.processMouseClick(event)
}
return false
}
// OnDrawLine is called every time the reader is going to display a line
// the argument of the function is the line number to display
func (l *TextDisplay) OnDrawLine(fn func(int) string) {
l.onDrawLine = fn
}
// OnPositionChanged is called every time the reader changes the top line or
// the total number of lines is changed
// Callback gets two numbers: the current top line, and the total number of
// lines. Top line number starts from 0.
func (l *TextDisplay) OnPositionChanged(fn func(int, int)) {
l.onPositionChanged = fn
}
func (l *TextDisplay) LineCount() int {
return l.lineCount
}
func (l *TextDisplay) SetLineCount(lineNo int) {
if l.topLine == lineNo {
return
}
if lineNo < l.topLine-1 {
l.topLine = lineNo - 1
}
l.lineCount = lineNo
if l.onPositionChanged != nil {
l.onPositionChanged(l.topLine, l.lineCount)
}
}
func (l *TextDisplay) TopLine() int {
return l.topLine
}
func (l *TextDisplay) SetTopLine(top int) {
if top < l.lineCount {
l.topLine = top
if l.onPositionChanged != nil {
l.onPositionChanged(l.topLine, l.lineCount)
}
}
}