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

#86 Add option to write description at end of line instead of begin of line #137

Merged
merged 1 commit into from Sep 9, 2022
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
80 changes: 63 additions & 17 deletions progressbar.go
Expand Up @@ -106,6 +106,9 @@ type config struct {

// whether the render function should make use of ANSI codes to reduce console I/O
useANSICodes bool

// showDescriptionAtLineEnd specifies whether description should be written at line end instead of line start
showDescriptionAtLineEnd bool
}

// Theme defines the elements of the bar
Expand Down Expand Up @@ -266,6 +269,13 @@ func OptionUseANSICodes(val bool) Option {
}
}

// OptionShowDescriptionAtLineEnd defines whether description should be written at line end instead of line start
func OptionShowDescriptionAtLineEnd() Option {
return func(p *ProgressBar) {
p.config.showDescriptionAtLineEnd = true
}
}

var defaultTheme = Theme{Saucer: "█", SaucerPadding: " ", BarStart: "|", BarEnd: "|"}

// NewOptions constructs a new instance of ProgressBar, with any options you specify
Expand Down Expand Up @@ -807,40 +817,65 @@ func renderProgressBar(c config, s *state) (int, error) {
/*
Progress Bar format
Description % |------ | (kb/s) (iteration count) (iteration rate) (predict time)

or if showDescriptionAtLineEnd is enabled
% |------ | (kb/s) (iteration count) (iteration rate) (predict time) Description
*/
repeatAmount := c.width - s.currentSaucerSize
if repeatAmount < 0 {
repeatAmount = 0
}
if c.ignoreLength {
spinner := spinners[c.spinnerType][int(math.Round(math.Mod(float64(time.Since(s.startTime).Milliseconds()/100), float64(len(spinners[c.spinnerType])))))]
if c.elapsedTime {
str = fmt.Sprintf("\r%s %s %s [%s] ",
spinners[c.spinnerType][int(math.Round(math.Mod(float64(time.Since(s.startTime).Milliseconds()/100), float64(len(spinners[c.spinnerType])))))],
c.description,
bytesString,
leftBrac,
)
if c.showDescriptionAtLineEnd {
str = fmt.Sprintf("\r%s %s [%s] %s ",
spinner,
bytesString,
leftBrac,
c.description,
)
} else {
str = fmt.Sprintf("\r%s %s %s [%s] ",
spinner,
c.description,
bytesString,
leftBrac,
)
}
} else {
str = fmt.Sprintf("\r%s %s %s ",
spinners[c.spinnerType][int(math.Round(math.Mod(float64(time.Since(s.startTime).Milliseconds()/100), float64(len(spinners[c.spinnerType])))))],
c.description,
bytesString,
)
if c.showDescriptionAtLineEnd {
str = fmt.Sprintf("\r%s %s %s ",
spinner,
bytesString,
c.description,
)
} else {
str = fmt.Sprintf("\r%s %s %s ",
spinner,
c.description,
bytesString,
)
}
}
} else if rightBrac == "" {
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s ",
c.description,
str = fmt.Sprintf("%4d%% %s%s%s%s %s",
s.currentPercent,
c.theme.BarStart,
saucer,
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
c.theme.BarEnd,
bytesString,
)

if c.showDescriptionAtLineEnd {
str = fmt.Sprintf("\r%s %s ", str, c.description)
} else {
str = fmt.Sprintf("\r%s %s ", c.description, str)
}
} else {
if s.currentPercent == 100 {
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s",
c.description,
str = fmt.Sprintf("%4d%% %s%s%s%s %s",
s.currentPercent,
c.theme.BarStart,
saucer,
Expand All @@ -851,9 +886,14 @@ func renderProgressBar(c config, s *state) (int, error) {
if c.showElapsedTimeOnFinish {
str = fmt.Sprintf("%s [%s]", str, leftBrac)
}

if c.showDescriptionAtLineEnd {
str = fmt.Sprintf("\r%s %s", str, c.description)
} else {
str = fmt.Sprintf("\r%s%s", c.description, str)
}
} else {
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s [%s:%s]",
c.description,
str = fmt.Sprintf("%4d%% %s%s%s%s %s [%s:%s]",
s.currentPercent,
c.theme.BarStart,
saucer,
Expand All @@ -863,6 +903,12 @@ func renderProgressBar(c config, s *state) (int, error) {
leftBrac,
rightBrac,
)

if c.showDescriptionAtLineEnd {
str = fmt.Sprintf("\r%s %s", str, c.description)
} else {
str = fmt.Sprintf("\r%s%s", c.description, str)
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions progressbar_test.go
Expand Up @@ -136,6 +136,20 @@ func ExampleOptionSetPredictTime() {
// 10% |█ |
}

func ExampleOptionShowDescriptionAtLineEnd() {
bar := NewOptions(100, OptionSetWidth(10), OptionShowDescriptionAtLineEnd(), OptionSetDescription("hello"))
_ = bar.Add(10)
// Output:
// 10% |█ | [0s:0s] hello
}

func ExampleOptionShowDescriptionAtLineEnd_WithSpinner() {
bar := NewOptions(-1, OptionSetWidth(10), OptionShowDescriptionAtLineEnd(), OptionSetDescription("hello"))
_ = bar.Add(1)
// Output:
// | [0s] hello
}

func ExampleDefault() {
bar := Default(100)
for i := 0; i < 50; i++ {
Expand Down