diff options
author | Yang Zhang <yang_zhang@iapcm.ac.cn> | 2018-12-31 08:53:11 +0800 |
---|---|---|
committer | Yang Zhang <yang_zhang@iapcm.ac.cn> | 2018-12-31 08:53:11 +0800 |
commit | a0ae5fc5349063fb9b0a3de04be555b3757c1f4b (patch) | |
tree | 134626c3186b0ff0493c41aded97d3525809e289 /util | |
parent | f22f9b7083ff65fb7abe00ea2fb7343a1b68c59d (diff) | |
download | git-bug-a0ae5fc5349063fb9b0a3de04be555b3757c1f4b.tar.gz |
Fix incorrect wrap of mixed wide and ascii chars
Diffstat (limited to 'util')
-rw-r--r-- | util/text/text.go | 14 | ||||
-rw-r--r-- | util/text/text_test.go | 13 |
2 files changed, 23 insertions, 4 deletions
diff --git a/util/text/text.go b/util/text/text.go index 0447bde1..f8062bc6 100644 --- a/util/text/text.go +++ b/util/text/text.go @@ -151,10 +151,18 @@ func softwrapLine(s string, w int) string { var chunks []string var word string wordType := NONE + flushWord := func() { + chunks = append(chunks, word) + word = "" + wordType = NONE + } for _, r := range []rune(newStr) { // A WIDE_CHAR itself constitutes a group. thisType := runeType(r) if thisType == WIDE_CHAR { + if wordType != NONE { + flushWord() + } chunks = append(chunks, string(r)) continue } @@ -162,7 +170,7 @@ func softwrapLine(s string, w int) string { // char with different type or end of string. if thisType != wordType { if wordType != NONE { - chunks = append(chunks, word) + flushWord() } word = string(r) wordType = thisType @@ -171,7 +179,7 @@ func softwrapLine(s string, w int) string { } } if word != "" { - chunks = append(chunks, word) + flushWord() } var line string = "" @@ -187,7 +195,7 @@ func softwrapLine(s string, w int) string { line += chunks[len(chunks)-1] chunks = chunks[:len(chunks)-1] width += wl - if width == w && len(chunks) > 0{ + if width == w && len(chunks) > 0 { line += "\n" width = 0 } diff --git a/util/text/text_test.go b/util/text/text_test.go index 480b1f1f..374ced16 100644 --- a/util/text/text_test.go +++ b/util/text/text_test.go @@ -5,7 +5,6 @@ import ( "testing" ) - func TestWrap(t *testing.T) { cases := []struct { Input, Output string @@ -102,6 +101,18 @@ func TestWrap(t *testing.T) { "一只敏捷的\x1b[31m狐\n狸跳过\x1b[0m了一只\n懒狗。", 12, }, + // Handle mixed wide and short characters + { + "敏捷 A quick 的狐狸 fox 跳过 jumps over a lazy 了一只懒狗 dog。", + "敏捷 A quick\n的狐狸 fox\n跳过 jumps\nover a lazy\n了一只懒狗\ndog。", + 12, + }, + // Handle mixed wide and short characters with color + { + "敏捷 A \x1b31mquick 的狐狸 fox 跳\x1b0m过 jumps over a lazy 了一只懒狗 dog。", + "敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。", + 12, + }, } for i, tc := range cases { |