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

feat: style ranges #458

merged 4 commits into from
Jan 9, 2025

Conversation

caarlos0
Copy link
Member

@caarlos0 caarlos0 commented Jan 7, 2025

Extracted from charmbracelet/gum#789 , this allows to style ranges of a given string without breaking its current styles.

The resulting ansi sequences aren't that beautiful (as there might be many styles+reset with nothing in them), but it works. We can optimize this later I think.

Also: maybe better waiting for lipgloss v2 and do it there instead. This does not handle renderers for example.

Extracted from charmbracelet/gum#789 , this
allows to style ranges of a given string without breaking its current
styles.

The resulting ansi sequences aren't that beautiful (as there might be
many styles+reset with nothing in them), but it works. We can optimize
this later I think.
@caarlos0 caarlos0 self-assigned this Jan 7, 2025
ranges.go Outdated Show resolved Hide resolved
ranges.go Outdated Show resolved Hide resolved
ranges.go Outdated
// 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 {
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...

@caarlos0 caarlos0 merged commit 9942166 into master Jan 9, 2025
19 checks passed
@caarlos0 caarlos0 deleted the style-range branch January 9, 2025 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants