-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentence_builder.go
379 lines (305 loc) · 13.3 KB
/
sentence_builder.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
// Copyright 2021 Nelson Germán Ghezzi. All rights reserved.
// Use of this source code is governed by a MIT license that
// can be found in the LICENSE file.
package coloring
import (
"container/list"
"fmt"
"strconv"
"strings"
)
// A SentenceBuilder allows to create a long string of text
// applying different style attributes to different parts of
// the sentence.
//
// It gives fine grained control of where the style attributes
// starts and ends independently of each other, allowing for styles
// spanning sections with different colors and attributes.
type SentenceBuilder struct {
sb strings.Builder
colorStack *list.List
backgroundColorStack *list.List
}
// Sentence creates a new SentenceBuilder.
func Sentence() *SentenceBuilder {
return &SentenceBuilder{strings.Builder{}, &list.List{}, &list.List{}}
}
func (builder *SentenceBuilder) write(component string) *SentenceBuilder {
builder.sb.WriteString(component)
return builder
}
// String returns a string with the currently built sentence. It also allows to use
// this instance of SenteceBuilder in any place that expects a Stringer type.
func (builder *SentenceBuilder) String() string {
return builder.sb.String()
}
// StyledText returns a StyledText instance that you can use in any place
// that expects a Stringer type. Further changes to this builder doesn't
// affect the returned StyledText.
//
// Note that the returned StyledText will contain a reset attribute at the end.
// This is to safely concatenate it with other strings without leaving open
// any style attribute.
func (builder *SentenceBuilder) StyledText() *StyledText {
return &StyledText{builder.sb.String() + resetSeq, unstyle(builder.sb.String())}
}
// Print reset all the styles and outputs the currently built sentence on os.Stdout.
func (builder *SentenceBuilder) Print() {
builder.Reset()
//nolint:forbidigo
fmt.Print(builder)
}
// PrintAndClear performs a SentenceBuilder.Print and then clears the current sentence
// buffer and returns this SentenceBuilder, allowing to start building a new sentence right away.
func (builder *SentenceBuilder) PrintAndClear() *SentenceBuilder {
builder.Print()
builder.sb.Reset()
return builder
}
// Println reset all the styles and outputs the currently built sentence on os.Stdout
// adding a new line at the end.
func (builder *SentenceBuilder) Println() {
builder.Reset()
//nolint:forbidigo
fmt.Println(builder)
}
// PrintlnAndClear performs a SentenceBuilder.Println and then clears the current sentence
// buffer and returns this SentenceBuilder, allowing to start building a new sentence right away.
func (builder *SentenceBuilder) PrintlnAndClear() *SentenceBuilder {
builder.Println()
builder.sb.Reset()
return builder
}
// Text adds a string of unformatted text to the sentence.
func (builder *SentenceBuilder) Text(text string) *SentenceBuilder {
return builder.write(text)
}
// Reset clears al formatting attributes currently applied to the sentence.
func (builder *SentenceBuilder) Reset() *SentenceBuilder {
return builder.write(resetSeq)
}
// Color adds a string of text with a specific color. The color
// must be in the range 0-255. See constants declared in the coloring
// package to access the most common ones (0-15).
func (builder *SentenceBuilder) Color(text string, color int) *SentenceBuilder {
return builder.ColorSet(color).write(text).ColorReset()
}
// ColorSet sets the current text color of the sentence. Further added text will
// have this color until another color is set or the color is reseted with ColorReset.
func (builder *SentenceBuilder) ColorSet(color int) *SentenceBuilder {
builder.colorStack.PushFront(color)
return builder.writeColor(color)
}
// ColorRgb adds a string of text with a specific color using RGB values. Each color
// component must be in the range 0-255. You can use hexadecimal numeric literals
// like ColorRgb("sample", 0xFF, 0xC0, 0x33).
func (builder *SentenceBuilder) ColorRgb(text string, r, g, b int) *SentenceBuilder {
return builder.ColorRgbSet(r, g, b).write(text).ColorReset()
}
// ColorRgbSet sets the current text color of the sentence using RGB values. Further added text
// will have this color until another color is set or the color is reseted with ColorReset.
// You can use hexadecimal numeric literals like ColorRgb("sample", 0xFF, 0xC0, 0x33).
func (builder *SentenceBuilder) ColorRgbSet(r, g, b int) *SentenceBuilder {
color := composeRgbColor(r, g, b)
builder.colorStack.PushFront(color)
return builder.writeColorRgb(color)
}
// ColorReset reverts the color to the one that was in use before the last call to
// ColorSet or ColorRgbSet.
func (builder *SentenceBuilder) ColorReset() *SentenceBuilder {
if currentColor := builder.colorStack.Front(); currentColor != nil {
builder.colorStack.Remove(currentColor)
}
if color := builder.colorStack.Front(); color != nil {
if colorInt, ok := color.Value.(int); ok {
builder.writeColor(colorInt)
}
if colorRgb, ok := color.Value.(string); ok {
builder.writeColorRgb(colorRgb)
}
} else {
builder.write(fgColorResetSeq)
}
return builder
}
// ColorDefault sets the color to the default one, irrespective of
// the current color stack, which is also cleared.
func (builder *SentenceBuilder) ColorDefault() *SentenceBuilder {
builder.colorStack.Init()
return builder.write(fgColorResetSeq)
}
// Background adds a string of text with a specific background color. The color
// must be in the range 0-255. See constants declared in the coloring
// package to access the most common ones (0-15).
func (builder *SentenceBuilder) Background(text string, color int) *SentenceBuilder {
return builder.BackgroundSet(color).write(text).BackgroundReset()
}
// BackgroundSet sets the current background color of the sentence. Further added text
// will have this background color until another background color is set or the background
// color is reseted with BackgroundReset.
func (builder *SentenceBuilder) BackgroundSet(color int) *SentenceBuilder {
builder.backgroundColorStack.PushFront(color)
return builder.writeBackgroundColor(color)
}
// BackgroundRgb adds a string of text with a specific background color using RGB values.
// Each color component must be in the range 0-255. You can use hexadecimal numeric
// literals like BackgroundRgb("sample", 0xFF, 0xC0, 0x33).
func (builder *SentenceBuilder) BackgroundRgb(text string, r, g, b int) *SentenceBuilder {
return builder.BackgroundRgbSet(r, g, b).write(text).BackgroundReset()
}
// BackgroundRgbSet sets the current background color of the sentence using RGB values.
// Further added text will have this background color until another background color is
// set or the background color is reseted with BackgroundReset. You can use hexadecimal
// numeric literals like ColorRgb("sample", 0xFF, 0xC0, 0x33).
func (builder *SentenceBuilder) BackgroundRgbSet(r, g, b int) *SentenceBuilder {
color := composeRgbColor(r, g, b)
builder.backgroundColorStack.PushFront(color)
return builder.writeBackgroundColorRgb(color)
}
// BackgroundReset reverts the background color to the one that was in use before the
// last call to BackgroundSet or BackgroundRgbSet.
func (builder *SentenceBuilder) BackgroundReset() *SentenceBuilder {
if currentColor := builder.backgroundColorStack.Front(); currentColor != nil {
builder.backgroundColorStack.Remove(currentColor)
}
if color := builder.backgroundColorStack.Front(); color != nil {
if colorInt, ok := color.Value.(int); ok {
builder.writeBackgroundColor(colorInt)
}
if colorRgb, ok := color.Value.(string); ok {
builder.writeBackgroundColorRgb(colorRgb)
}
} else {
builder.write(bgColorResetSeq)
}
return builder
}
// BackgroundDefault sets the background color to the default one, irrespective of
// the current background color stack, which is also cleared.
func (builder *SentenceBuilder) BackgroundDefault() *SentenceBuilder {
builder.backgroundColorStack.Init()
return builder.write(bgColorResetSeq)
}
// Bold adds a string of bold text to the sentence.
func (builder *SentenceBuilder) Bold(text string) *SentenceBuilder {
return builder.BoldStart().write(text).BoldEnd()
}
// BoldStart marks the start of bold text. Use BoldEnd to end the bold section.
func (builder *SentenceBuilder) BoldStart() *SentenceBuilder {
return builder.write(boldSeq)
}
// BoldEnd clears the bold attribute previously set with BoldStart.
func (builder *SentenceBuilder) BoldEnd() *SentenceBuilder {
return builder.write(boldResetSeq)
}
// Faint adds a string of dimmed text to the sentence.
func (builder *SentenceBuilder) Faint(text string) *SentenceBuilder {
return builder.FaintStart().write(text).FaintEnd()
}
// FaintStart marks the start of dimmed text. Use FaintEnd to end the dimmed section.
func (builder *SentenceBuilder) FaintStart() *SentenceBuilder {
return builder.write(faintSeq)
}
// FaintEnd clears the faint attribute previously set with FaintStart.
func (builder *SentenceBuilder) FaintEnd() *SentenceBuilder {
return builder.write(faintResetSeq)
}
// Italic adds a string of italic text to the sentence.
func (builder *SentenceBuilder) Italic(text string) *SentenceBuilder {
return builder.ItalicStart().write(text).ItalicEnd()
}
// ItalicStart marks the start of italics text. Use ItalicEnd to end the italics section.
func (builder *SentenceBuilder) ItalicStart() *SentenceBuilder {
return builder.write(italicSeq)
}
// ItalicEnd clears the italics attribute previously set with ItalicStart.
func (builder *SentenceBuilder) ItalicEnd() *SentenceBuilder {
return builder.write(italicResetSeq)
}
// Underline adds a string of underlined text to the sentence.
func (builder *SentenceBuilder) Underline(text string) *SentenceBuilder {
return builder.UnderlineStart().write(text).UnderlineEnd()
}
// UnderlineStart marks the start of underlined text. Use UnderlineEnd to end the underlined section.
func (builder *SentenceBuilder) UnderlineStart() *SentenceBuilder {
return builder.write(underlineSeq)
}
// UnderlineEnd clears the underline attribute previously set with UnderlineStart.
func (builder *SentenceBuilder) UnderlineEnd() *SentenceBuilder {
return builder.write(underlineResetSeq)
}
// Blink adds a string of blinking text to the sentence.
func (builder *SentenceBuilder) Blink(text string) *SentenceBuilder {
return builder.BlinkStart().write(text).BlinkEnd()
}
// BlinkStart marks the start of blinking text. Use BlinkEnd to end the blinking section.
func (builder *SentenceBuilder) BlinkStart() *SentenceBuilder {
return builder.write(blinkSeq)
}
// BlinkEnd clears the blink attribute previously set with BlinkStart.
func (builder *SentenceBuilder) BlinkEnd() *SentenceBuilder {
return builder.write(blinkResetSeq)
}
// Invert adds a string of text with inverted colors to the sentence.
func (builder *SentenceBuilder) Invert(text string) *SentenceBuilder {
return builder.InvertStart().write(text).InvertEnd()
}
// InvertStart marks the start of text with inverted colors. Use InvertEnd to end the inverted colors section.
func (builder *SentenceBuilder) InvertStart() *SentenceBuilder {
return builder.write(invertSeq)
}
// InvertEnd clears the invert attribute previously set with InvertStart.
func (builder *SentenceBuilder) InvertEnd() *SentenceBuilder {
return builder.write(invertResetSeq)
}
// Conceal adds a string of concealed text to the sentence.
func (builder *SentenceBuilder) Conceal(text string) *SentenceBuilder {
return builder.ConcealStart().write(text).ConcealEnd()
}
// ConcealStart marks the start of concealed text. Use ConcealEnd to end the concealed section.
func (builder *SentenceBuilder) ConcealStart() *SentenceBuilder {
return builder.write(concealSeq)
}
// ConcealEnd clears the conceal attribute previously set with ConcealStart.
func (builder *SentenceBuilder) ConcealEnd() *SentenceBuilder {
return builder.write(concealResetSeq)
}
// Strikethrough adds a string of crossed text to the sentence.
func (builder *SentenceBuilder) Strikethrough(text string) *SentenceBuilder {
return builder.StrikethroughStart().write(text).StrikethroughEnd()
}
// StrikethroughStart marks the start of crossed text. Use StrikethroughEnd to end the crossed section.
func (builder *SentenceBuilder) StrikethroughStart() *SentenceBuilder {
return builder.write(strikethroughSeq)
}
// StrikethroughEnd clears the strikethrough attribute previously set with StrikethroughStart.
func (builder *SentenceBuilder) StrikethroughEnd() *SentenceBuilder {
return builder.write(strikethroughResetSeq)
}
func (builder *SentenceBuilder) writeColor(color int) *SentenceBuilder {
return builder.writeColorAttribute(fgColorSetSeqOpen, strconv.Itoa(color))
}
func (builder *SentenceBuilder) writeColorRgb(color string) *SentenceBuilder {
return builder.writeColorAttribute(fgColorRgbSetSeqOpen, color)
}
func (builder *SentenceBuilder) writeBackgroundColor(color int) *SentenceBuilder {
return builder.writeColorAttribute(bgColorSetSeqOpen, strconv.Itoa(color))
}
func (builder *SentenceBuilder) writeBackgroundColorRgb(color string) *SentenceBuilder {
return builder.writeColorAttribute(bgColorRgbSetSeqOpen, color)
}
func (builder *SentenceBuilder) writeColorAttribute(openAttribute, color string) *SentenceBuilder {
return builder.write(openAttribute).write(attrDelimiter).write(color).write(endSeq)
}
func unstyle(s string) string {
unstyled := strings.Builder{}
for i := 0; i < len(s); i++ {
if s[i] == escapeSeq[0] {
for ; s[i] != endSeq[0]; i++ {
}
continue
}
unstyled.WriteByte(s[i])
}
return unstyled.String()
}