@@ -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+
269279var 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
0 commit comments