Skip to content

Commit 76030eb

Browse files
authored
deprecate text.RuneCount and upgrade testify version (#211)
1 parent 1d16aa6 commit 76030eb

File tree

13 files changed

+88
-53
lines changed

13 files changed

+88
-53
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ module github.com/jedib0t/go-pretty/v6
33
go 1.16
44

55
require (
6-
github.com/davecgh/go-spew v1.1.1 // indirect
76
github.com/mattn/go-runewidth v0.0.13
87
github.com/pkg/profile v1.6.0
9-
github.com/stretchr/testify v1.7.0
8+
github.com/stretchr/testify v1.7.4
109
golang.org/x/sys v0.0.0-20190412213103-97732733099d
1110
)

go.sum

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
1010
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
1111
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
1212
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
14-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
13+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
14+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
15+
github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM=
16+
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1517
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
1618
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1719
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1820
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
19-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
2021
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
23+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

progress/indicator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func indeterminateIndicatorMovingBackAndForth(indicator string) IndeterminateInd
9494

9595
if currentPosition == 0 {
9696
direction = 1
97-
} else if currentPosition+text.RuneCount(indicator) == maxLen {
97+
} else if currentPosition+text.RuneWidthWithoutEscSequences(indicator) == maxLen {
9898
direction = -1
9999
}
100100
nextPosition += direction
@@ -113,7 +113,7 @@ func indeterminateIndicatorMovingLeftToRight(indicator string) IndeterminateIndi
113113
currentPosition := nextPosition
114114

115115
nextPosition++
116-
if nextPosition+text.RuneCount(indicator) > maxLen {
116+
if nextPosition+text.RuneWidthWithoutEscSequences(indicator) > maxLen {
117117
nextPosition = 0
118118
}
119119

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

130130
return func(maxLen int) IndeterminateIndicator {
131131
if nextPosition == -1 {
132-
nextPosition = maxLen - text.RuneCount(indicator)
132+
nextPosition = maxLen - text.RuneWidthWithoutEscSequences(indicator)
133133
}
134134
currentPosition := nextPosition
135135
nextPosition--
@@ -165,7 +165,7 @@ func indeterminateIndicatorPacMan() IndeterminateIndicatorGenerator {
165165
if currentPosition == 0 {
166166
direction = 1
167167
indicator = pacManMovingRight
168-
} else if currentPosition+text.RuneCount(indicator) == maxLen {
168+
} else if currentPosition+text.RuneWidthWithoutEscSequences(indicator) == maxLen {
169169
direction = -1
170170
indicator = pacManMovingLeft
171171
}

progress/progress.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ func (p *Progress) initForRender() {
282282
utf8.RuneCountInString(p.style.Chars.BoxLeft) -
283283
utf8.RuneCountInString(p.style.Chars.BoxRight)
284284
p.lengthProgressOverall = p.messageWidth +
285-
text.RuneCount(p.style.Options.Separator) +
285+
text.RuneWidthWithoutEscSequences(p.style.Options.Separator) +
286286
p.lengthProgress + 1
287287
if p.style.Visibility.Percentage {
288-
p.lengthProgressOverall += text.RuneCount(fmt.Sprintf(p.style.Options.PercentFormat, 0.0))
288+
p.lengthProgressOverall += text.RuneWidthWithoutEscSequences(fmt.Sprintf(p.style.Options.PercentFormat, 0.0))
289289
}
290290

291291
// if not output write has been set, output to STDOUT

progress/render.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (p *Progress) generateTrackerStrDeterminate(value int64, total int64, maxLe
124124
} else if pFinishedDotsFraction == 0 {
125125
pInProgress = ""
126126
}
127-
pFinishedStrLen := text.RuneCount(pFinished + pInProgress)
127+
pFinishedStrLen := text.RuneWidthWithoutEscSequences(pFinished + pInProgress)
128128
if pFinishedStrLen < maxLen {
129129
pUnfinished = strings.Repeat(p.style.Chars.Unfinished, maxLen-pFinishedStrLen)
130130
}
@@ -144,8 +144,8 @@ func (p *Progress) generateTrackerStrIndeterminate(maxLen int) string {
144144
pUnfinished += strings.Repeat(p.style.Chars.Unfinished, indicator.Position)
145145
}
146146
pUnfinished += indicator.Text
147-
if text.RuneCount(pUnfinished) < maxLen {
148-
pUnfinished += strings.Repeat(p.style.Chars.Unfinished, maxLen-text.RuneCount(pUnfinished))
147+
if text.RuneWidthWithoutEscSequences(pUnfinished) < maxLen {
148+
pUnfinished += strings.Repeat(p.style.Chars.Unfinished, maxLen-text.RuneWidthWithoutEscSequences(pUnfinished))
149149
}
150150

151151
return p.style.Colors.Tracker.Sprintf("%s%s%s",
@@ -172,7 +172,7 @@ func (p *Progress) renderTracker(out *strings.Builder, t *Tracker, hint renderHi
172172
message = strings.Replace(message, "\r", "", -1)
173173
}
174174
if p.messageWidth > 0 {
175-
messageLen := text.RuneCount(message)
175+
messageLen := text.RuneWidthWithoutEscSequences(message)
176176
if messageLen < p.messageWidth {
177177
message = text.Pad(message, p.messageWidth, ' ')
178178
} else {

table/render.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (t *Table) renderLine(out *strings.Builder, row rowStr, hint renderHint) {
221221

222222
func (t *Table) renderLineMergeOutputs(out *strings.Builder, outLine *strings.Builder) {
223223
outLineStr := outLine.String()
224-
if text.RuneCount(outLineStr) > t.allowedRowLength {
224+
if text.RuneWidthWithoutEscSequences(outLineStr) > t.allowedRowLength {
225225
trimLength := t.allowedRowLength - utf8.RuneCountInString(t.style.Box.UnfinishedRow)
226226
if trimLength > 0 {
227227
out.WriteString(text.Trim(outLineStr, trimLength))
@@ -358,15 +358,15 @@ func (t *Table) renderTitle(out *strings.Builder) {
358358
rowLength = t.allowedRowLength
359359
}
360360
if t.style.Options.DrawBorder {
361-
lenBorder := rowLength - text.RuneCount(t.style.Box.TopLeft+t.style.Box.TopRight)
361+
lenBorder := rowLength - text.RuneWidthWithoutEscSequences(t.style.Box.TopLeft+t.style.Box.TopRight)
362362
out.WriteString(t.style.Box.TopLeft)
363363
out.WriteString(text.RepeatAndTrim(t.style.Box.MiddleHorizontal, lenBorder))
364364
out.WriteString(t.style.Box.TopRight)
365365
}
366366

367-
lenText := rowLength - text.RuneCount(t.style.Box.PaddingLeft+t.style.Box.PaddingRight)
367+
lenText := rowLength - text.RuneWidthWithoutEscSequences(t.style.Box.PaddingLeft+t.style.Box.PaddingRight)
368368
if t.style.Options.DrawBorder {
369-
lenText -= text.RuneCount(t.style.Box.Left + t.style.Box.Right)
369+
lenText -= text.RuneWidthWithoutEscSequences(t.style.Box.Left + t.style.Box.Right)
370370
}
371371
titleText := text.WrapText(t.title, lenText)
372372
for _, titleLine := range strings.Split(titleText, "\n") {

table/style.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ var (
358358
BottomLeft: "+",
359359
BottomRight: "+",
360360
BottomSeparator: "+",
361-
EmptySeparator: text.RepeatAndTrim(" ", text.RuneCount("+")),
361+
EmptySeparator: text.RepeatAndTrim(" ", text.RuneWidthWithoutEscSequences("+")),
362362
Left: "|",
363363
LeftSeparator: "+",
364364
MiddleHorizontal: "-",
@@ -389,7 +389,7 @@ var (
389389
BottomLeft: "┗",
390390
BottomRight: "┛",
391391
BottomSeparator: "┻",
392-
EmptySeparator: text.RepeatAndTrim(" ", text.RuneCount("╋")),
392+
EmptySeparator: text.RepeatAndTrim(" ", text.RuneWidthWithoutEscSequences("╋")),
393393
Left: "┃",
394394
LeftSeparator: "┣",
395395
MiddleHorizontal: "━",
@@ -420,7 +420,7 @@ var (
420420
BottomLeft: "╚",
421421
BottomRight: "╝",
422422
BottomSeparator: "╩",
423-
EmptySeparator: text.RepeatAndTrim(" ", text.RuneCount("╬")),
423+
EmptySeparator: text.RepeatAndTrim(" ", text.RuneWidthWithoutEscSequences("╬")),
424424
Left: "║",
425425
LeftSeparator: "╠",
426426
MiddleHorizontal: "═",
@@ -451,7 +451,7 @@ var (
451451
BottomLeft: "└",
452452
BottomRight: "┘",
453453
BottomSeparator: "┴",
454-
EmptySeparator: text.RepeatAndTrim(" ", text.RuneCount("┼")),
454+
EmptySeparator: text.RepeatAndTrim(" ", text.RuneWidthWithoutEscSequences("┼")),
455455
Left: "│",
456456
LeftSeparator: "├",
457457
MiddleHorizontal: "─",
@@ -482,7 +482,7 @@ var (
482482
BottomLeft: "╰",
483483
BottomRight: "╯",
484484
BottomSeparator: "┴",
485-
EmptySeparator: text.RepeatAndTrim(" ", text.RuneCount("┼")),
485+
EmptySeparator: text.RepeatAndTrim(" ", text.RuneWidthWithoutEscSequences("┼")),
486486
Left: "│",
487487
LeftSeparator: "├",
488488
MiddleHorizontal: "─",
@@ -513,7 +513,7 @@ var (
513513
BottomLeft: "\\",
514514
BottomRight: "/",
515515
BottomSeparator: "v",
516-
EmptySeparator: text.RepeatAndTrim(" ", text.RuneCount("+")),
516+
EmptySeparator: text.RepeatAndTrim(" ", text.RuneWidthWithoutEscSequences("+")),
517517
Left: "[",
518518
LeftSeparator: "{",
519519
MiddleHorizontal: "--",

table/table.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,9 @@ func (t *Table) getFormat(hint renderHint) text.Format {
550550

551551
func (t *Table) getMaxColumnLengthForMerging(colIdx int) int {
552552
maxColumnLength := t.maxColumnLengths[colIdx]
553-
maxColumnLength += text.RuneCount(t.style.Box.PaddingRight + t.style.Box.PaddingLeft)
553+
maxColumnLength += text.RuneWidthWithoutEscSequences(t.style.Box.PaddingRight + t.style.Box.PaddingLeft)
554554
if t.style.Options.SeparateColumns {
555-
maxColumnLength += text.RuneCount(t.style.Box.EmptySeparator)
555+
maxColumnLength += text.RuneWidthWithoutEscSequences(t.style.Box.EmptySeparator)
556556
}
557557
return maxColumnLength
558558
}
@@ -781,24 +781,24 @@ func (t *Table) initForRenderRowsStringify(rows []Row, hint renderHint) []rowStr
781781
func (t *Table) initForRenderRowSeparator() {
782782
t.maxRowLength = 0
783783
if t.autoIndex {
784-
t.maxRowLength += text.RuneCount(t.style.Box.PaddingLeft)
784+
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.PaddingLeft)
785785
t.maxRowLength += len(fmt.Sprint(len(t.rows)))
786-
t.maxRowLength += text.RuneCount(t.style.Box.PaddingRight)
786+
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.PaddingRight)
787787
if t.style.Options.SeparateColumns {
788-
t.maxRowLength += text.RuneCount(t.style.Box.MiddleSeparator)
788+
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.MiddleSeparator)
789789
}
790790
}
791791
if t.style.Options.SeparateColumns {
792-
t.maxRowLength += text.RuneCount(t.style.Box.MiddleSeparator) * (t.numColumns - 1)
792+
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.MiddleSeparator) * (t.numColumns - 1)
793793
}
794794
t.rowSeparator = make(rowStr, t.numColumns)
795795
for colIdx, maxColumnLength := range t.maxColumnLengths {
796-
maxColumnLength += text.RuneCount(t.style.Box.PaddingLeft + t.style.Box.PaddingRight)
796+
maxColumnLength += text.RuneWidthWithoutEscSequences(t.style.Box.PaddingLeft + t.style.Box.PaddingRight)
797797
t.maxRowLength += maxColumnLength
798798
t.rowSeparator[colIdx] = text.RepeatAndTrim(t.style.Box.MiddleHorizontal, maxColumnLength)
799799
}
800800
if t.style.Options.DrawBorder {
801-
t.maxRowLength += text.RuneCount(t.style.Box.Left + t.style.Box.Right)
801+
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.Left + t.style.Box.Right)
802802
}
803803
}
804804

text/align.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const (
2828
func (a Align) Apply(text string, maxLength int) string {
2929
text = a.trimString(text)
3030
sLen := utf8.RuneCountInString(text)
31-
sLenWoE := RuneCount(text)
31+
sLenWoE := RuneWidthWithoutEscSequences(text)
3232
numEscChars := sLen - sLenWoE
3333

3434
// now, align the text

text/ansi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Escape(str string, escapeSeq string) string {
3737
// StripEscape("\x1b[91mNymeria \x1b[94mGhost\x1b[0m\x1b[91m Lady\x1b[0m") == "Nymeria Ghost Lady"
3838
func StripEscape(str string) string {
3939
var out strings.Builder
40-
out.Grow(RuneCount(str))
40+
out.Grow(RuneWidthWithoutEscSequences(str))
4141

4242
isEscSeq := false
4343
for _, sChr := range str {

text/string.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func InsertEveryN(str string, runeToInsert rune, n int) string {
2727
return str
2828
}
2929

30-
sLen := RuneCount(str)
30+
sLen := RuneWidthWithoutEscSequences(str)
3131
var out strings.Builder
3232
out.Grow(sLen + (sLen / n))
3333
outLen, isEscSeq := 0, false
@@ -88,7 +88,7 @@ func LongestLineLen(str string) int {
8888
// Pad("Ghost", 7, ' ') == "Ghost "
8989
// Pad("Ghost", 10, '.') == "Ghost....."
9090
func Pad(str string, maxLen int, paddingChar rune) string {
91-
strLen := RuneCount(str)
91+
strLen := RuneWidthWithoutEscSequences(str)
9292
if strLen < maxLen {
9393
str += strings.Repeat(string(paddingChar), maxLen-strLen)
9494
}
@@ -118,7 +118,30 @@ func RepeatAndTrim(str string, maxRunes int) string {
118118
// RuneCount("Ghost") == 5
119119
// RuneCount("\x1b[33mGhost\x1b[0m") == 5
120120
// RuneCount("\x1b[33mGhost\x1b[0") == 5
121+
// Deprecated: in favor of RuneWidthWithoutEscSequences
121122
func RuneCount(str string) int {
123+
return RuneWidthWithoutEscSequences(str)
124+
}
125+
126+
// RuneWidth returns the mostly accurate character-width of the rune. This is
127+
// not 100% accurate as the character width is usually dependent on the
128+
// typeface (font) used in the console/terminal. For ex.:
129+
// RuneWidth('A') == 1
130+
// RuneWidth('ツ') == 2
131+
// RuneWidth('⊙') == 1
132+
// RuneWidth('︿') == 2
133+
// RuneWidth(0x27) == 0
134+
func RuneWidth(r rune) int {
135+
return runewidth.RuneWidth(r)
136+
}
137+
138+
// RuneWidthWithoutEscSequences is similar to RuneWidth, except for the fact
139+
// that it ignores escape sequences while counting. For ex.:
140+
// RuneWidthWithoutEscSequences("") == 0
141+
// RuneWidthWithoutEscSequences("Ghost") == 5
142+
// RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0m") == 5
143+
// RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0") == 5
144+
func RuneWidthWithoutEscSequences(str string) int {
122145
count, isEscSeq := 0, false
123146
for _, c := range str {
124147
if c == EscapeStartRune {
@@ -134,18 +157,6 @@ func RuneCount(str string) int {
134157
return count
135158
}
136159

137-
// RuneWidth returns the mostly accurate character-width of the rune. This is
138-
// not 100% accurate as the character width is usually dependant on the
139-
// typeface (font) used in the console/terminal. For ex.:
140-
// RuneWidth('A') == 1
141-
// RuneWidth('ツ') == 2
142-
// RuneWidth('⊙') == 1
143-
// RuneWidth('︿') == 2
144-
// RuneWidth(0x27) == 0
145-
func RuneWidth(r rune) int {
146-
return runewidth.RuneWidth(r)
147-
}
148-
149160
// Snip returns the given string with a fixed length. For ex.:
150161
// Snip("Ghost", 0, "~") == "Ghost"
151162
// Snip("Ghost", 1, "~") == "~"
@@ -155,9 +166,9 @@ func RuneWidth(r rune) int {
155166
// Snip("\x1b[33mGhost\x1b[0m", 7, "~") == "\x1b[33mGhost\x1b[0m "
156167
func Snip(str string, length int, snipIndicator string) string {
157168
if length > 0 {
158-
lenStr := RuneCount(str)
169+
lenStr := RuneWidthWithoutEscSequences(str)
159170
if lenStr > length {
160-
lenStrFinal := length - RuneCount(snipIndicator)
171+
lenStrFinal := length - RuneWidthWithoutEscSequences(snipIndicator)
161172
return Trim(str, lenStrFinal) + snipIndicator
162173
}
163174
}

text/string_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,28 @@ func TestRuneWidth(t *testing.T) {
174174
assert.Equal(t, 0, RuneWidth(rune(27))) // ANSI escape sequence
175175
}
176176

177+
func ExampleRuneWidthWithoutEscSequences() {
178+
fmt.Printf("RuneWidthWithoutEscSequences(\"\"): %d\n", RuneWidthWithoutEscSequences(""))
179+
fmt.Printf("RuneWidthWithoutEscSequences(\"Ghost\"): %d\n", RuneWidthWithoutEscSequences("Ghost"))
180+
fmt.Printf("RuneWidthWithoutEscSequences(\"Ghostツ\"): %d\n", RuneWidthWithoutEscSequences("Ghostツ"))
181+
fmt.Printf("RuneWidthWithoutEscSequences(\"\\x1b[33mGhost\\x1b[0m\"): %d\n", RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0m"))
182+
fmt.Printf("RuneWidthWithoutEscSequences(\"\\x1b[33mGhost\\x1b[0\"): %d\n", RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0"))
183+
184+
// Output: RuneWidthWithoutEscSequences(""): 0
185+
// RuneWidthWithoutEscSequences("Ghost"): 5
186+
// RuneWidthWithoutEscSequences("Ghostツ"): 7
187+
// RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0m"): 5
188+
// RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0"): 5
189+
}
190+
191+
func TestRuneWidthWithoutEscSequences(t *testing.T) {
192+
assert.Equal(t, 0, RuneWidthWithoutEscSequences(""))
193+
assert.Equal(t, 5, RuneWidthWithoutEscSequences("Ghost"))
194+
assert.Equal(t, 7, RuneWidthWithoutEscSequences("Ghostツ"))
195+
assert.Equal(t, 5, RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0m"))
196+
assert.Equal(t, 5, RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0"))
197+
}
198+
177199
func ExampleSnip() {
178200
fmt.Printf("Snip(\"Ghost\", 0, \"~\"): %#v\n", Snip("Ghost", 0, "~"))
179201
fmt.Printf("Snip(\"Ghost\", 1, \"~\"): %#v\n", Snip("Ghost", 1, "~"))

text/wrap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func wrapHard(paragraph string, wrapLen int, out *strings.Builder) {
201201
lineLen++
202202
}
203203

204-
wordLen := RuneCount(word)
204+
wordLen := RuneWidthWithoutEscSequences(word)
205205
if lineLen+wordLen <= wrapLen { // word fits within the line
206206
out.WriteString(word)
207207
lineLen += wordLen
@@ -227,7 +227,7 @@ func wrapSoft(paragraph string, wrapLen int, out *strings.Builder) {
227227
}
228228

229229
spacing, spacingLen := wrapSoftSpacing(lineLen)
230-
wordLen := RuneCount(word)
230+
wordLen := RuneWidthWithoutEscSequences(word)
231231
if lineLen+spacingLen+wordLen <= wrapLen { // word fits within the line
232232
out.WriteString(spacing)
233233
out.WriteString(word)

0 commit comments

Comments
 (0)