Skip to content

Commit

Permalink
Merge pull request #117 from wystans/animatedSaucerHead
Browse files Browse the repository at this point in the history
Animated Saucer Head
  • Loading branch information
schollz committed Dec 15, 2021
2 parents 7486f6b + 453e39c commit 8962db7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
41 changes: 41 additions & 0 deletions 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")
}
14 changes: 12 additions & 2 deletions progressbar.go
Expand Up @@ -39,6 +39,7 @@ type state struct {
currentPercent int
lastPercent int
currentSaucerSize int
isAltSaucerHead bool

lastShown time.Time
startTime time.Time
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -655,6 +657,7 @@ func renderProgressBar(c config, s *state) (int, error) {
leftBrac := ""
rightBrac := ""
saucer := ""
saucerHead := ""
bytesString := ""
str := ""

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 8962db7

Please sign in to comment.