Skip to content

Commit f21f4e3

Browse files
committed
Experimental Style funcs for progress
1 parent c802c02 commit f21f4e3

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

cmd/demo-progress/demo.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"flag"
55
"fmt"
66
"math/rand"
7+
"strings"
78
"time"
89

910
"github.com/jedib0t/go-pretty/v6/progress"
@@ -145,6 +146,34 @@ func main() {
145146
pw.Style().Visibility.Value = !*flagHideValue
146147
pw.Style().Visibility.Pinned = *flagShowPinned
147148

149+
colors := []text.Colors{
150+
{text.FgRed},
151+
{text.FgGreen},
152+
{text.FgYellow},
153+
{text.FgBlue},
154+
{text.FgMagenta},
155+
{text.FgCyan},
156+
}
157+
158+
pw.Style().CustomFuncs.TrackerDeterminate = func(value int64, total int64, maxLen int) string {
159+
v := float64(value) / float64(total)
160+
b := &strings.Builder{}
161+
fmt.Fprintf(b, "[")
162+
inner := maxLen - 2
163+
for i := 0; i < inner; i++ {
164+
delta := float64(i) / float64(inner)
165+
colorIdx := delta * float64(len(colors))
166+
color := colors[int(colorIdx)%len(colors)]
167+
if delta < v {
168+
fmt.Fprintf(b, "%s", color.Sprint("█"))
169+
} else {
170+
fmt.Fprintf(b, " ")
171+
}
172+
}
173+
fmt.Fprintf(b, "]")
174+
return b.String()
175+
}
176+
148177
// call Render() in async mode; yes we don't have any trackers at the moment
149178
go pw.Render()
150179

progress/render.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ func (p *Progress) generateTrackerStr(t *Tracker, maxLen int, hint renderHint) s
107107
// generateTrackerStrDeterminate generates the tracker string for the case where
108108
// the Total value is known, and the progress percentage can be calculated.
109109
func (p *Progress) generateTrackerStrDeterminate(value int64, total int64, maxLen int) string {
110+
if p.style.CustomFuncs.TrackerDeterminate != nil {
111+
return p.style.CustomFuncs.TrackerDeterminate(value, total, maxLen)
112+
}
113+
110114
pFinishedDots, pFinishedDotsFraction := 0.0, 0.0
111115
pDotValue := float64(total) / float64(maxLen)
112116
if pDotValue > 0 {
@@ -133,13 +137,13 @@ func (p *Progress) generateTrackerStrDeterminate(value int64, total int64, maxLe
133137
if pFinishedStrLen < maxLen {
134138
pUnfinished = strings.Repeat(p.style.Chars.Unfinished, maxLen-pFinishedStrLen)
135139
}
136-
140+
137141
return p.style.Colors.Tracker.Sprintf("%s%s%s%s%s",
138142
p.style.Chars.BoxLeft, pFinished, pInProgress, pUnfinished, p.style.Chars.BoxRight,
139143
)
140144
}
141145

142-
// generateTrackerStrDeterminate generates the tracker string for the case where
146+
// generateTrackerStrIndeterminate generates the tracker string for the case where
143147
// the Total value is unknown, and the progress percentage cannot be calculated.
144148
func (p *Progress) generateTrackerStrIndeterminate(maxLen int) string {
145149
indicator := p.style.Chars.Indeterminate(maxLen)

progress/style.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88

99
// Style declares how to render the Progress/Trackers.
1010
type Style struct {
11-
Name string // name of the Style
12-
Chars StyleChars // characters to use on the progress bar
13-
Colors StyleColors // colors to use on the progress bar
14-
Options StyleOptions // misc. options for the progress bar
15-
Visibility StyleVisibility // show/hide components of the progress bar(s)
11+
Name string // name of the Style
12+
Chars StyleChars // characters to use on the progress bar
13+
Colors StyleColors // colors to use on the progress bar
14+
Options StyleOptions // misc. options for the progress bar
15+
Visibility StyleVisibility // show/hide components of the progress bar(s)
16+
CustomFuncs StyleFunctions // custom render functions for the progress bar
1617
}
1718

1819
var (
@@ -215,3 +216,7 @@ var StyleVisibilityDefault = StyleVisibility{
215216
TrackerOverall: false,
216217
Value: true,
217218
}
219+
220+
type StyleFunctions struct {
221+
TrackerDeterminate func(value int64, total int64, maxLen int) string
222+
}

0 commit comments

Comments
 (0)