Skip to content

Commit

Permalink
fix: wide characters
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Jan 7, 2025
1 parent d1d34ec commit d5c5035
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
12 changes: 4 additions & 8 deletions ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// StyleRanges allows to, given a string, style ranges of it differently.
// The function will take into account existing styles.
// Ranges should not overlap.
func StyleRanges(s string, ranges []Range) string {
if len(ranges) == 0 {
return s
Expand All @@ -24,18 +25,13 @@ func StyleRanges(s string, ranges []Range) string {
if rng.Start > lastIdx {
buf.WriteString(ansi.Cut(s, lastIdx, rng.Start))
}
if l := len(stripped); rng.End >= l {
rng.End = l - 1
}
// Add the matched range with its highlight
buf.WriteString(rng.Style.Render(stripped[rng.Start : rng.End+1]))
lastIdx = rng.End + 1
buf.WriteString(rng.Style.Render(ansi.Cut(stripped, rng.Start, rng.End)))
lastIdx = rng.End
}

// Add any remaining text after the last match
if lastIdx < ansi.StringWidth(s) {
buf.WriteString(ansi.TruncateLeft(s, lastIdx, ""))
}
buf.WriteString(ansi.TruncateLeft(s, lastIdx, ""))

return buf.String()
}
Expand Down
30 changes: 20 additions & 10 deletions ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,48 @@ func TestStyleRanges(t *testing.T) {
name: "single range in middle",
input: "hello world",
ranges: []Range{
NewRange(6, 10, NewStyle().Bold(true)),
NewRange(6, 11, NewStyle().Bold(true)),
},
expected: "hello \x1b[1mworld\x1b[0m",
},
{
name: "multiple ranges",
input: "hello world",
ranges: []Range{
NewRange(0, 4, NewStyle().Bold(true)),
NewRange(6, 10, NewStyle().Italic(true)),
NewRange(0, 5, NewStyle().Bold(true)),
NewRange(6, 11, NewStyle().Italic(true)),
},
expected: "\x1b[1mhello\x1b[0m \x1b[3mworld\x1b[0m",
},
{
name: "overlapping with existing ANSI",
input: "hello \x1b[32mworld\x1b[0m",
ranges: []Range{
NewRange(0, 4, NewStyle().Bold(true)),
NewRange(0, 5, NewStyle().Bold(true)),
},
expected: "\x1b[1mhello\x1b[0m \x1b[32mworld\x1b[0m",
},
{
name: "style at start",
input: "hello world",
ranges: []Range{
NewRange(0, 4, NewStyle().Bold(true)),
NewRange(0, 5, NewStyle().Bold(true)),
},
expected: "\x1b[1mhello\x1b[0m world",
},
{
name: "style at end",
input: "hello world",
ranges: []Range{
NewRange(6, 10, NewStyle().Bold(true)),
NewRange(6, 11, NewStyle().Bold(true)),
},
expected: "hello \x1b[1mworld\x1b[0m",
},
{
name: "multiple styles with gap",
input: "hello beautiful world",
ranges: []Range{
NewRange(0, 4, NewStyle().Bold(true)),
NewRange(0, 5, NewStyle().Bold(true)),
NewRange(16, 23, NewStyle().Italic(true)),
},
expected: "\x1b[1mhello\x1b[0m beautiful \x1b[3mworld\x1b[0m",
Expand All @@ -73,19 +73,29 @@ func TestStyleRanges(t *testing.T) {
name: "adjacent ranges",
input: "hello world",
ranges: []Range{
NewRange(0, 4, NewStyle().Bold(true)),
NewRange(6, 10, NewStyle().Italic(true)),
NewRange(0, 5, NewStyle().Bold(true)),
NewRange(6, 11, NewStyle().Italic(true)),
},
expected: "\x1b[1mhello\x1b[0m \x1b[3mworld\x1b[0m",
},
{
name: "wide-width characters",
input: "Hello 你好 世界",
ranges: []Range{
NewRange(0, 5, NewStyle().Bold(true)), // "Hello"
NewRange(7, 10, NewStyle().Italic(true)), // "你好"
NewRange(11, 50, NewStyle().Bold(true)), // "世界"
},
expected: "\x1b[1mHello\x1b[0m \x1b[3m你好\x1b[0m \x1b[1m世界\x1b[0m",
},
}

for _, tt := range tests {
renderer.SetColorProfile(termenv.ANSI)
t.Run(tt.name, func(t *testing.T) {
result := StyleRanges(tt.input, tt.ranges)
if result != tt.expected {
t.Errorf("StyleRanges() = %q, want %q", result, tt.expected)
t.Errorf("StyleRanges()\n got = %q\nwant = %q\n", result, tt.expected)
}
})
}
Expand Down

0 comments on commit d5c5035

Please sign in to comment.