@@ -707,12 +707,7 @@ func getStringWidth(c config, str string, colorize bool) int {
707
707
}
708
708
709
709
func renderProgressBar (c config , s * state ) (int , error ) {
710
- leftBrac := ""
711
- rightBrac := ""
712
- saucer := ""
713
- saucerHead := ""
714
- bytesString := ""
715
- str := ""
710
+ var sb strings.Builder
716
711
717
712
averageRate := average (s .counterLastTenRates )
718
713
if len (s .counterLastTenRates ) == 0 || s .finished {
@@ -727,62 +722,66 @@ func renderProgressBar(c config, s *state) (int, error) {
727
722
728
723
// show iteration count in "current/total" iterations format
729
724
if c .showIterationsCount {
730
- if bytesString == "" {
731
- bytesString += "("
725
+ if sb . Len () == 0 {
726
+ sb . WriteString ( "(" )
732
727
} else {
733
- bytesString += ", "
728
+ sb . WriteString ( ", " )
734
729
}
735
730
if ! c .ignoreLength {
736
731
if c .showBytes {
737
732
currentHumanize , currentSuffix := humanizeBytes (s .currentBytes )
738
733
if currentSuffix == c .maxHumanizedSuffix {
739
- bytesString += fmt .Sprintf ("%s/%s%s" , currentHumanize , c .maxHumanized , c .maxHumanizedSuffix )
734
+ sb .WriteString (fmt .Sprintf ("%s/%s%s" ,
735
+ currentHumanize , c .maxHumanized , c .maxHumanizedSuffix ))
740
736
} else {
741
- bytesString += fmt .Sprintf ("%s%s/%s%s" , currentHumanize , currentSuffix , c .maxHumanized , c .maxHumanizedSuffix )
737
+ sb .WriteString (fmt .Sprintf ("%s%s/%s%s" ,
738
+ currentHumanize , currentSuffix , c .maxHumanized , c .maxHumanizedSuffix ))
742
739
}
743
740
} else {
744
- bytesString += fmt .Sprintf ("%.0f/%d" , s .currentBytes , c .max )
741
+ sb . WriteString ( fmt .Sprintf ("%.0f/%d" , s .currentBytes , c .max ) )
745
742
}
746
743
} else {
747
744
if c .showBytes {
748
745
currentHumanize , currentSuffix := humanizeBytes (s .currentBytes )
749
- bytesString += fmt .Sprintf ("%s%s" , currentHumanize , currentSuffix )
746
+ sb . WriteString ( fmt .Sprintf ("%s%s" , currentHumanize , currentSuffix ) )
750
747
} else {
751
- bytesString += fmt .Sprintf ("%.0f/%s" , s .currentBytes , "-" )
748
+ sb . WriteString ( fmt .Sprintf ("%.0f/%s" , s .currentBytes , "-" ) )
752
749
}
753
750
}
754
751
}
755
752
756
753
// show rolling average rate
757
754
if c .showBytes && averageRate > 0 && ! math .IsInf (averageRate , 1 ) {
758
- if bytesString == "" {
759
- bytesString += "("
755
+ if sb . Len () == 0 {
756
+ sb . WriteString ( "(" )
760
757
} else {
761
- bytesString += ", "
758
+ sb . WriteString ( ", " )
762
759
}
763
760
currentHumanize , currentSuffix := humanizeBytes (averageRate )
764
- bytesString += fmt .Sprintf ("%s%s/s" , currentHumanize , currentSuffix )
761
+ sb . WriteString ( fmt .Sprintf ("%s%s/s" , currentHumanize , currentSuffix ) )
765
762
}
766
763
767
764
// show iterations rate
768
765
if c .showIterationsPerSecond {
769
- if bytesString == "" {
770
- bytesString += "("
766
+ if sb . Len () == 0 {
767
+ sb . WriteString ( "(" )
771
768
} else {
772
- bytesString += ", "
769
+ sb . WriteString ( ", " )
773
770
}
774
771
if averageRate > 1 {
775
- bytesString += fmt .Sprintf ("%0.0f %s/s" , averageRate , c .iterationString )
772
+ sb . WriteString ( fmt .Sprintf ("%0.0f %s/s" , averageRate , c .iterationString ) )
776
773
} else if averageRate * 60 > 1 {
777
- bytesString += fmt .Sprintf ("%0.0f %s/min" , 60 * averageRate , c .iterationString )
774
+ sb . WriteString ( fmt .Sprintf ("%0.0f %s/min" , 60 * averageRate , c .iterationString ) )
778
775
} else {
779
- bytesString += fmt .Sprintf ("%0.0f %s/hr" , 3600 * averageRate , c .iterationString )
776
+ sb . WriteString ( fmt .Sprintf ("%0.0f %s/hr" , 3600 * averageRate , c .iterationString ) )
780
777
}
781
778
}
782
- if bytesString != "" {
783
- bytesString += ")"
779
+ if sb . Len () != 0 {
780
+ sb . WriteString ( ")" )
784
781
}
785
782
783
+ leftBrac , rightBrac , saucer , saucerHead := "" , "" , "" , ""
784
+
786
785
// show time prediction in "current/total" seconds format
787
786
switch {
788
787
case c .predictTime :
@@ -805,7 +804,7 @@ func renderProgressBar(c config, s *state) (int, error) {
805
804
}
806
805
}
807
806
808
- c .width = width - getStringWidth (c , c .description , true ) - 14 - len ( bytesString ) - len (leftBrac ) - len (rightBrac )
807
+ c .width = width - getStringWidth (c , c .description , true ) - 14 - sb . Len ( ) - len (leftBrac ) - len (rightBrac )
809
808
s .currentSaucerSize = int (float64 (s .currentPercent ) / 100.0 * float64 (c .width ))
810
809
}
811
810
if s .currentSaucerSize > 0 {
@@ -837,41 +836,41 @@ func renderProgressBar(c config, s *state) (int, error) {
837
836
or if showDescriptionAtLineEnd is enabled
838
837
% |------ | (kb/s) (iteration count) (iteration rate) (predict time) Description
839
838
*/
839
+
840
840
repeatAmount := c .width - s .currentSaucerSize
841
841
if repeatAmount < 0 {
842
842
repeatAmount = 0
843
843
}
844
+
845
+ str := ""
846
+
844
847
if c .ignoreLength {
845
848
spinner := spinners [c .spinnerType ][int (math .Round (math .Mod (float64 (time .Since (s .startTime ).Milliseconds ()/ 100 ), float64 (len (spinners [c .spinnerType ])))))]
846
849
if c .elapsedTime {
847
850
if c .showDescriptionAtLineEnd {
848
851
str = fmt .Sprintf ("\r %s %s [%s] %s " ,
849
852
spinner ,
850
- bytesString ,
853
+ sb . String () ,
851
854
leftBrac ,
852
- c .description ,
853
- )
855
+ c .description )
854
856
} else {
855
857
str = fmt .Sprintf ("\r %s %s %s [%s] " ,
856
858
spinner ,
857
859
c .description ,
858
- bytesString ,
859
- leftBrac ,
860
- )
860
+ sb .String (),
861
+ leftBrac )
861
862
}
862
863
} else {
863
864
if c .showDescriptionAtLineEnd {
864
865
str = fmt .Sprintf ("\r %s %s %s " ,
865
866
spinner ,
866
- bytesString ,
867
- c .description ,
868
- )
867
+ sb .String (),
868
+ c .description )
869
869
} else {
870
870
str = fmt .Sprintf ("\r %s %s %s " ,
871
871
spinner ,
872
872
c .description ,
873
- bytesString ,
874
- )
873
+ sb .String ())
875
874
}
876
875
}
877
876
} else if rightBrac == "" {
@@ -881,8 +880,7 @@ func renderProgressBar(c config, s *state) (int, error) {
881
880
saucer ,
882
881
strings .Repeat (c .theme .SaucerPadding , repeatAmount ),
883
882
c .theme .BarEnd ,
884
- bytesString ,
885
- )
883
+ sb .String ())
886
884
887
885
if c .showDescriptionAtLineEnd {
888
886
str = fmt .Sprintf ("\r %s %s " , str , c .description )
@@ -897,8 +895,7 @@ func renderProgressBar(c config, s *state) (int, error) {
897
895
saucer ,
898
896
strings .Repeat (c .theme .SaucerPadding , repeatAmount ),
899
897
c .theme .BarEnd ,
900
- bytesString ,
901
- )
898
+ sb .String ())
902
899
if c .showElapsedTimeOnFinish {
903
900
str = fmt .Sprintf ("%s [%s]" , str , leftBrac )
904
901
}
@@ -915,10 +912,9 @@ func renderProgressBar(c config, s *state) (int, error) {
915
912
saucer ,
916
913
strings .Repeat (c .theme .SaucerPadding , repeatAmount ),
917
914
c .theme .BarEnd ,
918
- bytesString ,
915
+ sb . String () ,
919
916
leftBrac ,
920
- rightBrac ,
921
- )
917
+ rightBrac )
922
918
923
919
if c .showDescriptionAtLineEnd {
924
920
str = fmt .Sprintf ("\r %s %s" , str , c .description )
0 commit comments