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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

feat: style ranges #458

wants to merge 3 commits into from

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
@@ -6,6 +6,20 @@ 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.
Copy link
Member

Choose a reason for hiding this comment

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

Ugh why do I need to comment before making a code suggestion?

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// See [StyleRanges] to style multipe ranges in the same string.
// See [StyleRanges] to style multiple ranges in the same string.

// 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 🤔

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