Skip to content

Commit 3c86af8

Browse files
authored
progress demo tweaks (#371)
1 parent 2ac3ff0 commit 3c86af8

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

cmd/demo-progress/demo.go

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
var (
1515
flagAutoStop = flag.Bool("auto-stop", false, "Auto-stop rendering?")
16+
flagCustomRenderer = flag.Bool("custom-renderer", false, "Use custom render functions with rainbow colors")
1617
flagHideETA = flag.Bool("hide-eta", false, "Hide the ETA?")
1718
flagHideETAOverall = flag.Bool("hide-eta-overall", false, "Hide the ETA in the overall tracker?")
1819
flagHideOverallTracker = flag.Bool("hide-overall", false, "Hide the Overall Tracker?")
@@ -27,7 +28,6 @@ var (
2728
flagRandomDefer = flag.Bool("rnd-defer", false, "Introduce random deferred starts")
2829
flagRandomRemove = flag.Bool("rnd-remove", false, "Introduce random remove of trackers on completion")
2930
flagRandomLogs = flag.Bool("rnd-logs", false, "Output random logs in the middle of tracking")
30-
flagCustomRender = flag.Bool("custom-render", false, "Use custom render functions with rainbow colors")
3131

3232
messageColors = []text.Color{
3333
text.FgRed,
@@ -42,6 +42,51 @@ var (
4242
timeStart = time.Now()
4343
)
4444

45+
// customTrackerDeterminateRenderer creates a progress bar using rainbow colors for determinate progress
46+
func customTrackerDeterminateRenderer(value int64, total int64, maxLen int) string {
47+
progress := float64(value) / float64(total)
48+
completed := int(progress * float64(maxLen))
49+
50+
var result strings.Builder
51+
for i := 0; i < maxLen; i++ {
52+
if i < completed {
53+
// Use rainbow colors based on position in the progress bar
54+
colorIdx := (i * 6) / maxLen // Map position to 6 rainbow colors
55+
colors := []text.Color{
56+
text.FgRed,
57+
text.FgYellow,
58+
text.FgGreen,
59+
text.FgCyan,
60+
text.FgBlue,
61+
text.FgMagenta,
62+
}
63+
if colorIdx >= len(colors) {
64+
colorIdx = len(colors) - 1
65+
}
66+
result.WriteString(colors[colorIdx].Sprint("█"))
67+
} else {
68+
result.WriteString(text.FgHiBlack.Sprint("░"))
69+
}
70+
}
71+
72+
return result.String()
73+
}
74+
75+
// customTrackerIndeterminateRenderer creates a progress bar using rotating rainbow colors for indeterminate progress
76+
func customTrackerIndeterminateRenderer(maxLen int) string {
77+
// For indeterminate progress, use rotating rainbow colors
78+
colors := []text.Color{
79+
text.FgRed,
80+
text.FgYellow,
81+
text.FgGreen,
82+
text.FgCyan,
83+
text.FgBlue,
84+
text.FgMagenta,
85+
}
86+
idx := int(time.Now().UnixNano()/100000000) % len(colors)
87+
return colors[idx].Sprint(strings.Repeat("█", maxLen))
88+
}
89+
4590
func getMessage(idx int64, units *progress.Units) string {
4691
var message string
4792
switch units {
@@ -121,51 +166,6 @@ func trackSomething(pw progress.Writer, idx int64, updateMessage bool) {
121166
}
122167
}
123168

124-
// customTrackerRender creates a progress bar using rainbow colors for determinate progress
125-
func customTrackerRender(value int64, total int64, maxLen int) string {
126-
progress := float64(value) / float64(total)
127-
completed := int(progress * float64(maxLen))
128-
129-
var result strings.Builder
130-
for i := 0; i < maxLen; i++ {
131-
if i < completed {
132-
// Use rainbow colors based on position in the progress bar
133-
colorIdx := (i * 6) / maxLen // Map position to 6 rainbow colors
134-
colors := []text.Color{
135-
text.FgRed,
136-
text.FgYellow,
137-
text.FgGreen,
138-
text.FgCyan,
139-
text.FgBlue,
140-
text.FgMagenta,
141-
}
142-
if colorIdx >= len(colors) {
143-
colorIdx = len(colors) - 1
144-
}
145-
result.WriteString(colors[colorIdx].Sprint("█"))
146-
} else {
147-
result.WriteString(text.FgHiBlack.Sprint("░"))
148-
}
149-
}
150-
151-
return result.String()
152-
}
153-
154-
// customTrackerIndeterminateRender creates a progress bar using rotating rainbow colors for indeterminate progress
155-
func customTrackerIndeterminateRender(maxLen int) string {
156-
// For indeterminate progress, use rotating rainbow colors
157-
colors := []text.Color{
158-
text.FgRed,
159-
text.FgYellow,
160-
text.FgGreen,
161-
text.FgCyan,
162-
text.FgBlue,
163-
text.FgMagenta,
164-
}
165-
idx := int(time.Now().UnixNano()/100000000) % len(colors)
166-
return colors[idx].Sprint(strings.Repeat("█", maxLen))
167-
}
168-
169169
func main() {
170170
flag.Parse()
171171
fmt.Printf("Tracking Progress of %d trackers ...\n\n", *flagNumTrackers)
@@ -193,10 +193,9 @@ func main() {
193193
pw.Style().Visibility.Pinned = *flagShowPinned
194194

195195
// set up custom render functions if flag is enabled
196-
if *flagCustomRender {
197-
pw.Style().Renderer.TrackerDeterminate = customTrackerRender
198-
pw.Style().Renderer.TrackerIndeterminate = customTrackerIndeterminateRender
199-
fmt.Println("Using custom render functions with rainbow colors!")
196+
if *flagCustomRenderer {
197+
pw.Style().Renderer.TrackerDeterminate = customTrackerDeterminateRenderer
198+
pw.Style().Renderer.TrackerIndeterminate = customTrackerIndeterminateRenderer
200199
}
201200

202201
// call Render() in async mode; yes we don't have any trackers at the moment

0 commit comments

Comments
 (0)