fix: preserve exact-width lines printed above the view (#1888)

Co-authored-by: Drew Smirnoff <drew@charm.land>
This commit is contained in:
Harjot Singh 2026-11-12 18:51:47 +05:30 committed by GitHub
parent 55ba0f6de3
commit bd09ae6834
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 2 deletions

View file

@ -793,7 +793,7 @@ func (s *cursedRenderer) insertAbove(str string) error {
for _, line := range lines {
lineWidth := ansi.StringWidth(line)
if w > 0 && lineWidth > w {
offset += (lineWidth / w)
offset += (lineWidth - 1) / w
}
}
@ -808,8 +808,8 @@ func (s *cursedRenderer) insertAbove(str string) error {
sb.WriteString(ansi.CursorUp(up))
sb.WriteString(ansi.InsertLine(offset))
for _, line := range lines {
sb.WriteString(line)
sb.WriteString(ansi.EraseLineRight)
sb.WriteString(line)
sb.WriteString("\r\n")
}

View file

@ -201,3 +201,46 @@ func TestCursedRenderer_updatesKittyKeyboardFlagsInPlace(t *testing.T) {
t.Fatalf("expected kitty keyboard protocol to be pushed once, got %d pushes in %q", n, got)
}
}
func TestCursedRenderer_insertAboveLineBoundaries(t *testing.T) {
t.Parallel()
const width = 40
tests := []struct {
name string
line string
rows int
}{
{name: "short", line: strings.Repeat("-", width-2) + "#", rows: 1},
{name: "exact width", line: strings.Repeat("-", width-1) + "#", rows: 1},
{name: "one cell over", line: strings.Repeat("-", width) + "#", rows: 2},
{name: "two exact rows", line: strings.Repeat("-", 2*width-1) + "#", rows: 2},
{name: "three exact rows", line: strings.Repeat("-", 3*width-1) + "#", rows: 3},
{name: "wide characters", line: strings.Repeat("界", width-1) + "-#", rows: 2},
{name: "styled", line: "\x1b[31m" + strings.Repeat("-", 2*width-1) + "#" + ansi.ResetStyle, rows: 2},
{name: "multiple lines", line: strings.Repeat("-", width-1) + "#\n" + strings.Repeat("-", 2*width-1) + "#", rows: 3},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var out bytes.Buffer
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, width, 2)
r.scr.SetPosition(0, 0)
if err := r.insertAbove(tt.line); err != nil {
t.Fatal(err)
}
got := out.String()
prefix := "\r" + ansi.CursorDown(1) + strings.Repeat("\n", tt.rows) + ansi.CursorUp(tt.rows+1) + ansi.InsertLine(tt.rows)
if !strings.HasPrefix(got, prefix) {
t.Fatalf("expected room for %d terminal rows with prefix %q, got %q", tt.rows, prefix, got)
}
for _, line := range strings.Split(tt.line, "\n") {
if ansi.StringWidth(line)%width == 0 && strings.Contains(got, line+ansi.EraseLineRight) {
t.Fatalf("erasing at the right margin would delete the inserted line's last cell, got %q", got)
}
}
})
}
}