fix(renderer): flush queued view before insert above (#1741)

Co-authored-by: Drew Smirnoff <drew@charm.land>
Signed-off-by: Meng Yan <yanmxa@gmail.com>
Signed-off-by: Drew Smirnoff <drew@charm.land>
This commit is contained in:
Meng Yan 2026-11-20 23:56:54 +09:00 committed by GitHub
parent 9c6598361d
commit b681f23559
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 121 additions and 3 deletions

View file

@ -303,6 +303,11 @@ func (s *cursedRenderer) flush(closing bool) (bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.flushLocked(closing)
}
// flushLocked is [cursedRenderer.flush] for callers that hold s.mu.
func (s *cursedRenderer) flushLocked(closing bool) (bool, error) {
if !closing && s.viewStale {
return false, nil
}
@ -809,6 +814,20 @@ func (s *cursedRenderer) insertAbove(str string) error {
return nil
}
// render only queues the latest view; the frame ticker draws it later, so
// a print can arrive before the view the program just returned is on the
// terminal. Draw that view first, so the lines go above what is actually
// on screen: scrolling with the old frame's geometry can move rows of the
// old frame into the scrollback, and a view that leaves the alt screen
// hasn't left it yet, so the lines would be printed on the alt screen and
// lost. A view that enters the alt screen is left for later: until then,
// the lines are still printed on the normal screen, where they persist.
if !s.view.AltScreen {
if _, err := s.flushLocked(false); err != nil {
return fmt.Errorf("bubbletea: error flushing before insert above: %w", err)
}
}
var sb strings.Builder
w, h := s.cellbuf.Width(), s.cellbuf.Height()
_, y := s.scr.Position()

View file

@ -455,10 +455,14 @@ func TestCursedRenderer_insertAboveBeforeFirstFlush(t *testing.T) {
t.Fatal(err)
}
want := "\r\n" + ansi.CursorUp(1) +
// The one-line view is drawn first, then the line is inserted above it,
// making room for one line rather than for the terminal's height.
got := output.String()
frame := "\r" + ansi.EraseScreenBelow + "footer"
insert := "\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)
if !strings.HasSuffix(got, frame+insert) {
t.Fatalf("unexpected insertAbove output:\nwant suffix: %q\n got: %q", frame+insert, got)
}
}
@ -574,6 +578,10 @@ func TestCursedRenderer_insertAboveLineBoundaries(t *testing.T) {
var out bytes.Buffer
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, width, 2)
r.render(NewView("view\nview"))
if _, err := r.flush(false); err != nil {
t.Fatal(err)
}
out.Reset()
r.scr.SetPosition(0, 0)
if err := r.insertAbove(tt.line); err != nil {
t.Fatal(err)
@ -592,3 +600,94 @@ func TestCursedRenderer_insertAboveLineBoundaries(t *testing.T) {
})
}
}
func TestCursedRenderer_insertAboveFlushesQueuedView(t *testing.T) {
var output bytes.Buffer
r := newCursedRenderer(
&output,
[]string{"TERM=xterm-256color", "TERM_PROGRAM=Apple_Terminal"},
80,
24,
)
r.render(NewView("stale frame\nstale row"))
if _, err := r.flush(false); err != nil {
t.Fatalf("flush initial view: %v", err)
}
output.Reset()
r.render(NewView("current frame"))
if err := r.insertAbove("committed block"); err != nil {
t.Fatalf("insert above: %v", err)
}
if r.lastView == nil || r.lastView.Content != "current frame" {
t.Fatalf("insertAbove did not flush the queued view: %#v", r.lastView)
}
written := output.String()
currentAt := strings.Index(written, "current frame")
committedAt := strings.Index(written, "committed block")
if currentAt < 0 || committedAt < 0 {
t.Fatalf("output missing markers (current=%d, committed=%d): %q", currentAt, committedAt, written)
}
if currentAt > committedAt {
t.Fatalf("inserted scrollback before flushing current frame: %q", written)
}
}
// Fixes: https://github.com/charmbracelet/bubbletea/issues/1881
func TestCursedRenderer_insertAboveAfterLeavingAltScreen(t *testing.T) {
t.Parallel()
var out bytes.Buffer
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)
alt := NewView("alt screen view")
alt.AltScreen = true
r.render(alt)
if _, err := r.flush(false); err != nil {
t.Fatal(err)
}
// The update leaves the alt screen and prints a line before the next
// frame is drawn. The line must be printed on the normal screen, after
// leaving the alt screen, or it is lost with it.
r.render(NewView("inline view"))
if err := r.insertAbove("printed line"); err != nil {
t.Fatal(err)
}
if _, err := r.flush(false); err != nil {
t.Fatal(err)
}
got := out.String()
assertInOrder(t, got, ansi.SetModeAltScreenSaveCursor, ansi.ResetModeAltScreenSaveCursor, "inline view", "printed line")
if n := strings.Count(got, "printed line"); n != 1 {
t.Fatalf("expected the line to be printed once, got %d times in %q", n, got)
}
}
func TestCursedRenderer_insertAboveBeforeEnteringAltScreen(t *testing.T) {
t.Parallel()
var out bytes.Buffer
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)
r.render(NewView("inline view"))
if _, err := r.flush(false); err != nil {
t.Fatal(err)
}
// The update enters the alt screen and prints a line before the next
// frame is drawn. The line is printed on the normal screen, before
// entering the alt screen, so it's still there after leaving it.
alt := NewView("alt screen view")
alt.AltScreen = true
r.render(alt)
if err := r.insertAbove("printed line"); err != nil {
t.Fatal(err)
}
if _, err := r.flush(false); err != nil {
t.Fatal(err)
}
assertInOrder(t, out.String(), "inline view", "printed line", ansi.SetModeAltScreenSaveCursor, "alt screen view")
}