fix(renderer): use view height before first flush (#1766)

Co-authored-by: Drew Smirnoff <drew@charm.land>
Signed-off-by: Drew Smirnoff <drew@charm.land>
This commit is contained in:
Morax 2026-11-12 21:45:21 +08:00 committed by GitHub
parent 94d45c807d
commit 6b30ecf328
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 0 deletions

View file

@ -799,6 +799,14 @@ func (s *cursedRenderer) insertAbove(str string) error {
var sb strings.Builder
w, h := s.cellbuf.Width(), s.cellbuf.Height()
_, y := s.scr.Position()
// Before the first flush, the cell buffer still has the terminal height
// rather than the height of the current inline view.
if s.lastView == nil && !s.view.AltScreen {
h = uv.NewStyledString(s.view.Content).Height()
if y < 0 {
y = 0
}
}
// We need to scroll the screen up by the number of lines in the queue.
sb.WriteByte('\r')

View file

@ -202,6 +202,24 @@ func TestCursedRenderer_updatesKittyKeyboardFlagsInPlace(t *testing.T) {
}
}
func TestCursedRenderer_insertAboveBeforeFirstFlush(t *testing.T) {
t.Parallel()
var output bytes.Buffer
renderer := newCursedRenderer(&output, []string{"TERM=xterm-256color"}, 80, 24)
renderer.render(NewView("footer"))
if err := renderer.insertAbove("startup"); err != nil {
t.Fatal(err)
}
want := "\r\n" + ansi.CursorUp(1) +
ansi.InsertLine(1) + ansi.EraseLineRight + "startup" + "\r\n"
if got := output.String(); got != want {
t.Fatalf("unexpected insertAbove output:\nwant: %q\n got: %q", want, got)
}
}
// Fixes: https://github.com/charmbracelet/bubbletea/issues/1780
func TestCursedRenderer_resizeStaleFrame(t *testing.T) {
t.Parallel()
@ -313,6 +331,7 @@ func TestCursedRenderer_insertAboveLineBoundaries(t *testing.T) {
var out bytes.Buffer
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, width, 2)
r.render(NewView("view\nview"))
r.scr.SetPosition(0, 0)
if err := r.insertAbove(tt.line); err != nil {
t.Fatal(err)