diff --git a/bar.go b/bar.go index 5d6b7dc..915be51 100644 --- a/bar.go +++ b/bar.go @@ -23,8 +23,8 @@ etaAlpha = 0.25 ) -type barFmtRunes [formatLen]rune -type barFmtBytes [formatLen][]byte +type fmtRunes [formatLen]rune +type fmtByteSegments [formatLen][]byte // Bar represents a progress Bar type Bar struct { @@ -46,7 +46,7 @@ state struct { id int width int - format barFmtRunes + format fmtRunes etaAlpha float64 total int64 current int64 @@ -354,21 +354,20 @@ var barBlock []byte buf := make([]byte, 0, termWidth) - fmtBytes := convertFmtRunesToBytes(s.format) + segments := fmtRunesToByteSegments(s.format) if s.simpleSpinner != nil { - for _, block := range [...][]byte{fmtBytes[rLeft], {s.simpleSpinner()}, fmtBytes[rRight]} { + for _, block := range [...][]byte{segments[rLeft], {s.simpleSpinner()}, segments[rRight]} { barBlock = append(barBlock, block...) } - return concatenateBlocks(buf, prependBlock, leftSpace, barBlock, rightSpace, appendBlock) - } - - barBlock = fillBar(s.total, s.current, s.width, fmtBytes, s.refill) - barCount := utf8.RuneCount(barBlock) - totalCount := prependCount + barCount + appendCount - if totalCount > termWidth { - shrinkWidth := termWidth - prependCount - appendCount - barBlock = fillBar(s.total, s.current, shrinkWidth, fmtBytes, s.refill) + } else { + barBlock = fillBar(s.total, s.current, s.width, segments, s.refill) + barCount := utf8.RuneCount(barBlock) + totalCount := prependCount + barCount + appendCount + if totalCount > termWidth { + shrinkWidth := termWidth - prependCount - appendCount + barBlock = fillBar(s.total, s.current, shrinkWidth, segments, s.refill) + } } return concatenateBlocks(buf, prependBlock, leftSpace, barBlock, rightSpace, appendBlock) @@ -381,7 +380,7 @@ return buf } -func fillBar(total, current int64, width int, fmtBytes barFmtBytes, rf *refill) []byte { +func fillBar(total, current int64, width int, fmtBytes fmtByteSegments, rf *refill) []byte { if width < 2 || total <= 0 { return []byte{} } @@ -439,14 +438,14 @@ } } -func convertFmtRunesToBytes(format barFmtRunes) barFmtBytes { - var fmtBytes barFmtBytes +func fmtRunesToByteSegments(format fmtRunes) fmtByteSegments { + var segments fmtByteSegments for i, r := range format { buf := make([]byte, utf8.RuneLen(r)) utf8.EncodeRune(buf, r) - fmtBytes[i] = buf - } - return fmtBytes + segments[i] = buf + } + return segments } func getSpinner() func() byte {