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

Various progressbar improvements #500

Merged
merged 3 commits into from
May 11, 2023
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
3 changes: 3 additions & 0 deletions _examples/progressbar/demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func main() {
p, _ := pterm.DefaultProgressbar.WithTotal(len(fakeInstallList)).WithTitle("Downloading stuff").Start()

for i := 0; i < p.Total; i++ {
if i == 6 {
time.Sleep(time.Second * 3) // Simulate a slow download.
}
p.UpdateTitle("Downloading " + fakeInstallList[i]) // Update the title of the progressbar.
pterm.Success.Println("Downloading " + fakeInstallList[i]) // If a progressbar is running, each print will be printed above the progressbar.
p.Increment() // Increment the progressbar by one. Use Add(x int) to increment by a custom amount.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (

require (
atomicgo.dev/assert v0.0.2 // indirect
atomicgo.dev/schedule v0.0.2 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ atomicgo.dev/cursor v0.1.1 h1:0t9sxQomCTRh5ug+hAMCs59x/UmC9QL6Ci5uosINKD4=
atomicgo.dev/cursor v0.1.1/go.mod h1:Lr4ZJB3U7DfPPOkbH7/6TOtJ4vFGHlgj1nc+n900IpU=
atomicgo.dev/keyboard v0.2.9 h1:tOsIid3nlPLZ3lwgG8KZMp/SFmr7P0ssEN5JUsm78K8=
atomicgo.dev/keyboard v0.2.9/go.mod h1:BC4w9g00XkxH/f1HXhW2sXmJFOCWbKn9xrOunSFtExQ=
atomicgo.dev/schedule v0.0.2 h1:2e/4KY6t3wokja01Cyty6qgkQM8MotJzjtqCH70oX2Q=
atomicgo.dev/schedule v0.0.2/go.mod h1:xeUa3oAkiuHYh8bKiQBRojqAMq3PXXbJujjb0hw8pEU=
github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs=
github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3kkcZX4hv9Rp8=
github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBExvZqnKYWlII=
Expand Down
42 changes: 28 additions & 14 deletions progressbar_printer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package pterm

import (
"atomicgo.dev/cursor"
"atomicgo.dev/schedule"
"fmt"
"io"
"strconv"
"math"
"strings"
"time"

Expand All @@ -28,7 +31,7 @@ var (
ShowCount: true,
ShowPercentage: true,
ShowElapsedTime: true,
BarFiller: " ",
BarFiller: Gray("█"),
MaxWidth: 80,
}
)
Expand All @@ -55,7 +58,8 @@ type ProgressbarPrinter struct {

IsActive bool

startedAt time.Time
startedAt time.Time
rerenderTask *schedule.Task

Writer io.Writer
}
Expand Down Expand Up @@ -173,6 +177,9 @@ func (p *ProgressbarPrinter) UpdateTitle(title string) *ProgressbarPrinter {

// This is the update logic, renders the progressbar
func (p *ProgressbarPrinter) updateProgress() *ProgressbarPrinter {
if !p.IsActive {
return p
}
if p.TitleStyle == nil {
p.TitleStyle = NewStyle()
}
Expand All @@ -195,25 +202,20 @@ func (p *ProgressbarPrinter) updateProgress() *ProgressbarPrinter {
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 + " "
before += p.TitleStyle.Sprint(p.Title) + " "
}
if p.ShowCount {
before += decoratorCount + " "
padding := 1 + int(math.Log10(float64(p.Total)))
before += Gray("[") + LightWhite(fmt.Sprintf("%0*d", padding, p.Current)) + Gray("/") + LightWhite(p.Total) + Gray("]") + " "
}

after += " "

if p.ShowPercentage {
currentPercentage := int(internal.PercentageRound(float64(int64(p.Total)), float64(int64(p.Current))))
decoratorCurrentPercentage := color.RGB(NewRGB(255, 0, 0).Fade(0, float32(p.Total), float32(p.Current), NewRGB(0, 255, 0)).GetValues()).
Sprintf("%3d%%", currentPercentage)
after += decoratorCurrentPercentage + " "
}
if p.ShowElapsedTime {
Expand Down Expand Up @@ -256,6 +258,7 @@ func (p *ProgressbarPrinter) Add(count int) *ProgressbarPrinter {

// Start the ProgressbarPrinter.
func (p ProgressbarPrinter) Start(title ...interface{}) (*ProgressbarPrinter, error) {
cursor.Hide()
if RawOutput && p.ShowTitle {
Fprintln(p.Writer, p.Title)
}
Expand All @@ -268,11 +271,22 @@ func (p ProgressbarPrinter) Start(title ...interface{}) (*ProgressbarPrinter, er

p.updateProgress()

if p.ShowElapsedTime {
p.rerenderTask = schedule.Every(time.Second, func() {
p.updateProgress()
})
}

return &p, nil
}

// Stop the ProgressbarPrinter.
func (p *ProgressbarPrinter) Stop() (*ProgressbarPrinter, error) {
if p.rerenderTask != nil && p.rerenderTask.IsActive() {
p.rerenderTask.Stop()
}
cursor.Show()

if !p.IsActive {
return p, nil
}
Expand Down
16 changes: 16 additions & 0 deletions pterm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
package pterm

import (
"atomicgo.dev/cursor"
"github.com/gookit/color"
"os"
"os/signal"
"syscall"
)

var (
Expand All @@ -26,6 +30,18 @@ var (

func init() {
color.ForceColor()

// Make the cursor visible when the program stops
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
for range c {
cursor.Show()

os.Exit(0)
}
}()
}

// EnableOutput enables the output of PTerm.
Expand Down