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 5 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
232 changes: 119 additions & 113 deletions progressbar_printer.go
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 @@ -61,101 +63,107 @@ type ProgressbarPrinter struct {
}

// WithTitle sets the name of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithTitle(name string) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithTitle(name string) *ProgressbarPrinter {
p.Title = name
return &p
return p
}

// WithMaxWidth sets the maximum width of the ProgressbarPrinter.
// If the terminal is smaller than the given width, the terminal width will be used instead.
// If the width is set to zero, or below, the terminal width will be used.
func (p ProgressbarPrinter) WithMaxWidth(maxWidth int) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithMaxWidth(maxWidth int) *ProgressbarPrinter {
KarolosLykos marked this conversation as resolved.
Show resolved Hide resolved
p.MaxWidth = maxWidth
return &p
return p
}

// WithTotal sets the total value of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithTotal(total int) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithTotal(total int) *ProgressbarPrinter {
p.Total = total
return &p
return p
}

// WithCurrent sets the current value of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithCurrent(current int) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithCurrent(current int) *ProgressbarPrinter {
p.Current = current
return &p
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 {
func (p *ProgressbarPrinter) WithBarCharacter(char string) *ProgressbarPrinter {
p.BarCharacter = char
return &p
return p
}

// WithLastCharacter sets the last character of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithLastCharacter(char string) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithLastCharacter(char string) *ProgressbarPrinter {
p.LastCharacter = char
return &p
return p
}

// WithElapsedTimeRoundingFactor sets the rounding factor of the elapsed time.
func (p ProgressbarPrinter) WithElapsedTimeRoundingFactor(duration time.Duration) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithElapsedTimeRoundingFactor(duration time.Duration) *ProgressbarPrinter {
p.ElapsedTimeRoundingFactor = duration
return &p
return p
}

// WithShowElapsedTime sets if the elapsed time should be displayed in the ProgressbarPrinter.
func (p ProgressbarPrinter) WithShowElapsedTime(b ...bool) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithShowElapsedTime(b ...bool) *ProgressbarPrinter {
p.ShowElapsedTime = internal.WithBoolean(b)
return &p
return p
}

// WithShowCount sets if the total and current count should be displayed in the ProgressbarPrinter.
func (p ProgressbarPrinter) WithShowCount(b ...bool) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithShowCount(b ...bool) *ProgressbarPrinter {
p.ShowCount = internal.WithBoolean(b)
return &p
return p
}

// WithShowTitle sets if the title should be displayed in the ProgressbarPrinter.
func (p ProgressbarPrinter) WithShowTitle(b ...bool) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithShowTitle(b ...bool) *ProgressbarPrinter {
p.ShowTitle = internal.WithBoolean(b)
return &p
return p
}

// WithShowPercentage sets if the completed percentage should be displayed in the ProgressbarPrinter.
func (p ProgressbarPrinter) WithShowPercentage(b ...bool) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithShowPercentage(b ...bool) *ProgressbarPrinter {
p.ShowPercentage = internal.WithBoolean(b)
return &p
return p
}

// WithTitleStyle sets the style of the title.
func (p ProgressbarPrinter) WithTitleStyle(style *Style) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithTitleStyle(style *Style) *ProgressbarPrinter {
p.TitleStyle = style
return &p
return p
}

// WithBarStyle sets the style of the bar.
func (p ProgressbarPrinter) WithBarStyle(style *Style) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithBarStyle(style *Style) *ProgressbarPrinter {
p.BarStyle = style
return &p
return p
}

// WithRemoveWhenDone sets if the ProgressbarPrinter should be removed when it is done.
func (p ProgressbarPrinter) WithRemoveWhenDone(b ...bool) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithRemoveWhenDone(b ...bool) *ProgressbarPrinter {
p.RemoveWhenDone = internal.WithBoolean(b)
return &p
return p
}

// WithBarFiller sets the filler character for the ProgressbarPrinter.
func (p ProgressbarPrinter) WithBarFiller(char string) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithBarFiller(char string) *ProgressbarPrinter {
p.BarFiller = char
return &p
return p
}

// WithWriter sets the custom Writer.
func (p ProgressbarPrinter) WithWriter(writer io.Writer) *ProgressbarPrinter {
func (p *ProgressbarPrinter) WithWriter(writer io.Writer) *ProgressbarPrinter {
p.Writer = writer
return &p
return p
}

// Increment current value by one.
Expand All @@ -167,77 +175,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 +185,98 @@ func (p *ProgressbarPrinter) Add(count int) *ProgressbarPrinter {
}

p.Current += count
p.updateProgress()

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
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.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()
}

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 {
fClearLine(p.Writer)
Fprinto(p.Writer, before+bar+after)
time.Sleep(p.Delay)
}
}
}()

return &p, nil
return p, nil
}

// Stop the ProgressbarPrinter.
Expand All @@ -291,7 +297,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 +306,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