Skip to content

Commit

Permalink
Use strings.Builder for bar string composition
Browse files Browse the repository at this point in the history
  • Loading branch information
oerlikon committed Nov 4, 2022
1 parent 1e41eee commit 3041da4
Showing 1 changed file with 41 additions and 45 deletions.
86 changes: 41 additions & 45 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,12 +707,7 @@ func getStringWidth(c config, str string, colorize bool) int {
}

func renderProgressBar(c config, s *state) (int, error) {
leftBrac := ""
rightBrac := ""
saucer := ""
saucerHead := ""
bytesString := ""
str := ""
var sb strings.Builder

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

// show iteration count in "current/total" iterations format
if c.showIterationsCount {
if bytesString == "" {
bytesString += "("
if sb.Len() == 0 {
sb.WriteString("(")
} else {
bytesString += ", "
sb.WriteString(", ")
}
if !c.ignoreLength {
if c.showBytes {
currentHumanize, currentSuffix := humanizeBytes(s.currentBytes)
if currentSuffix == c.maxHumanizedSuffix {
bytesString += fmt.Sprintf("%s/%s%s", currentHumanize, c.maxHumanized, c.maxHumanizedSuffix)
sb.WriteString(fmt.Sprintf("%s/%s%s",
currentHumanize, c.maxHumanized, c.maxHumanizedSuffix))
} else {
bytesString += fmt.Sprintf("%s%s/%s%s", currentHumanize, currentSuffix, c.maxHumanized, c.maxHumanizedSuffix)
sb.WriteString(fmt.Sprintf("%s%s/%s%s",
currentHumanize, currentSuffix, c.maxHumanized, c.maxHumanizedSuffix))
}
} else {
bytesString += fmt.Sprintf("%.0f/%d", s.currentBytes, c.max)
sb.WriteString(fmt.Sprintf("%.0f/%d", s.currentBytes, c.max))
}
} else {
if c.showBytes {
currentHumanize, currentSuffix := humanizeBytes(s.currentBytes)
bytesString += fmt.Sprintf("%s%s", currentHumanize, currentSuffix)
sb.WriteString(fmt.Sprintf("%s%s", currentHumanize, currentSuffix))
} else {
bytesString += fmt.Sprintf("%.0f/%s", s.currentBytes, "-")
sb.WriteString(fmt.Sprintf("%.0f/%s", s.currentBytes, "-"))
}
}
}

// show rolling average rate
if c.showBytes && averageRate > 0 && !math.IsInf(averageRate, 1) {
if bytesString == "" {
bytesString += "("
if sb.Len() == 0 {
sb.WriteString("(")
} else {
bytesString += ", "
sb.WriteString(", ")
}
currentHumanize, currentSuffix := humanizeBytes(averageRate)
bytesString += fmt.Sprintf("%s%s/s", currentHumanize, currentSuffix)
sb.WriteString(fmt.Sprintf("%s%s/s", currentHumanize, currentSuffix))
}

// show iterations rate
if c.showIterationsPerSecond {
if bytesString == "" {
bytesString += "("
if sb.Len() == 0 {
sb.WriteString("(")
} else {
bytesString += ", "
sb.WriteString(", ")
}
if averageRate > 1 {
bytesString += fmt.Sprintf("%0.0f %s/s", averageRate, c.iterationString)
sb.WriteString(fmt.Sprintf("%0.0f %s/s", averageRate, c.iterationString))
} else if averageRate*60 > 1 {
bytesString += fmt.Sprintf("%0.0f %s/min", 60*averageRate, c.iterationString)
sb.WriteString(fmt.Sprintf("%0.0f %s/min", 60*averageRate, c.iterationString))
} else {
bytesString += fmt.Sprintf("%0.0f %s/hr", 3600*averageRate, c.iterationString)
sb.WriteString(fmt.Sprintf("%0.0f %s/hr", 3600*averageRate, c.iterationString))
}
}
if bytesString != "" {
bytesString += ")"
if sb.Len() != 0 {
sb.WriteString(")")
}

leftBrac, rightBrac, saucer, saucerHead := "", "", "", ""

// show time prediction in "current/total" seconds format
switch {
case c.predictTime:
Expand All @@ -805,7 +804,7 @@ func renderProgressBar(c config, s *state) (int, error) {
}
}

c.width = width - getStringWidth(c, c.description, true) - 14 - len(bytesString) - len(leftBrac) - len(rightBrac)
c.width = width - getStringWidth(c, c.description, true) - 14 - sb.Len() - len(leftBrac) - len(rightBrac)
s.currentSaucerSize = int(float64(s.currentPercent) / 100.0 * float64(c.width))
}
if s.currentSaucerSize > 0 {
Expand Down Expand Up @@ -837,41 +836,41 @@ func renderProgressBar(c config, s *state) (int, error) {
or if showDescriptionAtLineEnd is enabled
% |------ | (kb/s) (iteration count) (iteration rate) (predict time) Description
*/

repeatAmount := c.width - s.currentSaucerSize
if repeatAmount < 0 {
repeatAmount = 0
}

str := ""

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 {
if c.showDescriptionAtLineEnd {
str = fmt.Sprintf("\r%s %s [%s] %s ",
spinner,
bytesString,
sb.String(),
leftBrac,
c.description,
)
c.description)
} else {
str = fmt.Sprintf("\r%s %s %s [%s] ",
spinner,
c.description,
bytesString,
leftBrac,
)
sb.String(),
leftBrac)
}
} else {
if c.showDescriptionAtLineEnd {
str = fmt.Sprintf("\r%s %s %s ",
spinner,
bytesString,
c.description,
)
sb.String(),
c.description)
} else {
str = fmt.Sprintf("\r%s %s %s ",
spinner,
c.description,
bytesString,
)
sb.String())
}
}
} else if rightBrac == "" {
Expand All @@ -881,8 +880,7 @@ func renderProgressBar(c config, s *state) (int, error) {
saucer,
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
c.theme.BarEnd,
bytesString,
)
sb.String())

if c.showDescriptionAtLineEnd {
str = fmt.Sprintf("\r%s %s ", str, c.description)
Expand All @@ -897,8 +895,7 @@ func renderProgressBar(c config, s *state) (int, error) {
saucer,
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
c.theme.BarEnd,
bytesString,
)
sb.String())
if c.showElapsedTimeOnFinish {
str = fmt.Sprintf("%s [%s]", str, leftBrac)
}
Expand All @@ -915,10 +912,9 @@ func renderProgressBar(c config, s *state) (int, error) {
saucer,
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
c.theme.BarEnd,
bytesString,
sb.String(),
leftBrac,
rightBrac,
)
rightBrac)

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

0 comments on commit 3041da4

Please sign in to comment.