Skip to content

Commit 9e099d3

Browse files
authored
Merge pull request #137 from LeoVie/master
#86 Add option to write description at end of line instead of begin of line
2 parents 9ee549e + a08b713 commit 9e099d3

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

progressbar.go

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ type config struct {
106106

107107
// whether the render function should make use of ANSI codes to reduce console I/O
108108
useANSICodes bool
109+
110+
// showDescriptionAtLineEnd specifies whether description should be written at line end instead of line start
111+
showDescriptionAtLineEnd bool
109112
}
110113

111114
// Theme defines the elements of the bar
@@ -266,6 +269,13 @@ func OptionUseANSICodes(val bool) Option {
266269
}
267270
}
268271

272+
// OptionShowDescriptionAtLineEnd defines whether description should be written at line end instead of line start
273+
func OptionShowDescriptionAtLineEnd() Option {
274+
return func(p *ProgressBar) {
275+
p.config.showDescriptionAtLineEnd = true
276+
}
277+
}
278+
269279
var defaultTheme = Theme{Saucer: "█", SaucerPadding: " ", BarStart: "|", BarEnd: "|"}
270280

271281
// NewOptions constructs a new instance of ProgressBar, with any options you specify
@@ -807,40 +817,65 @@ func renderProgressBar(c config, s *state) (int, error) {
807817
/*
808818
Progress Bar format
809819
Description % |------ | (kb/s) (iteration count) (iteration rate) (predict time)
820+
821+
or if showDescriptionAtLineEnd is enabled
822+
% |------ | (kb/s) (iteration count) (iteration rate) (predict time) Description
810823
*/
811824
repeatAmount := c.width - s.currentSaucerSize
812825
if repeatAmount < 0 {
813826
repeatAmount = 0
814827
}
815828
if c.ignoreLength {
829+
spinner := spinners[c.spinnerType][int(math.Round(math.Mod(float64(time.Since(s.startTime).Milliseconds()/100), float64(len(spinners[c.spinnerType])))))]
816830
if c.elapsedTime {
817-
str = fmt.Sprintf("\r%s %s %s [%s] ",
818-
spinners[c.spinnerType][int(math.Round(math.Mod(float64(time.Since(s.startTime).Milliseconds()/100), float64(len(spinners[c.spinnerType])))))],
819-
c.description,
820-
bytesString,
821-
leftBrac,
822-
)
831+
if c.showDescriptionAtLineEnd {
832+
str = fmt.Sprintf("\r%s %s [%s] %s ",
833+
spinner,
834+
bytesString,
835+
leftBrac,
836+
c.description,
837+
)
838+
} else {
839+
str = fmt.Sprintf("\r%s %s %s [%s] ",
840+
spinner,
841+
c.description,
842+
bytesString,
843+
leftBrac,
844+
)
845+
}
823846
} else {
824-
str = fmt.Sprintf("\r%s %s %s ",
825-
spinners[c.spinnerType][int(math.Round(math.Mod(float64(time.Since(s.startTime).Milliseconds()/100), float64(len(spinners[c.spinnerType])))))],
826-
c.description,
827-
bytesString,
828-
)
847+
if c.showDescriptionAtLineEnd {
848+
str = fmt.Sprintf("\r%s %s %s ",
849+
spinner,
850+
bytesString,
851+
c.description,
852+
)
853+
} else {
854+
str = fmt.Sprintf("\r%s %s %s ",
855+
spinner,
856+
c.description,
857+
bytesString,
858+
)
859+
}
829860
}
830861
} else if rightBrac == "" {
831-
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s ",
832-
c.description,
862+
str = fmt.Sprintf("%4d%% %s%s%s%s %s",
833863
s.currentPercent,
834864
c.theme.BarStart,
835865
saucer,
836866
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
837867
c.theme.BarEnd,
838868
bytesString,
839869
)
870+
871+
if c.showDescriptionAtLineEnd {
872+
str = fmt.Sprintf("\r%s %s ", str, c.description)
873+
} else {
874+
str = fmt.Sprintf("\r%s %s ", c.description, str)
875+
}
840876
} else {
841877
if s.currentPercent == 100 {
842-
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s",
843-
c.description,
878+
str = fmt.Sprintf("%4d%% %s%s%s%s %s",
844879
s.currentPercent,
845880
c.theme.BarStart,
846881
saucer,
@@ -851,9 +886,14 @@ func renderProgressBar(c config, s *state) (int, error) {
851886
if c.showElapsedTimeOnFinish {
852887
str = fmt.Sprintf("%s [%s]", str, leftBrac)
853888
}
889+
890+
if c.showDescriptionAtLineEnd {
891+
str = fmt.Sprintf("\r%s %s", str, c.description)
892+
} else {
893+
str = fmt.Sprintf("\r%s%s", c.description, str)
894+
}
854895
} else {
855-
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s [%s:%s]",
856-
c.description,
896+
str = fmt.Sprintf("%4d%% %s%s%s%s %s [%s:%s]",
857897
s.currentPercent,
858898
c.theme.BarStart,
859899
saucer,
@@ -863,6 +903,12 @@ func renderProgressBar(c config, s *state) (int, error) {
863903
leftBrac,
864904
rightBrac,
865905
)
906+
907+
if c.showDescriptionAtLineEnd {
908+
str = fmt.Sprintf("\r%s %s", str, c.description)
909+
} else {
910+
str = fmt.Sprintf("\r%s%s", c.description, str)
911+
}
866912
}
867913
}
868914

progressbar_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ func ExampleOptionSetPredictTime() {
136136
// 10% |█ |
137137
}
138138

139+
func ExampleOptionShowDescriptionAtLineEnd() {
140+
bar := NewOptions(100, OptionSetWidth(10), OptionShowDescriptionAtLineEnd(), OptionSetDescription("hello"))
141+
_ = bar.Add(10)
142+
// Output:
143+
// 10% |█ | [0s:0s] hello
144+
}
145+
146+
func ExampleOptionShowDescriptionAtLineEnd_WithSpinner() {
147+
bar := NewOptions(-1, OptionSetWidth(10), OptionShowDescriptionAtLineEnd(), OptionSetDescription("hello"))
148+
_ = bar.Add(1)
149+
// Output:
150+
// | [0s] hello
151+
}
152+
139153
func ExampleDefault() {
140154
bar := Default(100)
141155
for i := 0; i < 50; i++ {

0 commit comments

Comments
 (0)