diff --git a/examples/pacman/main.go b/examples/pacman/main.go new file mode 100644 index 0000000..7aece4c --- /dev/null +++ b/examples/pacman/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "time" + + "github.com/k0kubun/go-ansi" + "github.com/schollz/progressbar/v3" +) + +func main() { + doneCh := make(chan struct{}) + + bar := progressbar.NewOptions(1000, + progressbar.OptionSetWriter(ansi.NewAnsiStdout()), + progressbar.OptionEnableColorCodes(true), + progressbar.OptionSetWidth(50), + progressbar.OptionSetTheme(progressbar.Theme{ + Saucer: " ", + AltSaucerHead: "[yellow]<[reset]", + SaucerHead: "[yellow]-[reset]", + SaucerPadding: "[white]•", + BarStart: "[blue]|[reset]", + BarEnd: "[blue]|[reset]", + }), + progressbar.OptionOnCompletion(func() { + doneCh <- struct{}{} + }), + ) + + go func() { + for i := 0; i < 1000; i++ { + bar.Add(1) + time.Sleep(10 * time.Millisecond) + } + }() + + // got notified that progress bar is complete. + <-doneCh + fmt.Println("\n ======= progress bar completed ==========\n") +} diff --git a/progressbar.go b/progressbar.go index 0697540..bfed794 100644 --- a/progressbar.go +++ b/progressbar.go @@ -39,6 +39,7 @@ type state struct { currentPercent int lastPercent int currentSaucerSize int + isAltSaucerHead bool lastShown time.Time startTime time.Time @@ -104,6 +105,7 @@ type config struct { // Theme defines the elements of the bar type Theme struct { Saucer string + AltSaucerHead string SaucerHead string SaucerPadding string BarStart string @@ -655,6 +657,7 @@ func renderProgressBar(c config, s *state) (int, error) { leftBrac := "" rightBrac := "" saucer := "" + saucerHead := "" bytesString := "" str := "" @@ -754,11 +757,18 @@ func renderProgressBar(c config, s *state) (int, error) { } else { saucer = strings.Repeat(c.theme.Saucer, s.currentSaucerSize-1) } - saucerHead := c.theme.SaucerHead - if saucerHead == "" || s.currentSaucerSize == c.width { + + // Check if an alternate saucer head is set for animation + if c.theme.AltSaucerHead != "" && s.isAltSaucerHead { + saucerHead = c.theme.AltSaucerHead + s.isAltSaucerHead = false + } else if c.theme.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 + } else { + saucerHead = c.theme.SaucerHead + s.isAltSaucerHead = true } saucer += saucerHead }