-
Notifications
You must be signed in to change notification settings - Fork 301
/
text.go
174 lines (146 loc) · 3.8 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
package rty
import (
"fmt"
"github.com/windmilleng/tcell"
)
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 {
t = TranslateANSI(t)
colorIndices, colors, _, _, _ := decomposeString(t)
var colorPos int
var foregroundColor, backgroundColor, attributes string
var chs []rune
flush := func() {
if len(chs) == 0 {
return
}
b.directives = append(b.directives, textDirective(string(chs)))
chs = nil
}
for pos, ch := range t {
// Handle color tags.
if colorPos < len(colorIndices) && pos >= colorIndices[colorPos][0] && pos < colorIndices[colorPos][1] {
if pos == colorIndices[colorPos][1]-1 {
flush()
foregroundColor, backgroundColor, attributes = styleFromTag(foregroundColor, backgroundColor, attributes, colors[colorPos])
colorPos++
b.directives = append(b.directives, fgDirective(tcell.GetColor(foregroundColor)))
b.directives = append(b.directives, bgDirective(tcell.GetColor(backgroundColor)))
}
continue
}
chs = append(chs, ch)
}
flush()
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
}
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) {
return l.render(nil, availWidth, availHeight)
}
func (l *StringLayout) Render(w Writer, width int, height int) error {
l.render(w, width, height)
return nil
}
// returns width, height for laying out full string
func (l *StringLayout) render(w Writer, width int, height int) (int, int) {
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:
panic(fmt.Errorf("StringLayout.Render: unexpected directive %T %+v", d, d))
}
// now we know it's a text directive
for _, ch := range s {
// TODO(dbentley): combining characters
// TODO(dbentley): tab, etc.
// TODO(dbentley): runewidth
if nextX >= width {
nextX, nextY = 0, nextY+1
}
if nextX+1 > maxWidth {
maxWidth = nextX + 1
}
if nextY >= height {
return maxWidth, height
}
if ch == '\n' {
if nextX == 0 && w != nil {
// make sure we take up our space
w.SetContent(nextX, nextY, ch, nil)
}
nextX, nextY = 0, nextY+1
continue
}
if w != nil {
w.SetContent(nextX, nextY, ch, nil)
}
nextX = nextX + 1
}
}
if nextY == 0 {
nextY = 1
}
return maxWidth, nextY
}