bubbletea v2.1.2 with two changes. a frame taller than the screen keeps only the rows on screen, instead of a slice that holds the tallest frame ever drawn alive. a command run with Exec hands its result to the model as the next message, before any input read once the terminal is back. upstream sends it from a goroutine, so a key typed as an editor closes could reach the model first and act on the state from before the edit. up to v2.0.10 this patch also replaced the always-on frame ticker with one that painted on demand. upstream stops its ticker after half a second without changes since v2.1.0 (#1832), so that part is gone. to bump bubbletea: copy the new release over third_party/bubbletea, drop its tests, CI files, examples, tutorials and testdata, and apply this patch (patch -p1 -d third_party/bubbletea < third_party/bubbletea.patch). --- a/cursed_renderer.go +++ b/cursed_renderer.go @@ -369,9 +369,13 @@ content.Draw(s.cellbuf, s.cellbuf.Bounds()) // If the frame height is greater than the screen height, we drop the - // lines from the top of the buffer. + // lines from the top of the buffer. The kept lines are copied: a slice of + // the tall frame would keep every line of it alive, so the buffer would + // hold the tallest frame it ever drew for as long as the program runs. if frameHeight := frameArea.Dy(); frameHeight > s.height { - s.cellbuf.Lines = s.cellbuf.Lines[frameHeight-s.height:] + kept := make([]uv.Line, s.height) + copy(kept, s.cellbuf.Lines[frameHeight-s.height:]) + s.cellbuf.Lines = kept } // Alt screen mode. --- a/exec.go +++ b/exec.go @@ -98,8 +98,18 @@ } } -// exec runs an ExecCommand and delivers the results to the program as a Msg. -func (p *Program) exec(c ExecCommand, fn ExecCallback) { +// exec runs an ExecCommand and returns its callback's Msg, or nil without a +// callback. The event loop handles that Msg before any input read after the +// terminal comes back: sent through the queue instead, a key typed right after +// the command could reach the model ahead of the command's result. +func (p *Program) exec(c ExecCommand, fn ExecCallback) Msg { + result := func(err error) Msg { + if fn == nil { + return nil + } + return fn(err) + } + // Blank the current view before releasing the terminal so the renderer's // final flush doesn't write the last View() output to stdout. Without // this, the view content leaks to the terminal and persists after the @@ -111,10 +121,7 @@ if err := p.releaseTerminal(false); err != nil { // If we can't release input, abort. - if fn != nil { - go p.Send(fn(err)) - } - return + return result(err) } c.SetStdin(p.input) @@ -124,15 +131,9 @@ // Execute system command. if err := c.Run(); err != nil { _ = p.RestoreTerminal() // also try to restore the terminal. - if fn != nil { - go p.Send(fn(err)) - } - return + return result(err) } // Have the program re-capture input. - err := p.RestoreTerminal() - if fn != nil { - go p.Send(fn(err)) - } + return result(p.RestoreTerminal()) } --- a/tea.go +++ b/tea.go @@ -783,6 +783,15 @@ case msg := <-p.msgs: msg = p.translateInputEvent(msg) + // A command that takes the terminal runs here (NB: this blocks), + // and its result is handled as this message, so input read once + // the terminal is back cannot overtake it. + if exec, ok := msg.(execMsg); ok { + if result := p.exec(exec.cmd, exec.fn); result != nil { + msg = result + } + } + if size, ok := msg.(WindowSizeMsg); ok && !isPlausibleWindowSize(size.Width, size.Height) { continue } @@ -865,10 +874,6 @@ case programStatusSupportMsg: p.executeQuery(ansi.RequestProgramStatusSupport) - - case execMsg: - // NB: this blocks. - p.exec(msg.cmd, msg.fn) case terminalVersion: p.executeQuery(ansi.RequestNameVersion)