Skip to content

Commit 3041da4

Browse files
committed
Use strings.Builder for bar string composition
1 parent 1e41eee commit 3041da4

File tree

1 file changed

+41
-45
lines changed

1 file changed

+41
-45
lines changed

progressbar.go

+41-45
Original file line numberDiff line numberDiff line change
@@ -707,12 +707,7 @@ func getStringWidth(c config, str string, colorize bool) int {
707707
}
708708

709709
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
716711

717712
averageRate := average(s.counterLastTenRates)
718713
if len(s.counterLastTenRates) == 0 || s.finished {
@@ -727,62 +722,66 @@ func renderProgressBar(c config, s *state) (int, error) {
727722

728723
// show iteration count in "current/total" iterations format
729724
if c.showIterationsCount {
730-
if bytesString == "" {
731-
bytesString += "("
725+
if sb.Len() == 0 {
726+
sb.WriteString("(")
732727
} else {
733-
bytesString += ", "
728+
sb.WriteString(", ")
734729
}
735730
if !c.ignoreLength {
736731
if c.showBytes {
737732
currentHumanize, currentSuffix := humanizeBytes(s.currentBytes)
738733
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))
740736
} 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))
742739
}
743740
} else {
744-
bytesString += fmt.Sprintf("%.0f/%d", s.currentBytes, c.max)
741+
sb.WriteString(fmt.Sprintf("%.0f/%d", s.currentBytes, c.max))
745742
}
746743
} else {
747744
if c.showBytes {
748745
currentHumanize, currentSuffix := humanizeBytes(s.currentBytes)
749-
bytesString += fmt.Sprintf("%s%s", currentHumanize, currentSuffix)
746+
sb.WriteString(fmt.Sprintf("%s%s", currentHumanize, currentSuffix))
750747
} else {
751-
bytesString += fmt.Sprintf("%.0f/%s", s.currentBytes, "-")
748+
sb.WriteString(fmt.Sprintf("%.0f/%s", s.currentBytes, "-"))
752749
}
753750
}
754751
}
755752

756753
// show rolling average rate
757754
if c.showBytes && averageRate > 0 && !math.IsInf(averageRate, 1) {
758-
if bytesString == "" {
759-
bytesString += "("
755+
if sb.Len() == 0 {
756+
sb.WriteString("(")
760757
} else {
761-
bytesString += ", "
758+
sb.WriteString(", ")
762759
}
763760
currentHumanize, currentSuffix := humanizeBytes(averageRate)
764-
bytesString += fmt.Sprintf("%s%s/s", currentHumanize, currentSuffix)
761+
sb.WriteString(fmt.Sprintf("%s%s/s", currentHumanize, currentSuffix))
765762
}
766763

767764
// show iterations rate
768765
if c.showIterationsPerSecond {
769-
if bytesString == "" {
770-
bytesString += "("
766+
if sb.Len() == 0 {
767+
sb.WriteString("(")
771768
} else {
772-
bytesString += ", "
769+
sb.WriteString(", ")
773770
}
774771
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))
776773
} 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))
778775
} 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))
780777
}
781778
}
782-
if bytesString != "" {
783-
bytesString += ")"
779+
if sb.Len() != 0 {
780+
sb.WriteString(")")
784781
}
785782

783+
leftBrac, rightBrac, saucer, saucerHead := "", "", "", ""
784+
786785
// show time prediction in "current/total" seconds format
787786
switch {
788787
case c.predictTime:
@@ -805,7 +804,7 @@ func renderProgressBar(c config, s *state) (int, error) {
805804
}
806805
}
807806

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)
809808
s.currentSaucerSize = int(float64(s.currentPercent) / 100.0 * float64(c.width))
810809
}
811810
if s.currentSaucerSize > 0 {
@@ -837,41 +836,41 @@ func renderProgressBar(c config, s *state) (int, error) {
837836
or if showDescriptionAtLineEnd is enabled
838837
% |------ | (kb/s) (iteration count) (iteration rate) (predict time) Description
839838
*/
839+
840840
repeatAmount := c.width - s.currentSaucerSize
841841
if repeatAmount < 0 {
842842
repeatAmount = 0
843843
}
844+
845+
str := ""
846+
844847
if c.ignoreLength {
845848
spinner := spinners[c.spinnerType][int(math.Round(math.Mod(float64(time.Since(s.startTime).Milliseconds()/100), float64(len(spinners[c.spinnerType])))))]
846849
if c.elapsedTime {
847850
if c.showDescriptionAtLineEnd {
848851
str = fmt.Sprintf("\r%s %s [%s] %s ",
849852
spinner,
850-
bytesString,
853+
sb.String(),
851854
leftBrac,
852-
c.description,
853-
)
855+
c.description)
854856
} else {
855857
str = fmt.Sprintf("\r%s %s %s [%s] ",
856858
spinner,
857859
c.description,
858-
bytesString,
859-
leftBrac,
860-
)
860+
sb.String(),
861+
leftBrac)
861862
}
862863
} else {
863864
if c.showDescriptionAtLineEnd {
864865
str = fmt.Sprintf("\r%s %s %s ",
865866
spinner,
866-
bytesString,
867-
c.description,
868-
)
867+
sb.String(),
868+
c.description)
869869
} else {
870870
str = fmt.Sprintf("\r%s %s %s ",
871871
spinner,
872872
c.description,
873-
bytesString,
874-
)
873+
sb.String())
875874
}
876875
}
877876
} else if rightBrac == "" {
@@ -881,8 +880,7 @@ func renderProgressBar(c config, s *state) (int, error) {
881880
saucer,
882881
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
883882
c.theme.BarEnd,
884-
bytesString,
885-
)
883+
sb.String())
886884

887885
if c.showDescriptionAtLineEnd {
888886
str = fmt.Sprintf("\r%s %s ", str, c.description)
@@ -897,8 +895,7 @@ func renderProgressBar(c config, s *state) (int, error) {
897895
saucer,
898896
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
899897
c.theme.BarEnd,
900-
bytesString,
901-
)
898+
sb.String())
902899
if c.showElapsedTimeOnFinish {
903900
str = fmt.Sprintf("%s [%s]", str, leftBrac)
904901
}
@@ -915,10 +912,9 @@ func renderProgressBar(c config, s *state) (int, error) {
915912
saucer,
916913
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
917914
c.theme.BarEnd,
918-
bytesString,
915+
sb.String(),
919916
leftBrac,
920-
rightBrac,
921-
)
917+
rightBrac)
922918

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

0 commit comments

Comments
 (0)