Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animated Saucer Head #117

Merged
merged 5 commits into from Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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