forked from alperdrsnn/clime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanner.go
More file actions
440 lines (371 loc) · 9.73 KB
/
banner.go
File metadata and controls
440 lines (371 loc) · 9.73 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package clime
import (
"fmt"
"strings"
)
type BannerStyle struct {
TopLeft string
TopRight string
BottomLeft string
BottomRight string
Horizontal string
Vertical string
Padding int
}
var (
BannerStyleDefault = BannerStyle{
TopLeft: "┌",
TopRight: "┐",
BottomLeft: "└",
BottomRight: "┘",
Horizontal: "─",
Vertical: "│",
Padding: 1,
}
BannerStyleRounded = BannerStyle{
TopLeft: "╭",
TopRight: "╮",
BottomLeft: "╰",
BottomRight: "╯",
Horizontal: "─",
Vertical: "│",
Padding: 1,
}
BannerStyleBold = BannerStyle{
TopLeft: "┏",
TopRight: "┓",
BottomLeft: "┗",
BottomRight: "┛",
Horizontal: "━",
Vertical: "┃",
Padding: 1,
}
BannerStyleDouble = BannerStyle{
TopLeft: "╔",
TopRight: "╗",
BottomLeft: "╚",
BottomRight: "╝",
Horizontal: "═",
Vertical: "║",
Padding: 1,
}
BannerStyleSimple = BannerStyle{
TopLeft: "+",
TopRight: "+",
BottomLeft: "+",
BottomRight: "+",
Horizontal: "-",
Vertical: "|",
Padding: 1,
}
)
type BannerType int
const (
BannerSuccess BannerType = iota
BannerWarning
BannerError
BannerInfo
)
type Banner struct {
message string
bannerType BannerType
style BannerStyle
color *Color
borderColor *Color
width int
multiline bool
ResponsiveConfig *ResponsiveConfig
useSmartSizing bool
}
// NewBanner creates a new banner
func NewBanner(message string, bannerType BannerType) *Banner {
banner := &Banner{
message: message,
bannerType: bannerType,
style: BannerStyleDefault,
width: SmartWidth(0.9), // Use 90% of smart width
multiline: true,
useSmartSizing: true,
}
switch bannerType {
case BannerSuccess:
banner.color = Success
banner.borderColor = Success
case BannerWarning:
banner.color = Warning
banner.borderColor = Warning
case BannerError:
banner.color = Error
banner.borderColor = Error
case BannerInfo:
banner.color = Info
banner.borderColor = Info
}
return banner
}
// WithStyle sets the banner style
func (b *Banner) WithStyle(style BannerStyle) *Banner {
b.style = style
return b
}
// WithColor sets the text color
func (b *Banner) WithColor(color *Color) *Banner {
b.color = color
return b
}
// WithBorderColor sets the border color
func (b *Banner) WithBorderColor(color *Color) *Banner {
b.borderColor = color
return b
}
// WithWidth sets the banner width
func (b *Banner) WithWidth(width int) *Banner {
if width > 0 {
b.width = width
b.useSmartSizing = false
}
return b
}
// WithSmartWidth enables smart responsive width sizing
func (b *Banner) WithSmartWidth(percentage float64) *Banner {
b.width = SmartWidth(percentage)
b.useSmartSizing = true
return b
}
// WithResponsiveConfig sets responsive configuration for different breakpoints
func (b *Banner) WithResponsiveConfig(config ResponsiveConfig) *Banner {
b.ResponsiveConfig = &config
b.useSmartSizing = true
return b
}
// Multiline controls whether to use multiline layout for long messages
func (b *Banner) Multiline(enable bool) *Banner {
b.multiline = enable
return b
}
// Render renders the banner and returns the string representation
func (b *Banner) Render() string {
if b.message == "" {
return ""
}
if b.useSmartSizing {
rm := GetResponsiveManager()
rm.RefreshBreakpoint()
b.calculateResponsiveSize()
}
b.calculateOptimalWidth()
var result strings.Builder
result.WriteString(b.renderTopBorder())
result.WriteString("\n")
lines := b.prepareLines()
for _, line := range lines {
result.WriteString(b.renderContentLine(line))
result.WriteString("\n")
}
result.WriteString(b.renderBottomBorder())
return result.String()
}
// Print renders and prints the banner
func (b *Banner) Print() {
fmt.Print(b.Render())
}
// Println renders and prints the banner with a newline
func (b *Banner) Println() {
fmt.Println(b.Render())
}
// prepareLines prepares the message lines for rendering
func (b *Banner) prepareLines() []string {
if b.message == "" {
return []string{}
}
// Calculate available width for content
availableWidth := b.width - (2 * b.style.Padding) - 2 // 2 for borders
if availableWidth <= 0 {
availableWidth = 10
}
var lines []string
if b.multiline {
words := strings.Fields(b.message)
var currentLine strings.Builder
for _, word := range words {
if currentLine.Len() == 0 {
currentLine.WriteString(word)
} else if getVisualWidth(currentLine.String())+1+getVisualWidth(word) <= availableWidth {
currentLine.WriteString(" " + word)
} else {
lines = append(lines, currentLine.String())
currentLine.Reset()
currentLine.WriteString(word)
}
}
if currentLine.Len() > 0 {
lines = append(lines, currentLine.String())
}
} else {
if getVisualWidth(b.message) > availableWidth {
lines = append(lines, TruncateString(b.message, availableWidth))
} else {
lines = append(lines, b.message)
}
}
return lines
}
// calculateResponsiveSize calculates responsive banner size
func (b *Banner) calculateResponsiveSize() {
if b.ResponsiveConfig != nil {
rm := GetResponsiveManager()
config := b.ResponsiveConfig.GetConfigForBreakpoint(rm.GetCurrentBreakpoint())
if config != nil {
if config.Width != nil {
b.width = *config.Width
}
if config.Compact {
b.multiline = false
}
return
}
}
if b.useSmartSizing {
b.width = SmartWidth(0.9)
}
}
// renderTopBorder renders the top border
func (b *Banner) renderTopBorder() string {
borderWidth := b.width - 2
border := b.style.TopLeft + strings.Repeat(b.style.Horizontal, borderWidth) + b.style.TopRight
if b.borderColor != nil {
return b.borderColor.Sprint(border)
}
return border
}
// renderBottomBorder renders the bottom border
func (b *Banner) renderBottomBorder() string {
borderWidth := b.width - 2
border := b.style.BottomLeft + strings.Repeat(b.style.Horizontal, borderWidth) + b.style.BottomRight
if b.borderColor != nil {
return b.borderColor.Sprint(border)
}
return border
}
// calculateOptimalWidth calculates the optimal banner width
func (b *Banner) calculateOptimalWidth() {
lines := b.prepareLines()
maxLineLength := b.getMaxLineLength(lines)
requiredWidth := maxLineLength + (2 * b.style.Padding) + 2
if requiredWidth > b.width {
b.width = requiredWidth
}
}
// renderContentLine renders a single line of content with padding and border
func (b *Banner) renderContentLine(line string) string {
availableWidth := b.width - 2
var content strings.Builder
if b.borderColor != nil {
content.WriteString(b.borderColor.Sprint(b.style.Vertical))
} else {
content.WriteString(b.style.Vertical)
}
content.WriteString(strings.Repeat(" ", b.style.Padding))
if b.color != nil {
content.WriteString(b.color.Sprint(line))
} else {
content.WriteString(line)
}
usedWidth := (2 * b.style.Padding) + getVisualWidth(line)
remainingSpace := availableWidth - usedWidth
if remainingSpace > 0 {
content.WriteString(strings.Repeat(" ", remainingSpace))
}
content.WriteString(strings.Repeat(" ", b.style.Padding))
if b.borderColor != nil {
content.WriteString(b.borderColor.Sprint(b.style.Vertical))
} else {
content.WriteString(b.style.Vertical)
}
return content.String()
}
// getMaxLineLength gets the maximum length among all lines
func (b *Banner) getMaxLineLength(lines []string) int {
maxLength := 0
for _, line := range lines {
if getVisualWidth(line) > maxLength {
maxLength = getVisualWidth(line)
}
}
return maxLength
}
// SuccessBanner creates and displays a success banner
func SuccessBanner(message string) {
NewBanner(message, BannerSuccess).Println()
}
// WarningBanner creates and displays a warning banner
func WarningBanner(message string) {
NewBanner(message, BannerWarning).Println()
}
// ErrorBanner creates and displays an error banner
func ErrorBanner(message string) {
NewBanner(message, BannerError).Println()
}
// InfoBanner creates and displays an info banner
func InfoBanner(message string) {
NewBanner(message, BannerInfo).Println()
}
// SuccessLine prints a simple success message with icon
func SuccessLine(message string) {
fmt.Println(Success.Sprint("✓ " + message))
}
// WarningLine prints a simple warning message with icon
func WarningLine(message string) {
fmt.Println(Warning.Sprint("⚠ " + message))
}
// ErrorLine prints a simple error message with icon
func ErrorLine(message string) {
fmt.Println(Error.Sprint("✗ " + message))
}
// InfoLine prints a simple info message with icon
func InfoLine(message string) {
fmt.Println(Info.Sprint("ℹ " + message))
}
// CustomBanner creates a custom banner with specific colors and style
func CustomBanner(message string, textColor, borderColor *Color, style BannerStyle) *Banner {
banner := &Banner{
message: message,
bannerType: BannerInfo,
style: style,
color: textColor,
borderColor: borderColor,
width: NewTerminal().Width() - 4,
multiline: true,
}
return banner
}
// Header creates a header-style banner
func Header(title string) {
terminal := NewTerminal()
width := terminal.Width()
if width > 80 {
width = 80
}
padding := (width - getVisualWidth(title) - 4) / 2
if padding < 0 {
padding = 0
}
header := strings.Repeat("=", width)
titleLine := "=" + strings.Repeat(" ", padding) + title + strings.Repeat(" ", padding)
for getVisualWidth(titleLine) < width-1 {
titleLine += " "
}
titleLine += "="
fmt.Println(BoldColor.Sprint(header))
fmt.Println(BoldColor.Sprint(titleLine))
fmt.Println(BoldColor.Sprint(header))
}
// Separator prints a simple separator line
func Separator() {
terminal := NewTerminal()
width := terminal.Width()
if width > 80 {
width = 80
}
fmt.Println(DimColor.Sprint(strings.Repeat("─", width)))
}