-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressbar.go
More file actions
118 lines (102 loc) · 3.2 KB
/
Copy pathprogressbar.go
File metadata and controls
118 lines (102 loc) · 3.2 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
package progressbar
import (
"fmt"
"strings"
)
const (
ColorBlack = "\033[30m"
ColorRed = "\033[31m"
ColorGreen = "\033[32m"
ColorYellow = "\033[33m"
ColorBlue = "\033[34m"
ColorMagenta = "\033[35m"
ColorCyan = "\033[36m"
ColorWhite = "\033[37m"
ColorBrightBlack = "\033[90m"
ColorBrightRed = "\033[91m"
ColorBrightGreen = "\033[92m"
ColorBrightYellow = "\033[93m"
ColorBrightBlue = "\033[94m"
ColorBrightMagenta = "\033[95m"
ColorBrightCyan = "\033[96m"
ColorBrightWhite = "\033[97m"
colorReset = "\033[0m"
)
// Spinners built-in spinners for progress bar
var Spinners = map[uint8][]string{
0: {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
1: {"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"},
2: {"▁", "▂", "▃", "▄", "▅", "▆", "▇"},
3: {"*--", "-*-", "--*"},
4: {". ", ".. ", "...", " ..", " ."},
5: {"* ", "** ", "***", " **", " *"},
6: {"* ", " * ", " *"},
}
// Fillers built-in fillers for progress bar
var Fillers = map[uint8][2]string{
0: {"█", "░"},
1: {"|", " "},
2: {"#", " "},
}
// baseConfig config containing settings for visual elements of the progress bar
var baseConfig = progressBarConfig{
spinner: Spinners[0],
edges: [2]string{"[", "]"},
colors: [2]string{"", ColorGreen},
fillers: Fillers[0],
withPercent: true,
withSpinner: true,
}
// GetNewProgressBar get the progress bar object
func GetNewProgressBar() ProgressBar {
return &progressBar{
barLen: 50,
percent: 0,
config: baseConfig,
spinnerState: 0,
spinnerLen: len(baseConfig.spinner),
}
}
// progressBar returns the progress bar string with the current parameters
func (p *progressBar) render() string {
occupancy := int((float32(p.barLen) / 100) * float32(p.percent))
fill := p.barLen - occupancy
startPart := fmt.Sprintf("%s%s%s", p.config.colors[0], p.config.edges[0], p.config.colors[1])
filledPart := p.config.fillers[0]
emptyPart := p.config.fillers[1]
endPart := fmt.Sprintf("%s%s%s", colorReset, p.config.colors[0], p.config.edges[1])
var spinnerPart string
if p.config.withSpinner {
spinnerPart = fmt.Sprintf("%s%s ", p.config.colors[0], p.config.spinner[p.spinnerState])
// 0 1 2 3 4 5 6 ... bytes
// ^-------^ ^
// color spinner
// if color != "", else:
// 0 1 2 3 4 5 ... bytes
// ^
// spinner
}
var percentPart string
if p.config.withPercent {
percentPart = fmt.Sprintf(" %d%%", p.percent)
}
// Calculate how many bytes are required for the final string
bytesToWrite := len(startPart) + len(endPart) + // Progress bar edges
len(spinnerPart) + len(percentPart) + // Optional parts
(len(filledPart) * occupancy) + (len(emptyPart) * fill) + // Fillers
len(colorReset) // Reset color
var sb strings.Builder
sb.Grow(bytesToWrite) // Allocate the required memory for the string
if p.config.withSpinner {
sb.WriteString(spinnerPart)
}
sb.WriteString(startPart)
sb.WriteString(strings.Repeat(filledPart, occupancy))
sb.WriteString(strings.Repeat(emptyPart, fill))
sb.WriteString(endPart)
if p.config.withPercent {
sb.WriteString(percentPart)
}
sb.WriteString(colorReset)
return sb.String()
}