Skip to content

Commit

Permalink
text: Wrap* should account for display width; fixes #344 (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored Dec 13, 2024
1 parent 9dfb82e commit bfe1b7c
Show file tree
Hide file tree
Showing 17 changed files with 427 additions and 278 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.17.0
golang.org/x/term v0.17.0
golang.org/x/text v0.21.0
)

require (
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand All @@ -29,6 +28,8 @@ golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
8 changes: 4 additions & 4 deletions progress/indicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func indeterminateIndicatorMovingBackAndForth(indicator string) IndeterminateInd

if currentPosition == 0 {
direction = 1
} else if currentPosition+text.RuneWidthWithoutEscSequences(indicator) == maxLen {
} else if currentPosition+text.StringWidthWithoutEscSequences(indicator) == maxLen {
direction = -1
}
nextPosition += direction
Expand All @@ -113,7 +113,7 @@ func indeterminateIndicatorMovingLeftToRight(indicator string) IndeterminateIndi
currentPosition := nextPosition

nextPosition++
if nextPosition+text.RuneWidthWithoutEscSequences(indicator) > maxLen {
if nextPosition+text.StringWidthWithoutEscSequences(indicator) > maxLen {
nextPosition = 0
}

Expand All @@ -129,7 +129,7 @@ func indeterminateIndicatorMovingRightToLeft(indicator string) IndeterminateIndi

return func(maxLen int) IndeterminateIndicator {
if nextPosition == -1 {
nextPosition = maxLen - text.RuneWidthWithoutEscSequences(indicator)
nextPosition = maxLen - text.StringWidthWithoutEscSequences(indicator)
}
currentPosition := nextPosition
nextPosition--
Expand Down Expand Up @@ -165,7 +165,7 @@ func indeterminateIndicatorPacMan() IndeterminateIndicatorGenerator {
if currentPosition == 0 {
direction = 1
indicator = pacManMovingRight
} else if currentPosition+text.RuneWidthWithoutEscSequences(indicator) == maxLen {
} else if currentPosition+text.StringWidthWithoutEscSequences(indicator) == maxLen {
direction = -1
indicator = pacManMovingLeft
}
Expand Down
8 changes: 4 additions & 4 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,13 @@ func (p *Progress) initForRender() {
// calculate length of the actual progress bar by discounting the left/right
// border/box chars
p.lengthProgress = p.lengthTracker -
text.RuneWidthWithoutEscSequences(p.style.Chars.BoxLeft) -
text.RuneWidthWithoutEscSequences(p.style.Chars.BoxRight)
text.StringWidthWithoutEscSequences(p.style.Chars.BoxLeft) -
text.StringWidthWithoutEscSequences(p.style.Chars.BoxRight)
p.lengthProgressOverall = p.lengthMessage +
text.RuneWidthWithoutEscSequences(p.style.Options.Separator) +
text.StringWidthWithoutEscSequences(p.style.Options.Separator) +
p.lengthProgress + 1
if p.style.Visibility.Percentage {
p.lengthProgressOverall += text.RuneWidthWithoutEscSequences(
p.lengthProgressOverall += text.StringWidthWithoutEscSequences(
fmt.Sprintf(p.style.Options.PercentFormat, 0.0),
)
}
Expand Down
8 changes: 4 additions & 4 deletions progress/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (p *Progress) generateTrackerStrDeterminate(value int64, total int64, maxLe
} else if pFinishedDotsFraction == 0 {
pInProgress = ""
}
pFinishedStrLen := text.RuneWidthWithoutEscSequences(pFinished + pInProgress)
pFinishedStrLen := text.StringWidthWithoutEscSequences(pFinished + pInProgress)
if pFinishedStrLen < maxLen {
pUnfinished = strings.Repeat(p.style.Chars.Unfinished, maxLen-pFinishedStrLen)
}
Expand All @@ -149,8 +149,8 @@ func (p *Progress) generateTrackerStrIndeterminate(maxLen int) string {
pUnfinished += strings.Repeat(p.style.Chars.Unfinished, indicator.Position)
}
pUnfinished += indicator.Text
if text.RuneWidthWithoutEscSequences(pUnfinished) < maxLen {
pUnfinished += strings.Repeat(p.style.Chars.Unfinished, maxLen-text.RuneWidthWithoutEscSequences(pUnfinished))
if text.StringWidthWithoutEscSequences(pUnfinished) < maxLen {
pUnfinished += strings.Repeat(p.style.Chars.Unfinished, maxLen-text.StringWidthWithoutEscSequences(pUnfinished))
}

return p.style.Colors.Tracker.Sprintf("%s%s%s",
Expand Down Expand Up @@ -197,7 +197,7 @@ func (p *Progress) renderTracker(out *strings.Builder, t *Tracker, hint renderHi
message = strings.ReplaceAll(message, "\t", " ")
message = strings.ReplaceAll(message, "\r", "") // replace with text.ProcessCRLF?
if p.lengthMessage > 0 {
messageLen := text.RuneWidthWithoutEscSequences(message)
messageLen := text.StringWidthWithoutEscSequences(message)
if messageLen < p.lengthMessage {
message = text.Pad(message, p.lengthMessage, ' ')
} else {
Expand Down
8 changes: 4 additions & 4 deletions table/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (t *Table) renderLine(out *strings.Builder, row rowStr, hint renderHint) {

func (t *Table) renderLineMergeOutputs(out *strings.Builder, outLine *strings.Builder) {
outLineStr := outLine.String()
if text.RuneWidthWithoutEscSequences(outLineStr) > t.style.Size.WidthMax {
if text.StringWidthWithoutEscSequences(outLineStr) > t.style.Size.WidthMax {
trimLength := t.style.Size.WidthMax - utf8.RuneCountInString(t.style.Box.UnfinishedRow)
if trimLength > 0 {
out.WriteString(text.Trim(outLineStr, trimLength))
Expand Down Expand Up @@ -392,15 +392,15 @@ func (t *Table) renderTitle(out *strings.Builder) {
rowLength = wm
}
if t.style.Options.DrawBorder {
lenBorder := rowLength - text.RuneWidthWithoutEscSequences(t.style.Box.TopLeft+t.style.Box.TopRight)
lenBorder := rowLength - text.StringWidthWithoutEscSequences(t.style.Box.TopLeft+t.style.Box.TopRight)
out.WriteString(colorsBorder.Sprint(t.style.Box.TopLeft))
out.WriteString(colorsBorder.Sprint(text.RepeatAndTrim(t.style.Box.MiddleHorizontal, lenBorder)))
out.WriteString(colorsBorder.Sprint(t.style.Box.TopRight))
}

lenText := rowLength - text.RuneWidthWithoutEscSequences(t.style.Box.PaddingLeft+t.style.Box.PaddingRight)
lenText := rowLength - text.StringWidthWithoutEscSequences(t.style.Box.PaddingLeft+t.style.Box.PaddingRight)
if t.style.Options.DrawBorder {
lenText -= text.RuneWidthWithoutEscSequences(t.style.Box.Left + t.style.Box.Right)
lenText -= text.StringWidthWithoutEscSequences(t.style.Box.Left + t.style.Box.Right)
}
titleText := text.WrapText(t.title, lenText)
for _, titleLine := range strings.Split(titleText, "\n") {
Expand Down
16 changes: 8 additions & 8 deletions table/render_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (t *Table) extractMaxColumnLengthsFromRow(row rowStr, mci mergedColumnIndic

func (t *Table) extractMaxColumnLengthsFromRowForMergedColumns(colIdx int, mergedColumnLength int, mci mergedColumnIndices) {
numMergedColumns := mci.len(colIdx)
mergedColumnLength -= (numMergedColumns - 1) * text.RuneWidthWithoutEscSequences(t.style.Box.MiddleSeparator)
mergedColumnLength -= (numMergedColumns - 1) * text.StringWidthWithoutEscSequences(t.style.Box.MiddleSeparator)
maxLengthSplitAcrossColumns := mergedColumnLength / numMergedColumns
if maxLengthSplitAcrossColumns > t.maxColumnLengths[colIdx] {
t.maxColumnLengths[colIdx] = maxLengthSplitAcrossColumns
Expand Down Expand Up @@ -177,22 +177,22 @@ func (t *Table) initForRenderHideColumns() {
func (t *Table) initForRenderMaxRowLength() {
t.maxRowLength = 0
if t.autoIndex {
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.PaddingLeft)
t.maxRowLength += text.StringWidthWithoutEscSequences(t.style.Box.PaddingLeft)
t.maxRowLength += len(fmt.Sprint(len(t.rows)))
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.PaddingRight)
t.maxRowLength += text.StringWidthWithoutEscSequences(t.style.Box.PaddingRight)
if t.style.Options.SeparateColumns {
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.MiddleSeparator)
t.maxRowLength += text.StringWidthWithoutEscSequences(t.style.Box.MiddleSeparator)
}
}
if t.style.Options.SeparateColumns {
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.MiddleSeparator) * (t.numColumns - 1)
t.maxRowLength += text.StringWidthWithoutEscSequences(t.style.Box.MiddleSeparator) * (t.numColumns - 1)
}
for _, maxColumnLength := range t.maxColumnLengths {
maxColumnLength += text.RuneWidthWithoutEscSequences(t.style.Box.PaddingLeft + t.style.Box.PaddingRight)
maxColumnLength += text.StringWidthWithoutEscSequences(t.style.Box.PaddingLeft + t.style.Box.PaddingRight)
t.maxRowLength += maxColumnLength
}
if t.style.Options.DrawBorder {
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.Left + t.style.Box.Right)
t.maxRowLength += text.StringWidthWithoutEscSequences(t.style.Box.Left + t.style.Box.Right)
}
}

Expand Down Expand Up @@ -282,7 +282,7 @@ func (t *Table) initForRenderRowPainterColors() {
func (t *Table) initForRenderRowSeparator() {
t.rowSeparator = make(rowStr, t.numColumns)
for colIdx, maxColumnLength := range t.maxColumnLengths {
maxColumnLength += text.RuneWidthWithoutEscSequences(t.style.Box.PaddingLeft + t.style.Box.PaddingRight)
maxColumnLength += text.StringWidthWithoutEscSequences(t.style.Box.PaddingLeft + t.style.Box.PaddingRight)
t.rowSeparator[colIdx] = text.RepeatAndTrim(t.style.Box.MiddleHorizontal, maxColumnLength)
}
}
Expand Down
Loading

0 comments on commit bfe1b7c

Please sign in to comment.