Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit 3f9140e

Browse files
Fix bug when runes with width > 1 in an option with multiple lines (#445)
* Fix bug when runes with width > 1 in an option with multiple lines * add test Co-authored-by: Alec Aivazis <[email protected]>
1 parent d889203 commit 3f9140e

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

renderer.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package survey
33
import (
44
"bytes"
55
"fmt"
6-
"unicode/utf8"
7-
86
"github.com/AlecAivazis/survey/v2/core"
97
"github.com/AlecAivazis/survey/v2/terminal"
108
"golang.org/x/term"
@@ -180,7 +178,8 @@ func (r *Renderer) countLines(buf bytes.Buffer) int {
180178
delim = len(bufBytes) // no new line found, read rest of text
181179
}
182180

183-
if lineWidth := utf8.RuneCount(bufBytes[curr:delim]); lineWidth > w {
181+
str := string(bufBytes[curr:delim])
182+
if lineWidth := terminal.StringWidth(str); lineWidth > w {
184183
// account for word wrapping
185184
count += lineWidth / w
186185
if (lineWidth % w) == 0 {

terminal/runereader.go

+9
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,12 @@ func runeWidth(r rune) int {
384384
}
385385
return 1
386386
}
387+
388+
func StringWidth(str string) int {
389+
w := 0
390+
rs := []rune(str)
391+
for _, r := range rs {
392+
w += runeWidth(r)
393+
}
394+
return w
395+
}

tests/input.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"github.com/AlecAivazis/survey/v2"
5-
"github.com/AlecAivazis/survey/v2/tests/util"
5+
TestUtil "github.com/AlecAivazis/survey/v2/tests/util"
66
)
77

88
var val = ""
@@ -23,6 +23,8 @@ var table = []TestUtil.TestTableEntry{
2323
"Delete and forward delete test at random location (test if screen overflows)", &survey.Input{Message: "Hello world"}, &val, nil,
2424
}, {
2525
"Moving around lines with left & right arrow keys", &survey.Input{Message: "Hello world"}, &val, nil,
26+
}, {
27+
"Runes with width > 1. Enter 一 you get to the next line", &survey.Input{Message: "Hello world"}, &val, nil,
2628
},
2729
}
2830

0 commit comments

Comments
 (0)