-
Notifications
You must be signed in to change notification settings - Fork 1
/
text.go
306 lines (258 loc) · 8.22 KB
/
text.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
299
300
301
302
303
304
305
306
package termboxUI
//TODO: text box doesn't parse `\n\n` correctly. All text following these characters is ignored.
import (
"bufio"
"io"
"strings"
"github.com/nsf/termbox-go"
)
//======================================================//
// Useful Functions
//======================================================//
// This fills all of the cells of the terminal within a given rectangle to the specified attributes.
func FillArea(x, y, w, h int, fg, bg termbox.Attribute) {
for row := 0; row < h; row++ {
for column := 0; column < w; column++ {
termbox.SetCell(x+column, y+row, ' ', fg, bg)
}
}
return
}
// Draws a line to the terminal starting with the cell located at 'x' and continuing to the cell at 'w'
// Cells 'x' and 'w' are included.
func DrawHorizontalLine(x, y, w int, fg, bg termbox.Attribute) {
for i := 0; i <= w; i++ {
termbox.SetCell(x+i, y, '─', fg, bg)
}
return
}
// Draws a line to the terminal starting with the cell located at 'y' and continuing to the cell at 'h'
// Cells 'y' and 'h' are included.
func DrawVerticalLine(x, y, h int, fg, bg termbox.Attribute) {
for i := 0; i <= h; i++ {
termbox.SetCell(x, y+i, '│', fg, bg)
}
return
}
// Like FillArea, but it also draws a border around the area using the 'fg' attribute as the color.
func DrawRectangle(x, y, h, w int, fg, bg termbox.Attribute) {
FillArea(x, y, w, h, fg, bg)
DrawHorizontalLine(x, y, w, fg, bg) // top
DrawHorizontalLine(x, h+y, w, fg, bg) // bottom
DrawVerticalLine(x, y, h, fg, bg) // left
DrawVerticalLine(x+w, y, h, fg, bg) // right
termbox.SetCell(x, y, '┌', fg, bg) // top-left corner
termbox.SetCell(x+w, y, '┐', fg, bg) // top-right corner
termbox.SetCell(x, h+y, '└', fg, bg) // bottom-left corner
termbox.SetCell(x+w, h+y, '┘', fg, bg) // bottom-right corner
}
//======================================================//
// Basic Text
//======================================================//
// This is the most basic text drawing function.
// It writes a single line of text to the terminal with the specified settings.
func DrawText(x, y int, line string, fg, bg termbox.Attribute) (int, int) {
for i, ch := range line {
termbox.SetCell(x+i, y, ch, fg, bg)
}
return x + len(line), y
}
// This returns the termbox x coordinate to center the given string within the described area.
// That coordinate value returned should be referenced before drawing the text.
// Note that this doesn't actually draw the text string to the terminal.
func HorizontalCenterString(text string, dimension, offset int) int {
return (dimension-len(text))/2 + offset
}
//======================================================//
// Text Box
//======================================================//
// Values for passing in text alignment for text boxes.
// TexAlignmentDefault can be used in both vertical and horizontal cases when drawing the text box to the terminal.
const (
TextAlignmentLeft uint16 = iota
TextAlignmentRight
TextAlignmentCenter
TextAlignmentTop
TextAlignmentBottom
TextAlignmentDefault
)
// Basic text box for displaying text in a termbox window.
// HasBorder indicates that the border around the text box should be included when drawing. Note that the borders are drawn within the defined text box's area, effectively losing two columns and two rows of text writing area.
type TextBox struct {
HasBorder bool
WrapText bool
TextHorizontalJustification uint16
TextVerticalJustification uint16
Width int
Height int
Default_fg termbox.Attribute
Default_bg termbox.Attribute
text []string
textHeight int
activeIndex int
scrolling bool
reader io.Reader
}
// This will create a new text box definition.
// If the width or height exceed the dimensions of the termbox, then the screen dimension will be used in place of 'width' or 'height'
func CreateTextBox(width, height int, withBorder, wrapText bool, justification_h, justification_v uint16, fg, bg termbox.Attribute) *TextBox {
textbox := new(TextBox)
screenWidth, screenHeight := termbox.Size()
if width == -1 || width > screenWidth {
textbox.Width = screenWidth
} else {
textbox.Width = width
}
if height == -1 {
textbox.Height = screenHeight
} else {
textbox.Height = height
}
textbox.TextHorizontalJustification = justification_h
textbox.TextVerticalJustification = justification_v
textbox.Default_fg = fg
textbox.Default_bg = bg
textbox.HasBorder = withBorder
textbox.WrapText = wrapText
newHeight := textbox.Height
if textbox.HasBorder && textbox.Height > 2 {
newHeight -= 2
}
textbox.text = make([]string, newHeight)
textbox.textHeight = 0
textbox.activeIndex = 0
textbox.scrolling = true
textbox.reader = nil
return textbox
}
// This lets a text box accept a reader instead of an explicit string.
// The assumption is that the type of data from the read source is always 'string', at least for now...
func (tb *TextBox) AddTextFrom(strReader io.Reader) error {
tb.reader = strReader
return nil
}
// This adds a single line of text to the text box.
// The '\n' rune is translated to a new line and the '\t' rune is treated as four spaces.
func (tb *TextBox) AddText(text string) {
height := tb.Height
width := tb.Width
if tb.HasBorder {
height -= 2
width -= 2
}
var lines []string
linesHeight := 0
strArray := strings.Split(text, "\n")
for _, line := range strArray {
line = strings.Replace(line, "\t", " ", -1)
if len(line) == 0 {
break
}
if tb.WrapText && len(line) > width {
for len(line) != 0 {
var newLine = ""
if len(line) < width {
newLine = line
line = ""
} else {
newLine = line[:width-1]
line = line[width:]
}
if !tb.scrolling && linesHeight+tb.textHeight <= height {
lines = append(lines, newLine)
linesHeight++
} else {
break
}
}
} else {
if !tb.scrolling && linesHeight+tb.textHeight <= height {
lines = append(lines, line)
linesHeight++
} else {
lines = append(lines, line)
linesHeight++
}
}
}
if linesHeight > 0 {
temp := make([]string, linesHeight+tb.textHeight)
copy(temp, tb.text)
copy(temp[tb.textHeight:], lines)
tb.text = temp
tb.textHeight += linesHeight
}
}
// This will write the text box to the terminal. 'x' and 'y' are the upper-left coordinates from which the box will be drawn.
// The cell at that location is included when drawing.
// If the number of lines of the text box after wrapping is applied is larger than the height of the box, scrolling is automatically applied.
func (tb *TextBox) Draw(x, y int) {
if tb.reader != nil {
reader := bufio.NewReader(tb.reader)
str, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
panic(err)
}
if len(str) > 0 {
tb.AddText(str)
}
}
width := tb.Width
height := tb.Height
if tb.HasBorder {
DrawRectangle(x, y, height, width, tb.Default_fg, tb.Default_bg)
width -= 2
height -= 2
x++
y++
} else {
FillArea(x, y, width, height, tb.Default_fg, tb.Default_bg)
}
for i := 0; i <= height; i++ {
if i+tb.activeIndex > tb.textHeight {
break
}
if tb.activeIndex+i == len(tb.text) {
break
}
var x_coord, y_coord int
line := tb.text[tb.activeIndex+i]
switch tb.TextHorizontalJustification {
case TextAlignmentCenter:
x_coord = HorizontalCenterString(line, width, x)
case TextAlignmentRight:
x_coord = (x + width) - len(line) - 1
default:
x_coord = x
}
switch tb.TextVerticalJustification {
case TextAlignmentCenter:
y_coord = y + (height / 2) + i
case TextAlignmentBottom:
y_coord = (y + height) - tb.textHeight + i
default:
y_coord = y + i
}
DrawText(x_coord, y_coord, line, tb.Default_fg, tb.Default_bg)
}
}
// Handle the termbox key or character input.
// The up and down keys will scroll the text within the text box area.
// Any other input is ignored by text box.
func (tb *TextBox) HandleKey(key termbox.Key, ch rune, results chan UIEvent) bool {
eventConsumed := true
switch key {
case termbox.KeyArrowUp:
tb.activeIndex--
if tb.activeIndex < 0 {
tb.activeIndex = 0
}
case termbox.KeyArrowDown:
if !(tb.activeIndex+tb.Height >= tb.textHeight+1) {
tb.activeIndex++
}
default:
eventConsumed = false
}
return eventConsumed
}