Skip to content

Commit

Permalink
chore: review
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Jan 9, 2025
1 parent 9354b64 commit 1c3ba1b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 90 deletions.
16 changes: 1 addition & 15 deletions ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,10 @@ import (
"github.com/charmbracelet/x/ansi"
)

// StyleRanges allows to, given a string, style a range of it differently.
// The function will take into account existing styles.
// See [StyleRanges] to style multipe ranges in the same string.
func StyleRange(s string, start, end int, style Style) string {
if end == 0 {
return s
}
return StyleRanges(s, []Range{{
start,
end,
style,
}})
}

// 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 {
func StyleRanges(s string, ranges ...Range) string {
if len(ranges) == 0 {
return s
}
Expand Down
76 changes: 1 addition & 75 deletions ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,6 @@ import (
"github.com/muesli/termenv"
)

func TestStyleRange(t *testing.T) {
tests := []struct {
name string
input string
rng Range
expected string
}{
{
name: "empty ranges",
input: "hello world",
rng: Range{},
expected: "hello world",
},
{
name: "single range in middle",
input: "hello world",
rng: NewRange(6, 11, NewStyle().Bold(true)),
expected: "hello \x1b[1mworld\x1b[0m",
},
{
name: "multiple ranges",
input: "hello world",
rng: NewRange(0, 5, NewStyle().Bold(true)),
expected: "\x1b[1mhello\x1b[0m world",
},
{
name: "overlapping with existing ANSI",
input: "hello \x1b[32mworld\x1b[0m",
rng: NewRange(0, 5, NewStyle().Bold(true)),
expected: "\x1b[1mhello\x1b[0m \x1b[32mworld\x1b[0m",
},
{
name: "style at start",
input: "hello world",
rng: NewRange(0, 5, NewStyle().Bold(true)),
expected: "\x1b[1mhello\x1b[0m world",
},
{
name: "style at end",
input: "hello world",
rng: NewRange(6, 11, NewStyle().Bold(true)),
expected: "hello \x1b[1mworld\x1b[0m",
},
{
name: "multiple styles with gap",
input: "hello beautiful world",
rng: NewRange(0, 5, NewStyle().Bold(true)),
expected: "\x1b[1mhello\x1b[0m beautiful world",
},
{
name: "adjacent ranges",
input: "hello world",
rng: NewRange(6, 11, NewStyle().Italic(true)),
expected: "hello \x1b[3mworld\x1b[0m",
},
{
name: "wide-width characters",
input: "Hello 你好 世界",
rng: NewRange(11, 50, NewStyle().Bold(true)), // "世界"
expected: "Hello 你好 \x1b[1m世界\x1b[0m",
},
}

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

func TestStyleRanges(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -167,7 +93,7 @@ func TestStyleRanges(t *testing.T) {
for _, tt := range tests {
renderer.SetColorProfile(termenv.ANSI)
t.Run(tt.name, func(t *testing.T) {
result := StyleRanges(tt.input, tt.ranges)
result := StyleRanges(tt.input, tt.ranges...)
if result != tt.expected {
t.Errorf("StyleRanges()\n got = %q\nwant = %q\n", result, tt.expected)
}
Expand Down

0 comments on commit 1c3ba1b

Please sign in to comment.