Skip to content

Commit

Permalink
Merge pull request #315 from pterm/issue-314-Make_default_progressbar…
Browse files Browse the repository at this point in the history
…_smaller_and_add_option_for_full_width
  • Loading branch information
MarvinJWendt committed Feb 17, 2022
2 parents 1f4d2c7 + af0ab3a commit 590d29d
Show file tree
Hide file tree
Showing 36 changed files with 74 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/docs/printer/progressbar.md
Expand Up @@ -56,6 +56,8 @@ progressbar.Increment()
|[Title](https://pkg.go.dev/github.com/pterm/pterm#ProgressbarPrinter.WithTitle)|string|
|[TitleStyle](https://pkg.go.dev/github.com/pterm/pterm#ProgressbarPrinter.WithTitleStyle)|[*Style](https://pkg.go.dev/github.com/pterm/pterm#Style)|
|[Total](https://pkg.go.dev/github.com/pterm/pterm#ProgressbarPrinter.WithTotal)|int|
|[BarFiller](https://pkg.go.dev/github.com/pterm/pterm#ProgressbarPrinter.WithBarFiller)|string|
|[MaxWidth](https://pkg.go.dev/github.com/pterm/pterm#ProgressbarPrinter.WithMaxWidth)|int|

### Output functions

Expand Down
42 changes: 38 additions & 4 deletions progressbar_printer.go
Expand Up @@ -27,7 +27,8 @@ var (
ShowCount: true,
ShowPercentage: true,
ShowElapsedTime: true,
BarFiller: " ",
BarFiller: "░",
MaxWidth: 80,
}
)

Expand All @@ -40,6 +41,7 @@ type ProgressbarPrinter struct {
LastCharacter string
ElapsedTimeRoundingFactor time.Duration
BarFiller string
MaxWidth int

ShowElapsedTime bool
ShowCount bool
Expand All @@ -61,6 +63,14 @@ func (p ProgressbarPrinter) WithTitle(name string) *ProgressbarPrinter {
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 {
p.MaxWidth = maxWidth
return &p
}

// WithTotal sets the total value of the ProgressbarPrinter.
func (p ProgressbarPrinter) WithTotal(total int) *ProgressbarPrinter {
p.Total = total
Expand Down Expand Up @@ -133,6 +143,12 @@ func (p ProgressbarPrinter) WithRemoveWhenDone(b ...bool) *ProgressbarPrinter {
return &p
}

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

// Increment current value by one.
func (p *ProgressbarPrinter) Increment() *ProgressbarPrinter {
p.Add(1)
Expand Down Expand Up @@ -160,8 +176,16 @@ func (p *ProgressbarPrinter) updateProgress() *ProgressbarPrinter {

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
}

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

decoratorCount := Gray("[") + LightWhite(p.Current) + Gray("/") + LightWhite(p.Total) + Gray("]")
Expand All @@ -188,10 +212,20 @@ func (p *ProgressbarPrinter) updateProgress() *ProgressbarPrinter {
}

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

barCurrentLength := (p.Current * barMaxLength) / p.Total
barFiller := strings.Repeat(p.BarFiller, barMaxLength-barCurrentLength)
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 = ""
}

bar := p.BarStyle.Sprint(strings.Repeat(p.BarCharacter, barCurrentLength)+p.LastCharacter) + barFiller
if !RawOutput {
Printo(before + bar + after)
}
Expand Down
26 changes: 26 additions & 0 deletions progressbar_printer_test.go
Expand Up @@ -16,6 +16,18 @@ func TestProgressbarPrinter_Add(t *testing.T) {
p.Stop()
}

func TestProgressbarPrinter_Add_With(t *testing.T) {
proxyToDevNull()
w := pterm.GetTerminalWidth()
h := pterm.GetTerminalHeight()
pterm.SetForcedTerminalSize(1, 1)
p := pterm.DefaultProgressbar.WithTotal(2000)
p.Add(1337)
testza.AssertEqual(t, 1337, p.Current)
p.Stop()
pterm.SetForcedTerminalSize(w, h)
}

func TestProgressbarPrinter_AddWithNoStyle(t *testing.T) {
proxyToDevNull()
p := pterm.ProgressbarPrinter{}.WithTotal(2000)
Expand Down Expand Up @@ -176,6 +188,20 @@ func TestProgressbarPrinter_WithTotal(t *testing.T) {
testza.AssertEqual(t, 1337, p2.Total)
}

func TestProgressbarPrinter_WithMaxWidth(t *testing.T) {
p := pterm.ProgressbarPrinter{}
p2 := p.WithMaxWidth(1337)

testza.AssertEqual(t, 1337, p2.MaxWidth)
}

func TestProgressbarPrinter_WithBarFiller(t *testing.T) {
p := pterm.ProgressbarPrinter{}
p2 := p.WithBarFiller("-")

testza.AssertEqual(t, "-", p2.BarFiller)
}

func TestProgressbarPrinter_UpdateTitle(t *testing.T) {
p := pterm.ProgressbarPrinter{}
p2 := p.WithTitle("test")
Expand Down
Empty file modified testdata/snapshots/TestBarChartPrinter_NilStylePrint.testza 100755 → 100644
Empty file.
Empty file modified testdata/snapshots/TestBarChartPrinter_Render.testza 100755 → 100644
Empty file.
Empty file modified testdata/snapshots/TestBarChartPrinter_RenderExample.testza 100755 → 100644
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified testdata/snapshots/TestBigTextPrinter_Render.testza 100755 → 100644
Empty file.
Empty file modified testdata/snapshots/TestBigTextPrinter_RenderNoStyling.testza 100755 → 100644
Empty file.
Empty file modified testdata/snapshots/TestBigTextPrinter_RenderRawOutput.testza 100755 → 100644
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion testdata/snapshots/TestDemo0.testza 100755 → 100644

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion testdata/snapshots/TestDemo1.testza 100755 → 100644
@@ -1 +1 @@
(string) (len=2665) "\r \r\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\x1b[104m\x1b[104m\x1b[97;1m\x1b[97;1m Pseudo Application created with PTerm \x1b[0m\x1b[104m\x1b[0m\x1b[104m\x1b[0m\x1b[0m\n\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\r \r\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\x1b[104m\x1b[104m\x1b[97;1m\x1b[97;1m Pseudo Application created with PTerm \x1b[0m\x1b[104m\x1b[0m\x1b[104m\x1b[0m\x1b[0m\n\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\r \r\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\x1b[104m\x1b[104m\x1b[97;1m\x1b[97;1m Pseudo Application created with PTerm \x1b[0m\x1b[104m\x1b[0m\x1b[104m\x1b[0m\x1b[0m\n\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\r \r\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\x1b[104m\x1b[104m\x1b[97;1m\x1b[97;1m Pseudo Application created with PTerm \x1b[0m\x1b[104m\x1b[0m\x1b[104m\x1b[0m\x1b[0m\n\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\r \r\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\x1b[104m\x1b[104m\x1b[97;1m\x1b[97;1m Pseudo Application created with PTerm \x1b[0m\x1b[104m\x1b[0m\x1b[104m\x1b[0m\x1b[0m\n\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m1\x1b[0m\x1b[90m]\x1b[0m \x1b[36m\x1b[36m█\x1b[0m\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[36m\x1b[36m█\x1b[0m\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[36m\x1b[36m█\x1b[0m\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[36m\x1b[36m█\x1b[0m\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[36m\x1b[36m█\x1b[0m\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s"
(string) (len=2248) "\r \r\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\x1b[104m\x1b[104m\x1b[97;1m\x1b[97;1m Pseudo Application created with PTerm \x1b[0m\x1b[104m\x1b[0m\x1b[104m\x1b[0m\x1b[0m\n\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\r \r\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\x1b[104m\x1b[104m\x1b[97;1m\x1b[97;1m Pseudo Application created with PTerm \x1b[0m\x1b[104m\x1b[0m\x1b[104m\x1b[0m\x1b[0m\n\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\r \r\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\x1b[104m\x1b[104m\x1b[97;1m\x1b[97;1m Pseudo Application created with PTerm \x1b[0m\x1b[104m\x1b[0m\x1b[104m\x1b[0m\x1b[0m\n\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\r \r\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\x1b[104m\x1b[104m\x1b[97;1m\x1b[97;1m Pseudo Application created with PTerm \x1b[0m\x1b[104m\x1b[0m\x1b[104m\x1b[0m\x1b[0m\n\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\r \r\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\x1b[104m\x1b[104m\x1b[97;1m\x1b[97;1m Pseudo Application created with PTerm \x1b[0m\x1b[104m\x1b[0m\x1b[104m\x1b[0m\x1b[0m\n\x1b[104m\x1b[104m \x1b[0m\x1b[0m\n\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m1\x1b[0m\x1b[90m]\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s"
2 changes: 1 addition & 1 deletion testdata/snapshots/TestDemo2.testza 100755 → 100644

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion testdata/snapshots/TestDemo3.testza 100755 → 100644
@@ -1 +1 @@
(string) (len=1630) "\r \r\n## \x1b[1;33m\x1b[1;33mProgram Install Report\x1b[0m\n\x1b[1;33m\x1b[0m\x1b[0m\n\r \r\n## \x1b[1;33m\x1b[1;33mProgram Install Report\x1b[0m\n\x1b[1;33m\x1b[0m\x1b[0m\n\r \r\n## \x1b[1;33m\x1b[1;33mProgram Install Report\x1b[0m\n\x1b[1;33m\x1b[0m\x1b[0m\n\r \r\n## \x1b[1;33m\x1b[1;33mProgram Install Report\x1b[0m\n\x1b[1;33m\x1b[0m\x1b[0m\n\r \r\n## \x1b[1;33m\x1b[1;33mProgram Install Report\x1b[0m\n\x1b[1;33m\x1b[0m\x1b[0m\n\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m1\x1b[0m\x1b[90m]\x1b[0m \x1b[36m\x1b[36m█\x1b[0m\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[36m\x1b[36m█\x1b[0m\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[36m\x1b[36m█\x1b[0m\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[36m\x1b[36m█\x1b[0m\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[36m\x1b[36m█\x1b[0m\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s"
(string) (len=1213) "\r \r\n## \x1b[1;33m\x1b[1;33mProgram Install Report\x1b[0m\n\x1b[1;33m\x1b[0m\x1b[0m\n\r \r\n## \x1b[1;33m\x1b[1;33mProgram Install Report\x1b[0m\n\x1b[1;33m\x1b[0m\x1b[0m\n\r \r\n## \x1b[1;33m\x1b[1;33mProgram Install Report\x1b[0m\n\x1b[1;33m\x1b[0m\x1b[0m\n\r \r\n## \x1b[1;33m\x1b[1;33mProgram Install Report\x1b[0m\n\x1b[1;33m\x1b[0m\x1b[0m\n\r \r\n## \x1b[1;33m\x1b[1;33mProgram Install Report\x1b[0m\n\x1b[1;33m\x1b[0m\x1b[0m\n\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m1\x1b[0m\x1b[90m]\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s\r\x1b[96m\x1b[96m\x1b[0m\x1b[0m \x1b[90m[\x1b[0m\x1b[97m0\x1b[0m\x1b[90m/\x1b[0m\x1b[97m100\x1b[0m\x1b[90m]\x1b[0m \x1b[38;2;255;0;0m0%\x1b[0m | 0s"
2 changes: 1 addition & 1 deletion testdata/snapshots/TestDemo4.testza 100755 → 100644

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion testdata/snapshots/TestDemo5.testza 100755 → 100644

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion testdata/snapshots/TestDemo6.testza 100755 → 100644

Large diffs are not rendered by default.

0 comments on commit 590d29d

Please sign in to comment.