fix(ui): caret mode no longer panics under the Ascii color profile (U-P0-3)

The background-preserving reset was built by slicing lipgloss output,
which is empty without colors (NO_COLOR, dumb terminals). It is now
derived from the active termenv profile and skipped when there are no
colors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-31 23:09:23 +03:00
parent f57565989c
commit 6696fea67a
2 changed files with 64 additions and 5 deletions

View file

@ -10,6 +10,9 @@ import (
"testing"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"vcom/internal/config"
vfs "vcom/internal/fs"
"vcom/internal/testutil/teadriver"
@ -305,3 +308,43 @@ func TestUP04_NoConfirmOnlyForTrash(t *testing.T) {
t.Fatal("confirm_delete=false should move straight to the trash")
}
}
// U-P0-3: caret mode panicked under the Ascii color profile (NO_COLOR).
func TestUP03_CaretAsciiNoPanic(t *testing.T) {
profiles := map[string]termenv.Profile{"Ascii": termenv.Ascii, "ANSI": termenv.ANSI, "ANSI256": termenv.ANSI256, "TrueColor": termenv.TrueColor}
for name, profile := range profiles {
t.Run(name, func(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "notes.txt"), "hello world\nsecond line\nthird\n")
m := newTestModel(t, dir, t.TempDir())
d := teadriver.New(t, m, 160, 40, teadriver.WithProfile(profile), teadriver.WithLongCmd(300*time.Millisecond))
d.Settle(settle)
selectName(t, d, "notes.txt")
d.Press("f9") // info pane with text preview
d.Settle(settle)
d.Press("i") // caret mode
d.Settle(settle)
if !cur(t, d).cursorMode {
t.Fatalf("caret mode not entered, status %q", cur(t, d).status)
}
d.Press("j", "l", "v", "l")
if s := d.Screen(); !strings.Contains(s, "second line") {
t.Fatalf("preview text not rendered:\n%s", s)
}
})
}
}
func TestBackgroundPreservingReset(t *testing.T) {
prev := lipgloss.ColorProfile()
defer lipgloss.SetColorProfile(prev)
lipgloss.SetColorProfile(termenv.Ascii)
if _, ok := backgroundPreservingReset("#112233"); ok {
t.Fatal("Ascii profile has no background to preserve")
}
lipgloss.SetColorProfile(termenv.TrueColor)
got, ok := backgroundPreservingReset("#112233")
if !ok || got != "\x1b[39;22;23;24;59;48;2;17;34;51m" {
t.Fatalf("got %q %v", got, ok)
}
}

View file

@ -3398,14 +3398,30 @@ func (m *Model) renderTextCursorContent() string {
// foreground and text attributes, then restore the panel background
// so that gutter markers, cursor highlights, and selection highlights
// don't break the panel background for subsequent text on the line.
panelBG := lipgloss.NewStyle().Background(m.palette.Panel).Render("")
bgCode := strings.TrimSuffix(panelBG, "\x1b[0m")
inner := bgCode[2 : len(bgCode)-1]
safeReset := "\x1b[39;22;23;24;59;" + inner + "m"
result = strings.ReplaceAll(result, "\x1b[0m", safeReset)
// The background sequence comes from the active color profile instead
// of slicing lipgloss output, which is empty under the Ascii profile
// (NO_COLOR, dumb terminals) and used to panic (U-P0-3).
if safeReset, ok := backgroundPreservingReset(m.palette.Panel); ok {
result = strings.ReplaceAll(result, "\x1b[0m", safeReset)
}
return result
}
// backgroundPreservingReset returns an SGR sequence that resets foreground
// and text attributes but sets the background to bg. ok is false when the
// current profile has no colors (then a plain reset loses nothing).
func backgroundPreservingReset(bg lipgloss.Color) (string, bool) {
c := lipgloss.ColorProfile().Color(string(bg))
if c == nil {
return "", false
}
seq := c.Sequence(true)
if seq == "" {
return "", false
}
return "\x1b[39;22;23;24;59;" + seq + "m", true
}
func (m *Model) moveTextCursorWordForward() {
if !m.cursorMode {
return