-
Notifications
You must be signed in to change notification settings - Fork 301
/
text.go
158 lines (132 loc) · 3.31 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
package rty
import (
"fmt"
"io"
"github.com/gdamore/tcell"
"github.com/pkg/errors"
)
type StringBuilder interface {
Text(string) StringBuilder
Textf(string, ...interface{}) StringBuilder
Fg(tcell.Color) StringBuilder
Bg(tcell.Color) StringBuilder
Build() Component
}
func NewStringBuilder() StringBuilder {
return &stringBuilder{}
}
type directive interface {
directive()
}
type textDirective string
type fgDirective tcell.Color
type bgDirective tcell.Color
func (textDirective) directive() {}
func (fgDirective) directive() {}
func (bgDirective) directive() {}
type stringBuilder struct {
directives []directive
}
func (b *stringBuilder) Text(t string) StringBuilder {
writer := ANSIWriter()
_, _ = writer.Write([]byte(t))
b.directives = append(b.directives, writer.directives...)
return b
}
func (b *stringBuilder) Textf(format string, a ...interface{}) StringBuilder {
b.Text(fmt.Sprintf(format, a...))
return b
}
func (b *stringBuilder) Fg(c tcell.Color) StringBuilder {
b.directives = append(b.directives, fgDirective(c))
return b
}
func (b *stringBuilder) Bg(c tcell.Color) StringBuilder {
b.directives = append(b.directives, bgDirective(c))
return b
}
func (b *stringBuilder) Build() Component {
return &StringLayout{directives: b.directives}
}
type StringLayout struct {
directives []directive
}
var _ Component = &StringLayout{}
func TextString(s string) Component {
return NewStringBuilder().Text(s).Build()
}
func ColoredString(s string, fg tcell.Color) Component {
return NewStringBuilder().Fg(fg).Text(s).Build()
}
func BgColoredString(s string, fg tcell.Color, bg tcell.Color) Component {
return NewStringBuilder().Fg(fg).Bg(bg).Text(s).Build()
}
func (l *StringLayout) Size(availWidth int, availHeight int) (int, int, error) {
return l.render(nil, availWidth, availHeight)
}
func (l *StringLayout) Render(w Writer, width int, height int) error {
_, _, err := l.render(w, width, height)
return err
}
// returns width, height for laying out full string
func (l *StringLayout) render(w Writer, width int, height int) (int, int, error) {
nextX, nextY := 0, 0
maxWidth := 0
for _, d := range l.directives {
var s string
switch d := d.(type) {
case textDirective:
s = string(d)
case fgDirective:
if w != nil {
w = w.Foreground(tcell.Color(d))
}
continue
case bgDirective:
if w != nil {
w = w.Background(tcell.Color(d))
}
continue
default:
return 0, 0, fmt.Errorf("StringLayout.Render: unexpected directive %T %+v", d, d)
}
// now we know it's a text directive
tokenizer := NewTokenizer(s)
for {
token, err := tokenizer.Next()
if err != nil {
if err == io.EOF {
break
}
return 0, 0, errors.Wrapf(err, "StringLayout.Tokenize")
}
if nextX != 0 && nextX+len(token)-1 >= width {
nextX, nextY = 0, nextY+1
}
for _, r := range token {
if nextX >= width {
nextX, nextY = 0, nextY+1
}
if nextX+1 > maxWidth {
maxWidth = nextX + 1
}
if nextY >= height {
return maxWidth, height, nil
}
if r == '\n' {
if nextX == 0 && w != nil {
// make sure we take up our space
w.SetContent(nextX, nextY, r, nil)
}
nextX, nextY = 0, nextY+1
continue
}
if w != nil {
w.SetContent(nextX, nextY, r, nil)
}
nextX += 1
}
}
}
return maxWidth, nextY + 1, nil
}