forked from rivo/tview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
612 lines (576 loc) · 18.3 KB
/
strings.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package tview
import (
"math/rand"
"regexp"
"strconv"
"strings"
"unicode/utf8"
"github.com/gdamore/tcell/v2"
"github.com/rivo/uniseg"
)
// escapedTagPattern matches an escaped tag, e.g. "[red[]", at the beginning of
// a string.
var escapedTagPattern = regexp.MustCompile(`^\[[^\[\]]+\[+\]`)
// stepOptions is a bit field of options for [step]. A value of 0 results in
// [step] having the same behavior as uniseg.Step, i.e. no tview-related parsing
// is performed.
type stepOptions int
// Bit fields for [stepOptions].
const (
stepOptionsNone stepOptions = 0
stepOptionsStyle stepOptions = 1 << iota // Parse style tags.
stepOptionsRegion // Parse region tags.
)
// stepState represents the current state of the parser implemented in [step].
type stepState struct {
unisegState int // The state of the uniseg parser.
boundaries int // Information about boundaries, as returned by uniseg.Step.
style tcell.Style // The current style.
region string // The current region.
escapedTagState int // States for parsing escaped tags (defined in [step]).
grossLength int // The length of the cluster, including any tags not returned.
// The styles for the initial call to [step].
initialForeground tcell.Color
initialBackground tcell.Color
initialAttributes tcell.AttrMask
}
// IsWordBoundary returns true if the boundary between the returned grapheme
// cluster and the one following it is a word boundary.
func (s *stepState) IsWordBoundary() bool {
return s.boundaries&uniseg.MaskWord != 0
}
// IsSentenceBoundary returns true if the boundary between the returned grapheme
// cluster and the one following it is a sentence boundary.
func (s *stepState) IsSentenceBoundary() bool {
return s.boundaries&uniseg.MaskSentence != 0
}
// LineBreak returns whether the string can be broken into the next line after
// the returned grapheme cluster. If optional is true, the line break is
// optional. If false, the line break is mandatory, e.g. after a newline
// character.
func (s *stepState) LineBreak() (lineBreak, optional bool) {
switch s.boundaries & uniseg.MaskLine {
case uniseg.LineCanBreak:
return true, true
case uniseg.LineMustBreak:
return true, false
}
return false, false // uniseg.LineDontBreak.
}
// Width returns the grapheme cluster's width in cells.
func (s *stepState) Width() int {
return s.boundaries >> uniseg.ShiftWidth
}
// GrossLength returns the grapheme cluster's length in bytes, including any
// tags that were parsed but not explicitly returned.
func (s *stepState) GrossLength() int {
return s.grossLength
}
// Style returns the style for the grapheme cluster.
func (s *stepState) Style() tcell.Style {
return s.style
}
// step uses uniseg.Step to iterate over the grapheme clusters of a string but
// (optionally) also parses the string for style or region tags.
//
// This function can be called consecutively to extract all grapheme clusters
// from str, without returning any contained (parsed) tags. The return values
// are the first grapheme cluster, the remaining string, and the new state. Pass
// the remaining string and the returned state to the next call. If the rest
// string is empty, parsing is complete. Call the returned state's methods for
// boundary and cluster width information.
//
// The returned cluster may be empty if the given string consists of only
// (parsed) tags. The boundary and width information will be meaningless in
// this case but the style will describe the style at the end of the string.
//
// Pass nil for state on the first call. This will assume an initial style with
// [Styles.PrimitiveBackgroundColor] as the background color and
// [Styles.PrimaryTextColor] as the text color, no current region. If you want
// to start with a different style or region, you can set the state accordingly
// but you must then set [state.unisegState] to -1.
//
// There is no need to call uniseg.HasTrailingLineBreakInString on the last
// non-empty cluster as this function will do this for you and adjust the
// returned boundaries accordingly.
func step(str string, state *stepState, opts stepOptions) (cluster, rest string, newState *stepState) {
// Set up initial state.
if state == nil {
state = &stepState{
unisegState: -1,
style: tcell.StyleDefault.Background(Styles.PrimitiveBackgroundColor).Foreground(Styles.PrimaryTextColor),
}
}
if state.unisegState < 0 {
state.initialForeground, state.initialBackground, state.initialAttributes = state.style.Decompose()
}
if len(str) == 0 {
newState = state
return
}
// Get a grapheme cluster.
preState := state.unisegState
cluster, rest, state.boundaries, state.unisegState = uniseg.StepString(str, preState)
state.grossLength = len(cluster)
if rest == "" {
if !uniseg.HasTrailingLineBreakInString(cluster) {
state.boundaries &^= uniseg.MaskLine
}
}
// Parse tags.
if opts != 0 {
const (
etNone int = iota
etStart
etChar
etClosing
)
// Finite state machine for escaped tags.
switch state.escapedTagState {
case etStart:
if cluster[0] == '[' || cluster[0] == ']' { // Invalid escaped tag.
state.escapedTagState = etNone
} else { // Other characters are allowed.
state.escapedTagState = etChar
}
case etChar:
if cluster[0] == ']' { // In theory, this should not happen.
state.escapedTagState = etNone
} else if cluster[0] == '[' { // Starting closing sequence.
// Swallow the first one.
cluster, rest, state.boundaries, state.unisegState = uniseg.StepString(rest, preState)
state.grossLength += len(cluster)
if cluster[0] == ']' {
state.escapedTagState = etNone
} else {
state.escapedTagState = etClosing
}
} // More characters. Remain in etChar.
case etClosing:
if cluster[0] != '[' {
state.escapedTagState = etNone
}
}
// Regular tags.
if state.escapedTagState == etNone {
if cluster[0] == '[' {
// We've already opened a tag. Parse it.
length, style, region := parseTag(str, state)
if length > 0 {
state.style = style
state.region = region
cluster, rest, state.boundaries, state.unisegState = uniseg.StepString(str[length:], preState)
state.grossLength = len(cluster) + length
if rest == "" {
if !uniseg.HasTrailingLineBreakInString(cluster) {
state.boundaries &^= uniseg.MaskLine
}
}
}
// Is this an escaped tag?
if escapedTagPattern.MatchString(str[length:]) {
state.escapedTagState = etStart
}
}
if len(rest) > 0 && rest[0] == '[' {
// A tag might follow the cluster. If so, we need to fix the state
// for the boundaries to be correct.
if length, _, _ := parseTag(rest, state); length > 0 {
if len(rest) > length {
_, l := utf8.DecodeRuneInString(rest[length:])
cluster += rest[length : length+l]
}
var taglessRest string
cluster, taglessRest, state.boundaries, state.unisegState = uniseg.StepString(cluster, preState)
if taglessRest == "" {
if !uniseg.HasTrailingLineBreakInString(cluster) {
state.boundaries &^= uniseg.MaskLine
}
}
}
}
}
}
newState = state
return
}
// parseTag parses str for consecutive style and/or region tags, assuming that
// str starts with the opening bracket for the first tag. It returns the string
// length of all valid tags (0 if the first tag is not valid) and the updated
// style and region for valid tags (based on the provided state).
func parseTag(str string, state *stepState) (length int, style tcell.Style, region string) {
// Automata states for parsing tags.
const (
tagStateNone = iota
tagStateDoneTag
tagStateStart
tagStateRegionStart
tagStateEndForeground
tagStateStartBackground
tagStateNumericForeground
tagStateNameForeground
tagStateEndBackground
tagStateStartAttributes
tagStateNumericBackground
tagStateNameBackground
tagStateAttributes
tagStateRegionEnd
tagStateRegionName
tagStateEndAttributes
tagStateStartURL
tagStateEndURL
tagStateURL
)
// Helper function which checks if the given byte is one of a list of
// characters, including letters and digits.
isOneOf := func(b byte, chars string) bool {
if b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= '0' && b <= '9' {
return true
}
return strings.IndexByte(chars, b) >= 0
}
// Attribute map.
attrs := map[byte]tcell.AttrMask{
'B': tcell.AttrBold,
'U': tcell.AttrUnderline,
'I': tcell.AttrItalic,
'L': tcell.AttrBlink,
'D': tcell.AttrDim,
'S': tcell.AttrStrikeThrough,
'R': tcell.AttrReverse,
}
var (
tagState, tagLength int
tempStr strings.Builder
)
tStyle := state.style
tRegion := state.region
// Process state transitions.
for len(str) > 0 {
ch := str[0]
str = str[1:]
tagLength++
// Transition.
switch tagState {
case tagStateNone:
if ch == '[' { // Start of a tag.
tagState = tagStateStart
} else { // Not a tag. We're done.
return
}
case tagStateStart:
if ch == '"' { // Start of a region tag.
tempStr.Reset()
tagState = tagStateRegionStart
} else if !isOneOf(ch, "#:-") { // Invalid style tag.
return
} else if ch == '-' { // Reset foreground color.
tStyle = tStyle.Foreground(state.initialForeground)
tagState = tagStateEndForeground
} else if ch == ':' { // No foreground color.
tagState = tagStateStartBackground
} else {
tempStr.Reset()
tempStr.WriteByte(ch)
if ch == '#' { // Numeric foreground color.
tagState = tagStateNumericForeground
} else { // Letters or numbers.
tagState = tagStateNameForeground
}
}
case tagStateEndForeground:
if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == ':' {
tagState = tagStateStartBackground
} else { // Invalid tag.
return
}
case tagStateNumericForeground:
if ch == ']' || ch == ':' {
if tempStr.Len() != 7 { // Must be #rrggbb.
return
}
tStyle = tStyle.Foreground(tcell.GetColor(tempStr.String()))
}
if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == ':' { // Start of background color.
tagState = tagStateStartBackground
} else if strings.IndexByte("0123456789abcdefABCDEF", ch) >= 0 { // Hex digit.
tempStr.WriteByte(ch)
tagState = tagStateNumericForeground
} else { // Invalid tag.
return
}
case tagStateNameForeground:
if ch == ']' || ch == ':' {
name := tempStr.String()
if name[0] >= '0' && name[0] <= '9' { // Must not start with a digit.
return
}
tStyle = tStyle.Foreground(tcell.ColorNames[name])
}
if !isOneOf(ch, "]:") { // Invalid tag.
return
} else if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == ':' { // Start of background color.
tagState = tagStateStartBackground
} else { // Letters or numbers.
tempStr.WriteByte(ch)
}
case tagStateStartBackground:
if !isOneOf(ch, "#:-]") { // Invalid style tag.
return
} else if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == '-' { // Reset background color.
tStyle = tStyle.Background(state.initialBackground)
tagState = tagStateEndBackground
} else if ch == ':' { // No background color.
tagState = tagStateStartAttributes
} else {
tempStr.Reset()
tempStr.WriteByte(ch)
if ch == '#' { // Numeric background color.
tagState = tagStateNumericBackground
} else { // Letters or numbers.
tagState = tagStateNameBackground
}
}
case tagStateEndBackground:
if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == ':' { // Start of attributes.
tagState = tagStateStartAttributes
} else { // Invalid tag.
return
}
case tagStateNumericBackground:
if ch == ']' || ch == ':' {
if tempStr.Len() != 7 { // Must be #rrggbb.
return
}
tStyle = tStyle.Background(tcell.GetColor(tempStr.String()))
}
if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == ':' { // Start of attributes.
tagState = tagStateStartAttributes
} else if strings.IndexByte("0123456789abcdefABCDEF", ch) >= 0 { // Hex digit.
tempStr.WriteByte(ch)
tagState = tagStateNumericBackground
} else { // Invalid tag.
return
}
case tagStateNameBackground:
if ch == ']' || ch == ':' {
name := tempStr.String()
if name[0] >= '0' && name[0] <= '9' { // Must not start with a digit.
return
}
tStyle = tStyle.Background(tcell.ColorNames[name])
}
if !isOneOf(ch, "]:") { // Invalid tag.
return
} else if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == ':' { // Start of background color.
tagState = tagStateStartAttributes
} else { // Letters or numbers.
tempStr.WriteByte(ch)
}
case tagStateStartAttributes:
if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == '-' { // Reset attributes.
tStyle = tStyle.Attributes(state.initialAttributes)
tagState = tagStateEndAttributes
} else if ch == ':' { // Start of URL.
tagState = tagStateStartURL
} else if strings.IndexByte("buildsrBUILDSR", ch) >= 0 { // Attribute tag.
tempStr.Reset()
tempStr.WriteByte(ch)
tagState = tagStateAttributes
} else { // Invalid tag.
return
}
case tagStateAttributes:
if ch == ']' || ch == ':' {
flags := tempStr.String()
_, _, a := tStyle.Decompose()
for index := 0; index < len(flags); index++ {
ch := flags[index]
if ch >= 'a' && ch <= 'z' {
a |= attrs[ch-('a'-'A')]
} else {
a &^= attrs[ch]
}
}
tStyle = tStyle.Attributes(a)
}
if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == ':' { // Start of URL.
tagState = tagStateStartURL
} else if strings.IndexByte("buildsrBUILDSR", ch) >= 0 { // Attribute tag.
tempStr.WriteByte(ch)
} else { // Invalid tag.
return
}
case tagStateEndAttributes:
if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == ':' { // Start of URL.
tagState = tagStateStartURL
} else { // Invalid tag.
return
}
case tagStateStartURL:
if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else if ch == '-' { // Reset URL.
tStyle = tStyle.Url("").UrlId("")
tagState = tagStateEndURL
} else { // URL character.
tempStr.Reset()
tempStr.WriteByte(ch)
tStyle = tStyle.UrlId(strconv.Itoa(int(rand.Uint32()))) // Generate a unique ID for this URL.
tagState = tagStateURL
}
case tagStateEndURL:
if ch == ']' { // End of tag.
tagState = tagStateDoneTag
} else { // Invalid tag.
return
}
case tagStateURL:
if ch == ']' { // End of tag.
tStyle = tStyle.Url(tempStr.String())
tagState = tagStateDoneTag
} else { // URL character.
tempStr.WriteByte(ch)
}
case tagStateRegionStart:
if ch == '"' { // End of region tag.
tagState = tagStateRegionEnd
} else if isOneOf(ch, "_,;: -.") { // Region name.
tempStr.WriteByte(ch)
tagState = tagStateRegionName
} else { // Invalid tag.
return
}
case tagStateRegionEnd:
if ch == ']' { // End of tag.
tRegion = tempStr.String()
tagState = tagStateDoneTag
} else { // Invalid tag.
return
}
case tagStateRegionName:
if ch == '"' { // End of region tag.
tagState = tagStateRegionEnd
} else if isOneOf(ch, "_,;: -.") { // Region name.
tempStr.WriteByte(ch)
} else { // Invalid tag.
return
}
}
// The last transition led to a tag end. Make the tag permanent.
if tagState == tagStateDoneTag {
length, style, region = tagLength, tStyle, tRegion
tagState = tagStateNone // Reset state.
}
}
return
}
// TaggedStringWidth returns the width of the given string needed to print it on
// screen. The text may contain style tags which are not counted.
func TaggedStringWidth(text string) (width int) {
var state *stepState
for len(text) > 0 {
_, text, state = step(text, state, stepOptionsStyle)
width += state.Width()
}
return
}
// WordWrap splits a text such that each resulting line does not exceed the
// given screen width. Split points are determined using the algorithm described
// in [Unicode Standard Annex #14].
//
// This function considers style tags to have no width.
//
// [Unicode Standard Annex #14]: https://www.unicode.org/reports/tr14/
func WordWrap(text string, width int) (lines []string) {
if width <= 0 {
return
}
var (
state *stepState
lineWidth, lineLength, lastOption, lastOptionWidth int
)
str := text
for len(str) > 0 {
// Parse the next character.
_, str, state = step(str, state, stepOptionsStyle)
cWidth := state.Width()
// Would it exceed the line width?
if lineWidth+cWidth > width {
if lastOptionWidth == 0 {
// No split point so far. Just split at the current position.
lines = append(lines, text[:lineLength])
text = text[lineLength:]
lineWidth, lineLength, lastOption, lastOptionWidth = 0, 0, 0, 0
} else {
// Split at the last split point.
lines = append(lines, text[:lastOption])
text = text[lastOption:]
lineWidth -= lastOptionWidth
lineLength -= lastOption
lastOption, lastOptionWidth = 0, 0
}
}
// Move ahead.
lineWidth += cWidth
lineLength += state.GrossLength()
// Check for split points.
if lineBreak, optional := state.LineBreak(); lineBreak {
if optional {
// Remember this split point.
lastOption = lineLength
lastOptionWidth = lineWidth
} else {
// We must split here.
lines = append(lines, strings.TrimRight(text[:lineLength], "\n\r"))
text = text[lineLength:]
lineWidth, lineLength, lastOption, lastOptionWidth = 0, 0, 0, 0
}
}
}
lines = append(lines, text)
return
}
// Escape escapes the given text such that color and/or region tags are not
// recognized and substituted by the print functions of this package. For
// example, to include a tag-like string in a box title or in a TextView:
//
// box.SetTitle(tview.Escape("[squarebrackets]"))
// fmt.Fprint(textView, tview.Escape(`["quoted"]`))
func Escape(text string) string {
return nonEscapePattern.ReplaceAllString(text, "$1[]")
}
// stripTags strips style tags from the given string. (Region tags are not
// stripped.)
func stripTags(text string) string {
var (
str strings.Builder
state *stepState
)
for len(text) > 0 {
var c string
c, text, state = step(text, state, stepOptionsStyle)
str.WriteString(c)
}
return str.String()
}