Skip to content
Myr

Fix --gap-line cutting a grapheme cluster

Before adoption: this commit came in with the history the project adopted, so no landing records it.

RepeatToFill filled the remaining width rune by rune, so a cluster could be split at the right edge. Iterate grapheme clusters instead. Fix #4920

authored by Junegunn Choi · committed by Junegunn Choi · parent a140afe · Browse files · 3 files+34 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+34 added−7 removed

  • other:+34 added−7 removed

3 files+34 added−7 removed

modified:src/util/util_test.go+21 added
107 unmodified lines
108 }109 if RepeatToFill("abcde", 10, 42) != strings.Repeat("abcde", 4)+"abcde"[:2] {110 t.Error("Expected:", strings.Repeat("abcde", 4)+"abcde"[:2])111 }112113 // Should not cut a grapheme cluster in half114 for _, test := range []struct {115 str string116 limit int117 expected string118 }{119 {"a\u0301b", 1, "a\u0301"},120 {"a\u0301b", 3, "a\u0301ba\u0301"},121 {"a\u0301b", 4, "a\u0301ba\u0301b"},122 {"a\u4e00", 2, "a"},123 {"a\u4e00", 4, "a\u4e00a"},124 {"-\U0001f468\u200d\U0001f469\u200d\U0001f467", 1, "-"},125 {"-\U0001f468\u200d\U0001f469\u200d\U0001f467", 2, "-"},126 {"-\U0001f468\u200d\U0001f469\u200d\U0001f467", 4, "-\U0001f468\u200d\U0001f469\u200d\U0001f467-"},127 } {128 actual := RepeatToFill(test.str, StringWidth(test.str), test.limit)129 if actual != test.expected {130 t.Errorf("Expected: %q, actual: %q", test.expected, actual)131 }132 }133}134135func TestStringWidth(t *testing.T) {136 w := StringWidth("─")
35 unmodified lines
modified:src/util/util.go+9 added-7 removed
103 unmodified lines
104 times := limit / length105 rest := limit % length106 output := strings.Repeat(str, times)107 if rest > 0 {108 for _, r := range str {109 rest -= uniseg.StringWidth(string(r))110 if rest < 0 {108 // Iterate over grapheme clusters so that we don't cut a cluster in half109 end := 0110 graphemes := uniseg.NewGraphemes(str)111 for rest > 0 && graphemes.Next() {112 width := graphemes.Width()113 if width > rest {114 break115 }113 output += string(r)114 if rest == 0 {115 break116 }116 rest -= width117 _, end = graphemes.Positions()118 }119 output += str[:end]120 }121 return output122}123
42 unmodified lines
modified:CHANGELOG.md+4 added
1CHANGELOG2=========340.74.55------6- Fixed `--gap-line` cutting a grapheme cluster when filling the last cells of the line (#4920)780.74.49------10- Fixed an escape sequence split across reads being parsed as a fragment, which leaked the rest into the query (#4899)11 - e.g. A terminal answering the startup `DECRQM` query late left `?2004;2$y`, CTRL-UP left `5A`, and SGR mouse input left `0;1;1M`
3450 unmodified lines