forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
box_printer.go
371 lines (330 loc) · 13.2 KB
/
box_printer.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
package pterm
import (
"fmt"
"io"
"strings"
"github.com/mattn/go-runewidth"
"github.com/overmindtech/pterm/internal"
)
// BoxPrinter is able to render a box around printables.
type BoxPrinter struct {
Title string
TitleTopLeft bool
TitleTopRight bool
TitleTopCenter bool
TitleBottomLeft bool
TitleBottomRight bool
TitleBottomCenter bool
TextStyle *Style
VerticalString string
BoxStyle *Style
HorizontalString string
TopRightCornerString string
TopLeftCornerString string
BottomLeftCornerString string
BottomRightCornerString string
TopPadding int
BottomPadding int
RightPadding int
LeftPadding int
Writer io.Writer
}
// DefaultBox is the default BoxPrinter.
var DefaultBox = BoxPrinter{
VerticalString: "|",
TopRightCornerString: "└",
TopLeftCornerString: "┘",
BottomLeftCornerString: "┐",
BottomRightCornerString: "┌",
HorizontalString: "─",
BoxStyle: &ThemeDefault.BoxStyle,
TextStyle: &ThemeDefault.BoxTextStyle,
RightPadding: 1,
LeftPadding: 1,
TopPadding: 0,
BottomPadding: 0,
TitleTopLeft: true,
}
// WithTitle returns a new box with a specific Title.
func (p BoxPrinter) WithTitle(str string) *BoxPrinter {
p.Title = str
return &p
}
// WithTitleTopLeft returns a new box with a specific Title alignment.
func (p BoxPrinter) WithTitleTopLeft(b ...bool) *BoxPrinter {
b2 := internal.WithBoolean(b)
p.TitleTopLeft = b2
p.TitleTopRight = false
p.TitleTopCenter = false
p.TitleBottomLeft = false
p.TitleBottomRight = false
p.TitleBottomCenter = false
return &p
}
// WithTitleTopRight returns a new box with a specific Title alignment.
func (p BoxPrinter) WithTitleTopRight(b ...bool) *BoxPrinter {
b2 := internal.WithBoolean(b)
p.TitleTopLeft = false
p.TitleTopRight = b2
p.TitleTopCenter = false
p.TitleBottomLeft = false
p.TitleBottomRight = false
p.TitleBottomCenter = false
return &p
}
// WithTitleTopCenter returns a new box with a specific Title alignment.
func (p BoxPrinter) WithTitleTopCenter(b ...bool) *BoxPrinter {
b2 := internal.WithBoolean(b)
p.TitleTopLeft = false
p.TitleTopRight = false
p.TitleTopCenter = b2
p.TitleBottomLeft = false
p.TitleBottomRight = false
p.TitleBottomCenter = false
return &p
}
// WithTitleBottomLeft returns a new box with a specific Title alignment.
func (p BoxPrinter) WithTitleBottomLeft(b ...bool) *BoxPrinter {
b2 := internal.WithBoolean(b)
p.TitleTopLeft = false
p.TitleTopRight = false
p.TitleTopCenter = false
p.TitleBottomLeft = b2
p.TitleBottomRight = false
p.TitleBottomCenter = false
return &p
}
// WithTitleBottomRight returns a new box with a specific Title alignment.
func (p BoxPrinter) WithTitleBottomRight(b ...bool) *BoxPrinter {
b2 := internal.WithBoolean(b)
p.TitleTopLeft = false
p.TitleTopRight = false
p.TitleTopCenter = false
p.TitleBottomLeft = false
p.TitleBottomRight = b2
p.TitleBottomCenter = false
return &p
}
// WithTitleBottomCenter returns a new box with a specific Title alignment.
func (p BoxPrinter) WithTitleBottomCenter(b ...bool) *BoxPrinter {
b2 := internal.WithBoolean(b)
p.TitleTopLeft = false
p.TitleTopRight = false
p.TitleTopCenter = false
p.TitleBottomLeft = false
p.TitleBottomRight = false
p.TitleBottomCenter = b2
return &p
}
// WithBoxStyle returns a new box with a specific box Style.
func (p BoxPrinter) WithBoxStyle(style *Style) *BoxPrinter {
p.BoxStyle = style
return &p
}
// WithTextStyle returns a new box with a specific text Style.
func (p BoxPrinter) WithTextStyle(style *Style) *BoxPrinter {
p.TextStyle = style
return &p
}
// WithTopRightCornerString returns a new box with a specific TopRightCornerString.
func (p BoxPrinter) WithTopRightCornerString(str string) *BoxPrinter {
p.TopRightCornerString = str
return &p
}
// WithTopLeftCornerString returns a new box with a specific TopLeftCornerString.
func (p BoxPrinter) WithTopLeftCornerString(str string) *BoxPrinter {
p.TopLeftCornerString = str
return &p
}
// WithBottomRightCornerString returns a new box with a specific BottomRightCornerString.
func (p BoxPrinter) WithBottomRightCornerString(str string) *BoxPrinter {
p.BottomRightCornerString = str
return &p
}
// WithBottomLeftCornerString returns a new box with a specific BottomLeftCornerString.
func (p BoxPrinter) WithBottomLeftCornerString(str string) *BoxPrinter {
p.BottomLeftCornerString = str
return &p
}
// WithVerticalString returns a new box with a specific VerticalString.
func (p BoxPrinter) WithVerticalString(str string) *BoxPrinter {
p.VerticalString = str
return &p
}
// WithHorizontalString returns a new box with a specific HorizontalString.
func (p BoxPrinter) WithHorizontalString(str string) *BoxPrinter {
p.HorizontalString = str
return &p
}
// WithTopPadding returns a new box with a specific TopPadding.
func (p BoxPrinter) WithTopPadding(padding int) *BoxPrinter {
if padding < 0 {
padding = 0
}
p.TopPadding = padding
return &p
}
// WithBottomPadding returns a new box with a specific BottomPadding.
func (p BoxPrinter) WithBottomPadding(padding int) *BoxPrinter {
if padding < 0 {
padding = 0
}
p.BottomPadding = padding
return &p
}
// WithRightPadding returns a new box with a specific RightPadding.
func (p BoxPrinter) WithRightPadding(padding int) *BoxPrinter {
if padding < 0 {
padding = 0
}
p.RightPadding = padding
return &p
}
// WithLeftPadding returns a new box with a specific LeftPadding.
func (p BoxPrinter) WithLeftPadding(padding int) *BoxPrinter {
if padding < 0 {
padding = 0
}
p.LeftPadding = padding
return &p
}
// WithWriter sets the custom Writer.
func (p BoxPrinter) WithWriter(writer io.Writer) *BoxPrinter {
p.Writer = writer
return &p
}
// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func (p BoxPrinter) Sprint(a ...interface{}) string {
if p.BoxStyle == nil {
p.BoxStyle = &ThemeDefault.BoxStyle
}
if p.TextStyle == nil {
p.TextStyle = &ThemeDefault.BoxTextStyle
}
maxWidth := internal.GetStringMaxWidth(Sprint(a...))
var topLine string
var bottomLine string
if p.Title == "" {
topLine = p.BoxStyle.Sprint(p.BottomRightCornerString) + strings.Repeat(p.BoxStyle.Sprint(p.HorizontalString),
maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.BottomLeftCornerString)
bottomLine = p.BoxStyle.Sprint(p.TopRightCornerString) + strings.Repeat(p.BoxStyle.Sprint(p.HorizontalString),
maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.TopLeftCornerString)
} else {
p.Title = strings.ReplaceAll(p.Title, "\n", " ")
if (maxWidth + p.RightPadding + p.LeftPadding - 4) < internal.GetStringMaxWidth(p.Title) {
p.RightPadding = internal.GetStringMaxWidth(p.Title) - (maxWidth + p.RightPadding + p.LeftPadding - 5)
}
if p.TitleTopLeft {
topLine = p.BoxStyle.Sprint(p.BottomRightCornerString) + internal.AddTitleToLine(p.Title, p.BoxStyle.Sprint(p.HorizontalString), maxWidth+p.LeftPadding+p.RightPadding, true) + p.BoxStyle.Sprint(p.BottomLeftCornerString)
bottomLine = p.BoxStyle.Sprint(p.TopRightCornerString) + strings.Repeat(p.BoxStyle.Sprint(p.HorizontalString),
maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.TopLeftCornerString)
} else if p.TitleTopRight {
topLine = p.BoxStyle.Sprint(p.BottomRightCornerString) + internal.AddTitleToLine(p.Title, p.BoxStyle.Sprint(p.HorizontalString), maxWidth+p.LeftPadding+p.RightPadding, false) + p.BoxStyle.Sprint(p.BottomLeftCornerString)
bottomLine = p.BoxStyle.Sprint(p.TopRightCornerString) + strings.Repeat(p.BoxStyle.Sprint(p.HorizontalString),
maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.TopLeftCornerString)
} else if p.TitleTopCenter {
topLine = p.BoxStyle.Sprint(p.BottomRightCornerString) + internal.AddTitleToLineCenter(p.Title, p.BoxStyle.Sprint(p.HorizontalString), maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.BottomLeftCornerString)
bottomLine = p.BoxStyle.Sprint(p.TopRightCornerString) + strings.Repeat(p.BoxStyle.Sprint(p.HorizontalString),
maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.TopLeftCornerString)
} else if p.TitleBottomLeft {
topLine = p.BoxStyle.Sprint(p.BottomRightCornerString) + strings.Repeat(p.BoxStyle.Sprint(p.HorizontalString),
maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.BottomLeftCornerString)
bottomLine = p.BoxStyle.Sprint(p.TopRightCornerString) + internal.AddTitleToLine(p.Title, p.BoxStyle.Sprint(p.HorizontalString), maxWidth+p.LeftPadding+p.RightPadding, true) + p.BoxStyle.Sprint(p.TopLeftCornerString)
} else if p.TitleBottomRight {
topLine = p.BoxStyle.Sprint(p.BottomRightCornerString) + strings.Repeat(p.BoxStyle.Sprint(p.HorizontalString),
maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.BottomLeftCornerString)
bottomLine = p.BoxStyle.Sprint(p.TopRightCornerString) + internal.AddTitleToLine(p.Title, p.BoxStyle.Sprint(p.HorizontalString), maxWidth+p.LeftPadding+p.RightPadding, false) + p.BoxStyle.Sprint(p.TopLeftCornerString)
} else if p.TitleBottomCenter {
topLine = p.BoxStyle.Sprint(p.BottomRightCornerString) + strings.Repeat(p.BoxStyle.Sprint(p.HorizontalString),
maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.BottomLeftCornerString)
bottomLine = p.BoxStyle.Sprint(p.TopRightCornerString) + internal.AddTitleToLineCenter(p.Title, p.BoxStyle.Sprint(p.HorizontalString), maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.TopLeftCornerString)
}
}
boxString := strings.Repeat("\n", p.TopPadding) + Sprint(a...) + strings.Repeat("\n", p.BottomPadding)
ss := strings.Split(boxString, "\n")
for i, s2 := range ss {
if runewidth.StringWidth(RemoveColorFromString(s2)) < maxWidth {
ss[i] = p.BoxStyle.Sprint(p.VerticalString) + strings.Repeat(" ", p.LeftPadding) + p.TextStyle.Sprint(s2) +
strings.Repeat(" ", maxWidth-runewidth.StringWidth(RemoveColorFromString(s2))+p.RightPadding) +
p.BoxStyle.Sprint(p.VerticalString)
} else {
ss[i] = p.BoxStyle.Sprint(p.VerticalString) + strings.Repeat(" ", p.LeftPadding) + p.TextStyle.Sprint(s2) +
strings.Repeat(" ", p.RightPadding) + p.BoxStyle.Sprint(p.VerticalString)
}
}
return topLine + "\n" + strings.Join(ss, "\n") + "\n" + bottomLine
}
// Sprintln formats using the default formats for its operands and returns the resulting string.
// Spaces are always added between operands and a newline is appended.
func (p BoxPrinter) Sprintln(a ...interface{}) string {
return p.Sprint(strings.TrimSuffix(Sprintln(a...), "\n")) + "\n"
}
// Sprintf formats according to a format specifier and returns the resulting string.
func (p BoxPrinter) Sprintf(format string, a ...interface{}) string {
return p.Sprint(Sprintf(format, a...))
}
// Sprintfln formats according to a format specifier and returns the resulting string.
// Spaces are always added between operands and a newline is appended.
func (p BoxPrinter) Sprintfln(format string, a ...interface{}) string {
return p.Sprintf(format, a...) + "\n"
}
// Print formats using the default formats for its operands and writes to standard output.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func (p BoxPrinter) Print(a ...interface{}) *TextPrinter {
Fprint(p.Writer, p.Sprint(a...))
tp := TextPrinter(p)
return &tp
}
// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func (p BoxPrinter) Println(a ...interface{}) *TextPrinter {
Fprint(p.Writer, p.Sprintln(a...))
tp := TextPrinter(p)
return &tp
}
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func (p BoxPrinter) Printf(format string, a ...interface{}) *TextPrinter {
Fprint(p.Writer, p.Sprintf(format, a...))
tp := TextPrinter(p)
return &tp
}
// Printfln formats according to a format specifier and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func (p BoxPrinter) Printfln(format string, a ...interface{}) *TextPrinter {
Fprint(p.Writer, p.Sprintfln(format, a...))
tp := TextPrinter(p)
return &tp
}
// PrintOnError prints every error which is not nil.
// If every error is nil, nothing will be printed.
// This can be used for simple error checking.
func (p BoxPrinter) PrintOnError(a ...interface{}) *TextPrinter {
for _, arg := range a {
if err, ok := arg.(error); ok {
if err != nil {
p.Println(err)
}
}
}
tp := TextPrinter(p)
return &tp
}
// PrintOnErrorf wraps every error which is not nil and prints it.
// If every error is nil, nothing will be printed.
// This can be used for simple error checking.
func (p BoxPrinter) PrintOnErrorf(format string, a ...interface{}) *TextPrinter {
for _, arg := range a {
if err, ok := arg.(error); ok {
if err != nil {
p.Println(fmt.Errorf(format, err))
}
}
}
tp := TextPrinter(p)
return &tp
}