Skip to content

Commit

Permalink
Merge pull request #15 from CrushedPixel/master
Browse files Browse the repository at this point in the history
Added Description and SaucerHead features
  • Loading branch information
schollz authored Jun 9, 2018
2 parents 85ec7a0 + f7bddd0 commit 7b9a21f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 7b9a21f

Please sign in to comment.