Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: style ranges #458

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
"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.

Check failure on line 11 in ranges.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

`multipe` is a misspelling of `multiple` (misspell)

Check failure on line 11 in ranges.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

`multipe` is a misspelling of `multiple` (misspell)

Check failure on line 11 in ranges.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

`multipe` is a misspelling of `multiple` (misspell)

Check failure on line 11 in ranges.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

`multipe` is a misspelling of `multiple` (misspell)

Check failure on line 11 in ranges.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

`multipe` is a misspelling of `multiple` (misspell)

Check failure on line 11 in ranges.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

`multipe` is a misspelling of `multiple` (misspell)
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
func StyleRange(s string, start, end int, style Style) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, maybe we could do StyleRanges(s, NewRange(start, end, style)) where func StyleRanges(s string, ranges ...Range) string 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sounds good to me as well :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, great call @aymanbagabas. Part of me thinks it should be singular, though, just so users don't think it must take multiple values 🤔. But maybe I'm overthinking it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw I'm more inclined into using plural, as it indicates one or more, whereas singular indicates only one...

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.
Expand Down
74 changes: 74 additions & 0 deletions ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,80 @@ 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
Loading