Skip to content

Commit

Permalink
Get a more accurate estimation of the amount of space the progress ba…
Browse files Browse the repository at this point in the history
…r takes up in the terminal by counting its runes, not its characters
  • Loading branch information
CrushedPixel committed Jun 9, 2018
1 parent a859014 commit 8fd2e1b
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,22 @@ func renderProgressBar(c config, s state) (int, error) {

// the width of the string, if printed to the console
// does not include the carriage return character
stringWidth := len(str) - 1
cleanString := strings.Replace(str, "\r", "", -1)

if c.colorCodes {
// convert any color codes in the progress bar into the respective ANSI codes
str = colorstring.Color(str)

// the ANSI codes for the colors do not take up space in the console output,
// so they do not count towards the output string width
stringWidth = len(ansiRegex.ReplaceAllString(str, "")) - 1
cleanString = ansiRegex.ReplaceAllString(cleanString, "")
}

// get the amount of runes in the string instead of the
// character count of the string, as some runes span multiple characters.
// see https://stackoverflow.com/a/12668840/2733724
stringWidth := len([]rune(cleanString))

return stringWidth, writeString(c, str)
}

Expand Down

0 comments on commit 8fd2e1b

Please sign in to comment.