fix(renderer): don't trim final newline (because it can be intentional)

This commit is contained in:
Christian Rocha 2026-09-09 15:34:39 -04:00 committed by drew
parent d5bfd5c2ff
commit 4e99467d8b
No known key found for this signature in database
GPG key ID: 877E0EA9036E900E
13 changed files with 105 additions and 20 deletions

View file

@ -514,16 +514,28 @@ func (s *cursedRenderer) flush(closing bool) error {
// cursor position might get updated during rendering.
s.scr.MoveTo(view.Cursor.X, view.Cursor.Y)
} else if !view.AltScreen {
// We don't want the cursor to be dangling at the end of the line in
// inline mode because it can cause unwanted line wraps in some
// terminals. So we move it to the beginning of the next line if
// necessary.
// This is only needed when the cursor is hidden because when it's
// visible, we already set its position above.
x, y := s.scr.Position()
if x >= s.width-1 {
s.scr.MoveTo(0, y)
// Park the cursor at the beginning of the frame's last line. The
// cursor is hidden in this branch (a visible cursor is positioned
// above), so parking it never moves anything the user can see, and
// like before, it keeps the cursor from dangling at the end of the
// last drawn line, which can cause unwanted line wraps in some
// terminals.
//
// The frame's height counts the view's trailing blank lines, so
// parking here also commits the renderer to them: a frame ending
// in intentional blank lines keeps its bottom margin, and output
// printed after the program exits starts below the frame rather
// than on its last visible line. Frames taller than the screen
// have their top rows dropped, so their last visible row is the
// screen's.
y := frameArea.Dy() - 1
if y >= s.height {
y = s.height - 1
}
if y < 0 {
y = 0
}
s.scr.MoveTo(0, y)
}
if err := s.scr.Flush(); err != nil {

View file

@ -95,6 +95,79 @@ func assertInOrder(t *testing.T, got string, wants ...string) {
}
}
// Views ending in blank lines should keep them: the frame's height counts
// the view's trailing blank lines, and the cursor must be parked below the
// frame after rendering. That way the blank lines survive the closing
// erase, and output printed after the program exits starts below the frame
// instead of on its last visible line.
func TestCursedRenderer_trailingBlankLines(t *testing.T) {
t.Parallel()
var out bytes.Buffer
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)
r.start()
// A 3-line frame: one visible line, two intentional blank lines. The
// cursor must be parked on the third row (0-indexed row 2).
r.render(NewView("hello\n\n"))
if err := r.flush(false); err != nil {
t.Fatal(err)
}
if x, y := r.scr.Position(); x != 0 || y != 2 {
t.Fatalf("cursor parked at (%d, %d), want (0, 2)", x, y)
}
// The parked position must survive a repaint that redraws the same
// content (e.g. after a suspend/resize clears the buffer), so the
// frame keeps its trailing blank lines across flushes.
r.pendingErase = true
r.render(NewView("hello\n\n"))
if err := r.flush(false); err != nil {
t.Fatal(err)
}
if x, y := r.scr.Position(); x != 0 || y != 2 {
t.Fatalf("cursor parked at (%d, %d) after repaint, want (0, 2)", x, y)
}
// Frames without trailing blank lines park the cursor at the start of
// their last line, matching the previous renderer's behavior.
r.render(NewView("hello"))
if err := r.flush(false); err != nil {
t.Fatal(err)
}
if x, y := r.scr.Position(); x != 0 || y != 0 {
t.Fatalf("cursor parked at (%d, %d), want (0, 0)", x, y)
}
if err := r.close(); err != nil {
t.Fatal(err)
}
}
// Frames taller than the screen drop their top rows, so the parked cursor
// must land on the screen's last visible row, not beyond it.
func TestCursedRenderer_trailingBlankLinesTallFrame(t *testing.T) {
t.Parallel()
var out bytes.Buffer
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)
r.start()
// A 30-line frame (26 content lines + 4 trailing blank lines) inside a
// 24-row terminal. The renderer drops the top 6 rows, leaving the
// screen's last visible row as the frame's last row.
content := strings.Repeat("line\n", 30)
r.render(NewView(content))
if err := r.flush(false); err != nil {
t.Fatal(err)
}
if x, y := r.scr.Position(); x != 0 || y != 23 {
t.Fatalf("cursor parked at (%d, %d), want (0, 23)", x, y)
}
if err := r.close(); err != nil {
t.Fatal(err)
}
}
func TestCursedRenderer_restoresKittyKeyboardStack(t *testing.T) {
t.Parallel()

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p]10;?]11;?]12;?
[?25l[?2004h[>4;2m[>1u success [>4m[<1u[?25h[?2004l[?2026$p[?2027$p]10;?]11;?]12;?

View file

@ -1 +1 @@
[?25l [?2004h[>4;2m[>1usuccess[>4m[<1u [?25h[?2004l[?2026$p[?2027$p
[?25l [?2004h[>4;2m[>1usuccess [>4m[<1u[?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p]52;c;?]52;c;c3VjY2Vzcw==
[?25l[?2004h[>4;2m[>1u success [>4m[<1u[?25h[?2004l[?2026$p[?2027$p]52;c;?]52;c;c3VjY2Vzcw==

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>1u success [>4m[<1u[?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[>1u]11;#ffffff success[>4m[<1u [?25h[?2004l]111[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>1u]11;#ffffff success [>4m[<1u[?25h[?2004l]111[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>1u success [>4m[<1u[?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>1u success [>4m[<1u[?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[>3u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>3u success [>4m[<1u[?25h[?2004l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[?1003h[?1006h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$p
[?25l[?2004h[?1003h[?1006h[>4;2m[>1u success [>4m[<1u[?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[?1002h[?1006h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$p
[?25l[?2004h[?1002h[?1006h[>4;2m[>1u success [>4m[<1u[?25h[?2004l[?1002l[?1003l[?1006l[?2026$p[?2027$p

View file

@ -1 +1 @@
[?25l[?2004h[>4;2m[>1u success[>4m[<1u [?25h[?2004l[?2026$p[?2027$p
[?25l[?2004h[>4;2m[>1u success [>4m[<1u[?25h[?2004l[?2026$p[?2027$p