forked from gdamore/tcell
/
text.go
279 lines (249 loc) · 6.27 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
// Copyright 2015 The Tcell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package views
import (
"github.com/mattn/go-runewidth"
"github.com/gdamore/tcell"
)
// Text is a Widget with containing a block of text, which can optionally
// be styled.
type Text struct {
view View
align Alignment
style tcell.Style
text []rune
widths []int
styles []tcell.Style
lengths []int
width int
height int
WidgetWatchers
}
func (t *Text) clear() {
v := t.view
w, h := v.Size()
v.Clear()
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
v.SetContent(x, y, ' ', nil, t.style)
}
}
}
// calcY figures the initial Y offset. Alignment is top by default.
func (t *Text) calcY(height int) int {
if t.align&VAlignCenter != 0 {
return (height - len(t.lengths)) / 2
}
if t.align&VAlignBottom != 0 {
return height - len(t.lengths)
}
return 0
}
// calcX figures the initial X offset for the given line.
// Alignment is left by default.
func (t *Text) calcX(width, line int) int {
if t.align&HAlignCenter != 0 {
return (width - t.lengths[line]) / 2
}
if t.align&HAlignRight != 0 {
return width - t.lengths[line]
}
return 0
}
// Draw draws the Text.
func (t *Text) Draw() {
v := t.view
if v == nil {
return
}
width, height := v.Size()
if width == 0 || height == 0 {
return
}
t.clear()
// Note that we might wind up with a negative X if the width
// is larger than the length. That's OK, and correct even.
// The view will clip it properly in that case.
// We align to the left & top by default.
y := t.calcY(height)
r := rune(0)
w := 0
x := 0
var styl tcell.Style
var comb []rune
line := 0
newline := true
for i, l := range t.text {
if newline {
x = t.calcX(width, line)
newline = false
}
if l == '\n' {
if w != 0 {
v.SetContent(x, y, r, comb, styl)
}
newline = true
w = 0
comb = nil
line++
y++
continue
}
if t.widths[i] == 0 {
comb = append(comb, l)
continue
}
if w != 0 {
v.SetContent(x, y, r, comb, styl)
x += w
}
r = l
w = t.widths[i]
styl = t.styles[i]
comb = nil
}
if w != 0 {
v.SetContent(x, y, r, comb, styl)
}
}
// Size returns the width and height in character cells of the Text.
func (t *Text) Size() (int, int) {
if len(t.text) != 0 {
return t.width, t.height
}
return 0, 0
}
// SetAlignment sets the alignment. Negative values
// indicate right justification, positive values are left,
// and zero indicates center aligned.
func (t *Text) SetAlignment(align Alignment) {
if align != t.align {
t.align = align
t.PostEventWidgetContent(t)
}
}
// Alignment returns the alignment of the Text.
func (t *Text) Alignment() Alignment {
return t.align
}
// SetView sets the View object used for the text bar.
func (t *Text) SetView(view View) {
t.view = view
}
// HandleEvent implements a tcell.EventHandler, but does nothing.
func (t *Text) HandleEvent(tcell.Event) bool {
return false
}
// SetText sets the text used for the string. Any previously set
// styles on individual rune indices are reset, and the default style
// for the widget is set.
func (t *Text) SetText(s string) {
t.width = 0
t.text = []rune(s)
if len(t.widths) < len(t.text) {
t.widths = make([]int, len(t.text))
} else {
t.widths = t.widths[0:len(t.text)]
}
if len(t.styles) < len(t.text) {
t.styles = make([]tcell.Style, len(t.text))
} else {
t.styles = t.styles[0:len(t.text)]
}
t.lengths = []int{}
length := 0
for i, r := range t.text {
t.widths[i] = runewidth.RuneWidth(r)
t.styles[i] = t.style
if r == '\n' {
t.lengths = append(t.lengths, length)
if length > t.width {
t.width = length
length = 0
}
} else if t.widths[i] == 0 && length == 0 {
// If first character on line is combining, inject
// a leading space. (Shame on the caller!)
t.widths = append(t.widths, 0)
copy(t.widths[i+1:], t.widths[i:])
t.widths[i] = 1
t.text = append(t.text, ' ')
copy(t.text[i+1:], t.text[i:])
t.text[i] = ' '
t.styles = append(t.styles, t.style)
copy(t.styles[i+1:], t.styles[i:])
t.styles[i] = t.style
length++
} else {
length += t.widths[i]
}
}
if length > 0 {
t.lengths = append(t.lengths, length)
if length > t.width {
t.width = length
}
}
t.height = len(t.lengths)
t.PostEventWidgetContent(t)
}
// Text returns the text that was set.
func (t *Text) Text() string {
return string(t.text)
}
// SetStyle sets the style used. This applies to every cell in the
// in the text.
func (t *Text) SetStyle(style tcell.Style) {
t.style = style
for i := 0; i < len(t.text); i++ {
if t.widths[i] != 0 {
t.styles[i] = t.style
}
}
t.PostEventWidgetContent(t)
}
// Style returns the previously set default style. Note that
// individual characters may have different styles.
func (t *Text) Style() tcell.Style {
return t.style
}
// SetStyleAt sets the style at the given rune index. Note that for
// strings containing combining characters, it is not possible to
// change the style at the position of the combining character, but
// those positions *do* count for calculating the index. A lot of
// complexity can be avoided by avoiding the use of combining characters.
func (t *Text) SetStyleAt(pos int, style tcell.Style) {
if pos < 0 || pos >= len(t.text) || t.widths[pos] < 1 {
return
}
t.styles[pos] = style
t.PostEventWidgetContent(t)
}
// StyleAt gets the style at the given rune index. If an invalid
// index is given, or the index is a combining character, then
// tcell.StyleDefault is returned.
func (t *Text) StyleAt(pos int) tcell.Style {
if pos < 0 || pos >= len(t.text) || t.widths[pos] < 1 {
return tcell.StyleDefault
}
return t.styles[pos]
}
// Resize is called when our View changes sizes.
func (t *Text) Resize() {
t.PostEventWidgetResize(t)
}
// NewText creates an empty Text.
func NewText() *Text {
return &Text{}
}