From f7bddd0985e47e35e8bc6f2e94fc7d28fb0a196b Mon Sep 17 00:00:00 2001 From: CrushedPixel Date: Fri, 8 Jun 2018 16:01:18 +0100 Subject: [PATCH] Added Description and SaucerHead feature in a backwards-compatible manner --- progressbar.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/progressbar.go b/progressbar.go index 40f870d..b6337fd 100644 --- a/progressbar.go +++ b/progressbar.go @@ -35,11 +35,13 @@ type config struct { writer io.Writer theme Theme renderWithBlankState bool + description string } // Theme defines the elements of the bar type Theme struct { Saucer string + SaucerHead string SaucerPadding string BarStart string BarEnd string @@ -76,6 +78,13 @@ func OptionSetRenderBlankState(r bool) Option { } } +// OptionSetDescription sets the description of the bar to render in front of it +func OptionSetDescription(description string) Option { + return func(p *ProgressBar) { + p.config.description = description + } +} + var defaultTheme = Theme{Saucer: "█", SaucerPadding: " ", BarStart: "|", BarEnd: "|"} // NewOptions constructs a new instance of ProgressBar, with any options you specify @@ -162,10 +171,23 @@ func renderProgressBar(c config, s state) error { leftTime = time.Since(s.startTime).Seconds() / float64(s.currentNum) * (float64(c.max) - float64(s.currentNum)) } - str := fmt.Sprintf("\r%4d%% %s%s%s%s [%s:%s] ", + var saucer string + if s.currentSaucerSize > 0 { + saucer = strings.Repeat(c.theme.Saucer, s.currentSaucerSize-1) + saucerHead := c.theme.SaucerHead + if saucerHead == "" || s.currentSaucerSize == c.width { + // use the saucer for the saucer head if it hasn't been set + // to preserve backwards compatibility + saucerHead = c.theme.Saucer + } + saucer += saucerHead + } + + str := fmt.Sprintf("\r%s%4d%% %s%s%s%s [%s:%s] ", + c.description, s.currentPercent, c.theme.BarStart, - strings.Repeat(c.theme.Saucer, s.currentSaucerSize), + saucer, strings.Repeat(c.theme.SaucerPadding, c.width-s.currentSaucerSize), c.theme.BarEnd, (time.Duration(time.Since(s.startTime).Seconds()) * time.Second).String(),