-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package lipgloss | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/charmbracelet/x/ansi" | ||
) | ||
|
||
// StyleRanges allows to, given a string, style ranges of it differently. | ||
// The function will take into account existing styles. | ||
func StyleRanges(s string, ranges []Range) string { | ||
if len(ranges) == 0 { | ||
return s | ||
} | ||
|
||
var buf strings.Builder | ||
lastIdx := 0 | ||
stripped := ansi.Strip(s) | ||
|
||
// Use Truncate and TruncateLeft to style match.MatchedIndexes without | ||
// losing the original option style: | ||
for _, rng := range ranges { | ||
// Add the text before this match | ||
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 | ||
} | ||
|
||
// Add any remaining text after the last match | ||
if lastIdx < ansi.StringWidth(s) { | ||
buf.WriteString(ansi.TruncateLeft(s, lastIdx, "")) | ||
} | ||
|
||
return buf.String() | ||
} | ||
|
||
// NewRange returns a range that can be used with [StyleRanges]. | ||
func NewRange(start, end int, style Style) Range { | ||
return Range{start, end, style} | ||
} | ||
|
||
// Range to be used with [StyleRanges]. | ||
type Range struct { | ||
Start, End int | ||
Style Style | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package lipgloss | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/muesli/termenv" | ||
) | ||
|
||
func TestStyleRanges(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
ranges []Range | ||
expected string | ||
}{ | ||
{ | ||
name: "empty ranges", | ||
input: "hello world", | ||
ranges: []Range{}, | ||
expected: "hello world", | ||
}, | ||
{ | ||
name: "single range in middle", | ||
input: "hello world", | ||
ranges: []Range{ | ||
NewRange(6, 10, 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)), | ||
}, | ||
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)), | ||
}, | ||
expected: "\x1b[1mhello\x1b[0m \x1b[32mworld\x1b[0m", | ||
}, | ||
{ | ||
name: "style at start", | ||
input: "hello world", | ||
ranges: []Range{ | ||
NewRange(0, 4, NewStyle().Bold(true)), | ||
}, | ||
expected: "\x1b[1mhello\x1b[0m world", | ||
}, | ||
{ | ||
name: "style at end", | ||
input: "hello world", | ||
ranges: []Range{ | ||
NewRange(6, 10, 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(16, 23, NewStyle().Italic(true)), | ||
}, | ||
expected: "\x1b[1mhello\x1b[0m beautiful \x1b[3mworld\x1b[0m", | ||
}, | ||
{ | ||
name: "adjacent ranges", | ||
input: "hello world", | ||
ranges: []Range{ | ||
NewRange(0, 4, NewStyle().Bold(true)), | ||
NewRange(6, 10, NewStyle().Italic(true)), | ||
}, | ||
expected: "\x1b[1mhello\x1b[0m \x1b[3mworld\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) | ||
} | ||
}) | ||
} | ||
} |