fix(render): terminate CSI at its final byte and reset styles after truncation

TruncateVisual ended escape mode at the first byte in 0x40–0x7e, but the CSI
introducer '[' (0x5b) is itself in that range — real SGR sequences dropped out
of escape mode immediately and their parameter bytes counted as visible text.
Masked in tests because lipgloss strips color off-TTY; now covered with raw
ANSI input.

Also re-emit a reset when truncation cuts a styled string before its closing
reset, so the ellipsis and following text don't inherit the open style.
This commit is contained in:
Aleks Petrov committed 2026-07-07 19:51:12 +02:00
1 parent fa49a2b462
commit bd3cab4dd8
2 files changed
+23 -4

No files matched your search

+11 -1
View File
@@ -57,11 +57,21 @@ func TestPadOrTruncate(t *testing.T) {
}
func TestTruncateVisualStyled(t *testing.T) {
styled := lipgloss.NewStyle().Foreground(lipgloss.Color("#7eb8da")).Render("styled long content here")
// Raw ANSI rather than lipgloss.Render: lipgloss strips color when stdout
// is not a TTY (test runs), which would make this test vacuous.
styled := "\x1b[38;2;126;184;218mstyled long content here\x1b[0m"
out := TruncateVisual(styled, 12)
if got := lipgloss.Width(out); got != 12 {
t.Errorf("styled truncate: width %d, want 12", got)
}
// Truncation cuts before the string's own closing reset — the result must
// re-emit one so the ellipsis and following text don't inherit the style.
if !strings.Contains(out, "\x1b[0m") {
t.Errorf("styled truncate leaks open style, no reset: %q", out)
}
if !strings.HasSuffix(out, "...") {
t.Errorf("styled truncate missing ellipsis: %q", out)
}
}
func TestNormalizeSparkline(t *testing.T) {
+12 -3
View File
@@ -26,8 +26,9 @@ func PadOrTruncate(s string, targetWidth int) string {
// TruncateVisual truncates s to targetWidth visual cells, appending "..." when
// truncation occurs. ANSI escape sequences (lipgloss color codes) are copied
// through with zero visible width; a CSI sequence starts at ESC (0x1b) and
// ends at a byte in 0x40–0x7e.
// through with zero visible width; a CSI sequence starts at ESC (0x1b), and
// ends at a byte in 0x40–0x7e — excluding the '[' introducer itself, which is
// in that range but marks the start of the parameter bytes.
func TruncateVisual(s string, targetWidth int) string {
if targetWidth <= 0 {
return ""
@@ -43,16 +44,18 @@ func TruncateVisual(s string, targetWidth int) string {
result := ""
width := 0
inEsc := false
sawEsc := false
for _, r := range s {
if inEsc {
result += string(r)
if r >= 0x40 && r <= 0x7e {
if r != '[' && r >= 0x40 && r <= 0x7e {
inEsc = false
}
continue
}
if r == 0x1b {
inEsc = true
sawEsc = true
result += string(r)
continue
}
@@ -64,6 +67,12 @@ func TruncateVisual(s string, targetWidth int) string {
width += runeWidth
}
// Truncation may cut the string before its closing reset — emit one so the
// ellipsis, padding, and whatever renders next don't inherit an open style.
if sawEsc {
result += "\x1b[0m"
}
for width < targetWidth-3 {
result += " "
width++