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

feat: proposal live update progressbar #486

Closed
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
178 changes: 97 additions & 81 deletions progressbar_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ var ActiveProgressBarPrinters []*ProgressbarPrinter

var (
// DefaultProgressbar is the default ProgressbarPrinter.
DefaultProgressbar = ProgressbarPrinter{
DefaultProgressbar = &ProgressbarPrinter{
Total: 100,
BarCharacter: "█",
LastCharacter: "█",
Delay: time.Millisecond * 200,
ElapsedTimeRoundingFactor: time.Second,
BarStyle: &ThemeDefault.ProgressbarBarStyle,
TitleStyle: &ThemeDefault.ProgressbarTitleStyle,
Expand All @@ -38,6 +39,7 @@ type ProgressbarPrinter struct {
Title string
Total int
Current int
Delay time.Duration
BarCharacter string
LastCharacter string
ElapsedTimeRoundingFactor time.Duration
Expand All @@ -54,6 +56,7 @@ type ProgressbarPrinter struct {
BarStyle *Style

IsActive bool
update bool

startedAt time.Time

Expand Down Expand Up @@ -86,6 +89,12 @@ func (p ProgressbarPrinter) WithCurrent(current int) *ProgressbarPrinter {
return &p
}

// WithDelay add a delay to the ProgressbarPrinter.
func (p ProgressbarPrinter) WithDelay(delay time.Duration) *ProgressbarPrinter {
p.Delay = delay
return &p
}

// WithBarCharacter sets the bar character of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithBarCharacter(char string) *ProgressbarPrinter {
p.BarCharacter = char
Expand Down Expand Up @@ -167,77 +176,6 @@ func (p *ProgressbarPrinter) Increment() *ProgressbarPrinter {
// UpdateTitle updates the title and re-renders the progressbar
func (p *ProgressbarPrinter) UpdateTitle(title string) *ProgressbarPrinter {
p.Title = title
p.updateProgress()
return p
}

// This is the update logic, renders the progressbar
func (p *ProgressbarPrinter) updateProgress() *ProgressbarPrinter {
if p.TitleStyle == nil {
p.TitleStyle = NewStyle()
}
if p.BarStyle == nil {
p.BarStyle = NewStyle()
}
if p.Total == 0 {
return nil
}

var before string
var after string
var width int

if p.MaxWidth <= 0 {
width = GetTerminalWidth()
} else if GetTerminalWidth() < p.MaxWidth {
width = GetTerminalWidth()
} else {
width = p.MaxWidth
}

currentPercentage := int(internal.PercentageRound(float64(int64(p.Total)), float64(int64(p.Current))))

decoratorCount := Gray("[") + LightWhite(p.Current) + Gray("/") + LightWhite(p.Total) + Gray("]")

decoratorCurrentPercentage := color.RGB(NewRGB(255, 0, 0).Fade(0, float32(p.Total), float32(p.Current), NewRGB(0, 255, 0)).GetValues()).
Sprint(strconv.Itoa(currentPercentage) + "%")

decoratorTitle := p.TitleStyle.Sprint(p.Title)

if p.ShowTitle {
before += decoratorTitle + " "
}
if p.ShowCount {
before += decoratorCount + " "
}

after += " "

if p.ShowPercentage {
after += decoratorCurrentPercentage + " "
}
if p.ShowElapsedTime {
after += "| " + p.parseElapsedTime()
}

barMaxLength := width - len(RemoveColorFromString(before)) - len(RemoveColorFromString(after)) - 1

barCurrentLength := (p.Current * barMaxLength) / p.Total
var barFiller string
if barMaxLength-barCurrentLength > 0 {
barFiller = strings.Repeat(p.BarFiller, barMaxLength-barCurrentLength)
}

var bar string
if barCurrentLength > 0 {
bar = p.BarStyle.Sprint(strings.Repeat(p.BarCharacter, barCurrentLength)+p.LastCharacter) + barFiller
} else {
bar = ""
}

if !RawOutput {
Fprinto(p.Writer, before+bar+after)
}
return p
}

Expand All @@ -248,29 +186,107 @@ func (p *ProgressbarPrinter) Add(count int) *ProgressbarPrinter {
}

p.Current += count
p.updateProgress()
p.update = true

if p.Current >= p.Total {
p.Stop()
if p.Current > p.Total {
p.Current = p.Total
}

return p
}

// Start the ProgressbarPrinter.
func (p ProgressbarPrinter) Start(title ...interface{}) (*ProgressbarPrinter, error) {
func (p *ProgressbarPrinter) Start(title ...interface{}) (*ProgressbarPrinter, error) {
if RawOutput && p.ShowTitle {
Fprintln(p.Writer, p.Title)
}
p.IsActive = true
p.update = true

if len(title) != 0 {
p.Title = Sprint(title...)
}
ActiveProgressBarPrinters = append(ActiveProgressBarPrinters, &p)
ActiveProgressBarPrinters = append(ActiveProgressBarPrinters, p)
p.startedAt = time.Now()

p.updateProgress()
go func() {
for p.IsActive {
if !p.ShowElapsedTime && !p.update {
continue
}

p.update = false

if p.TitleStyle == nil {
p.TitleStyle = NewStyle()
}
if p.BarStyle == nil {
p.BarStyle = NewStyle()
}
if p.Total == 0 {
continue
}

var before string
var after string
var width int

if p.MaxWidth <= 0 {
width = GetTerminalWidth()
} else if GetTerminalWidth() < p.MaxWidth {
width = GetTerminalWidth()
} else {
width = p.MaxWidth
}

currentPercentage := int(internal.PercentageRound(float64(int64(p.Total)), float64(int64(p.Current))))
decoratorCount := Gray("[") + LightWhite(p.Current) + Gray("/") + LightWhite(p.Total) + Gray("]")

decoratorCurrentPercentage := color.RGB(NewRGB(255, 0, 0).Fade(0, float32(p.Total), float32(p.Current), NewRGB(0, 255, 0)).GetValues()).
Sprint(strconv.Itoa(currentPercentage) + "%")

decoratorTitle := p.TitleStyle.Sprint(p.Title)

if p.ShowTitle {
before += decoratorTitle + " "
}
if p.ShowCount {
before += decoratorCount + " "
}

after += " "

if p.ShowPercentage {
after += decoratorCurrentPercentage + " "
}
if p.ShowElapsedTime {
after += "| " + p.parseElapsedTime()
p.update = true
}

barMaxLength := width - len(RemoveColorFromString(before)) - len(RemoveColorFromString(after)) - 1

barCurrentLength := (p.Current * barMaxLength) / p.Total
var barFiller string
if barMaxLength-barCurrentLength > 0 {
barFiller = strings.Repeat(p.BarFiller, barMaxLength-barCurrentLength)
}

var bar string
if barCurrentLength > 0 {
bar = p.BarStyle.Sprint(strings.Repeat(p.BarCharacter, barCurrentLength)+p.LastCharacter) + barFiller
} else {
bar = ""
}

if !RawOutput {
Fprinto(p.Writer, before+bar+after)
time.Sleep(p.Delay)
}
}
}()

return &p, nil
return p, nil
}

// Stop the ProgressbarPrinter.
Expand All @@ -291,7 +307,7 @@ func (p *ProgressbarPrinter) Stop() (*ProgressbarPrinter, error) {
// GenericStart runs Start, but returns a LivePrinter.
// This is used for the interface LivePrinter.
// You most likely want to use Start instead of this in your program.
func (p ProgressbarPrinter) GenericStart() (*LivePrinter, error) {
func (p *ProgressbarPrinter) GenericStart() (*LivePrinter, error) {
p2, _ := p.Start()
lp := LivePrinter(p2)
return &lp, nil
Expand All @@ -300,7 +316,7 @@ func (p ProgressbarPrinter) GenericStart() (*LivePrinter, error) {
// GenericStop runs Stop, but returns a LivePrinter.
// This is used for the interface LivePrinter.
// You most likely want to use Stop instead of this in your program.
func (p ProgressbarPrinter) GenericStop() (*LivePrinter, error) {
func (p *ProgressbarPrinter) GenericStop() (*LivePrinter, error) {
p2, _ := p.Stop()
lp := LivePrinter(p2)
return &lp, nil
Expand Down