-
Notifications
You must be signed in to change notification settings - Fork 51
/
textutil.go
277 lines (243 loc) · 6.57 KB
/
textutil.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
package clui
import (
xs "github.com/huandu/xstrings"
term "github.com/nsf/termbox-go"
"regexp"
"strings"
)
var (
colorMap = map[string]term.Attribute{
"default": term.ColorDefault,
"black": term.ColorBlack,
"red": term.ColorRed,
"green": term.ColorGreen,
"yellow": term.ColorYellow,
"blue": term.ColorBlue,
"magenta": term.ColorMagenta,
"cyan": term.ColorCyan,
"white": term.ColorWhite,
"bold": term.AttrBold,
"bright": term.AttrBold, // windows make color brighter when it is bold
"underline": term.AttrUnderline,
"underlined": term.AttrUnderline,
"reverse": term.AttrReverse,
}
)
// Ellipsize truncates text to maxWidth by replacing a
// substring in the middle with ellipsis and keeping
// the beginning and ending of the string untouched.
// If maxWidth is less than 5 then no ellipsis is
// added, the text is just truncated from the right.
func Ellipsize(str string, maxWidth int) string {
ln := xs.Len(str)
if ln <= maxWidth {
return str
}
if maxWidth < 5 {
return xs.Slice(str, 0, maxWidth)
}
left := int((maxWidth - 3) / 2)
right := maxWidth - left - 3
return xs.Slice(str, 0, left) + "..." + xs.Slice(str, ln-right, -1)
}
// CutText makes a text no longer than maxWidth
func CutText(str string, maxWidth int) string {
ln := xs.Len(str)
if ln <= maxWidth {
return str
}
return xs.Slice(str, 0, maxWidth)
}
// AlignText calculates the initial position of the text
// output depending on str length and available width.
// The str is truncated in case of its lenght greater than
// width. Function returns shift that should be added to
// original label position before output instead of padding
// the string with spaces. The reason is to make possible
// to draw a label aligned but with transparent beginning
// and ending. If you do not need transparency you can
// add spaces manually using the returned shift value
func AlignText(str string, width int, align Align) (shift int, out string) {
length := xs.Len(str)
if length >= width {
return 0, CutText(str, width)
}
if align == AlignRight {
return width - length, str
} else if align == AlignCenter {
return (width - length) / 2, str
}
return 0, str
}
// AlignColorizedText does the same as AlignText does but
// it preserves the color of the letters by adding correct
// color tags to the line beginning.
// Note: function is ineffective and a bit slow - do not use
// it everywhere
func AlignColorizedText(str string, width int, align Align) (int, string) {
rawText := UnColorizeText(str)
length := xs.Len(rawText)
if length <= width {
shift, _ := AlignText(rawText, width, align)
return shift, str
}
skip := 0
if align == AlignRight {
skip = length - width
} else if align == AlignCenter {
skip = (length - width) / 2
}
fgChanged, bgChanged := false, false
curr := 0
parser := NewColorParser(str, term.ColorBlack, term.ColorBlack)
out := ""
for curr < skip+width {
elem := parser.NextElement()
if elem.Type == ElemEndOfText {
break
}
if elem.Type == ElemPrintable {
curr++
if curr == skip+1 {
if fgChanged {
out += "<t:" + ColorToString(elem.Fg) + ">"
}
if bgChanged {
out += "<b:" + ColorToString(elem.Bg) + ">"
}
out += string(elem.Ch)
} else if curr > skip+1 {
out += string(elem.Ch)
}
} else if elem.Type == ElemTextColor {
fgChanged = true
if curr > skip+1 {
out += "<t:" + ColorToString(elem.Fg) + ">"
}
} else if elem.Type == ElemBackColor {
bgChanged = true
if curr > skip+1 {
out += "<b:" + ColorToString(elem.Bg) + ">"
}
}
}
return 0, out
}
// SliceColorized returns a slice of text with correct color
// tags. start and end are real printable rune indices
func SliceColorized(str string, start, end int) string {
if str == "" {
return str
}
if start < 0 {
start = 0
}
fgChanged, bgChanged := false, false
curr := 0
parser := NewColorParser(str, term.ColorBlack, term.ColorBlack)
var out string
for {
if end != -1 && curr >= end {
break
}
elem := parser.NextElement()
if elem.Type == ElemEndOfText {
break
}
switch elem.Type {
case ElemTextColor:
fgChanged = true
if out != "" {
out += "<t:" + ColorToString(elem.Fg) + ">"
}
case ElemBackColor:
bgChanged = true
if out != "" {
out += "<b:" + ColorToString(elem.Bg) + ">"
}
case ElemPrintable:
if curr == start {
if fgChanged {
out += "<t:" + ColorToString(elem.Fg) + ">"
}
if bgChanged {
out += "<b:" + ColorToString(elem.Bg) + ">"
}
}
if curr >= start {
out += string(elem.Ch)
}
curr++
}
}
return out
}
// UnColorizeText removes all color-related tags from the
// string. Tags to remove: <(f|t|b|c):.*>
func UnColorizeText(str string) string {
rx := regexp.MustCompile("<(f|c|t|b):[^>]*>")
return rx.ReplaceAllString(str, "")
}
// StringToColor returns attribute by its string description.
// Description is the list of attributes separated with
// spaces, plus or pipe symbols. You can use 8 base colors:
// black, white, red, green, blue, magenta, yellow, cyan
// and a few modifiers:
// bold or bright, underline or underlined, reverse
// Note: some terminals do not support all modifiers, e.g,
// Windows one understands only bold/bright - it makes the
// color brighter with the modidierA
// Examples: "red bold", "green+underline+bold"
func StringToColor(str string) term.Attribute {
var parts []string
if strings.ContainsRune(str, '+') {
parts = strings.Split(str, "+")
} else if strings.ContainsRune(str, '|') {
parts = strings.Split(str, "|")
} else if strings.ContainsRune(str, ' ') {
parts = strings.Split(str, " ")
} else {
parts = append(parts, str)
}
var clr term.Attribute
for _, item := range parts {
item = strings.Trim(item, " ")
item = strings.ToLower(item)
c, ok := colorMap[item]
if ok {
clr |= c
}
}
return clr
}
// GetColorMap returns the color map (id is the color name, value its code)
func GetColorMap() map[string]term.Attribute {
return colorMap
}
// SetColorMap sets a custom color map (id is the color name, value its code)
func SetColorMap(cmap map[string]term.Attribute) {
colorMap = cmap
}
// ColorToString returns string representation of the attribute
func ColorToString(attr term.Attribute) string {
var out string
rawClr := attr & 15
if rawClr < 8 {
for k, v := range colorMap {
if v == rawClr {
out += k + " "
break
}
}
}
if attr&term.AttrBold != 0 {
out += "bold "
}
if attr&term.AttrUnderline != 0 {
out += "underline "
}
if attr&term.AttrReverse != 0 {
out += "reverse "
}
return strings.TrimSpace(out)
}