fix(bonfire): keep the order of lines printed above a live view and drain them before it ends
- each PrintAbove block is one bubbletea print message sent straight to the program's queue under liveMu, so the event loop prints it in call order instead of a command goroutine racing it back - runLive unregisters, sends finishMsg and waits under liveMu, so a late printer writes after the final frame and no line is lost - TestPrintAboveOrderedNoLoss pins order and no loss for spinner, progress, process and a printer racing the finish (CR-01)
This commit is contained in:
@@ -46,10 +46,18 @@ type (
|
||||
}
|
||||
// finishMsg ends a live view; err is the result of the work.
|
||||
finishMsg struct{ err error }
|
||||
// printAboveMsg is a line to print above the view.
|
||||
printAboveMsg string
|
||||
)
|
||||
|
||||
// printLine is the message that prints s, one or more lines, above the
|
||||
// running view. It is bubbletea's own Println message, sent straight to
|
||||
// the program's queue: the event loop prints it itself, in the order the
|
||||
// messages arrive. Returning tea.Println as a command instead would run
|
||||
// it on a goroutine of its own, so lines would race each other and a line
|
||||
// still in flight when the view quits would be lost.
|
||||
func printLine(s string) tea.Msg {
|
||||
return tea.Println(s)()
|
||||
}
|
||||
|
||||
// itemState is the display state of one item in the process view.
|
||||
type itemState int
|
||||
|
||||
@@ -306,24 +314,6 @@ func (m processModel) View() tea.View {
|
||||
return tea.NewView(b.String())
|
||||
}
|
||||
|
||||
// liveModel wraps a view so lines from PrintAbove print above it.
|
||||
type liveModel struct {
|
||||
inner tea.Model
|
||||
}
|
||||
|
||||
func (m liveModel) Init() tea.Cmd { return m.inner.Init() }
|
||||
|
||||
func (m liveModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if line, ok := msg.(printAboveMsg); ok {
|
||||
return m, tea.Println(string(line))
|
||||
}
|
||||
inner, cmd := m.inner.Update(msg)
|
||||
m.inner = inner
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m liveModel) View() tea.View { return m.inner.View() }
|
||||
|
||||
// The running live program, for PrintAbove. Only one live view runs at a
|
||||
// time; widgets started while one runs print plain lines.
|
||||
var (
|
||||
@@ -341,12 +331,13 @@ func liveActive() bool {
|
||||
// runLive runs model as a bubbletea program on out while work runs on the
|
||||
// calling goroutine. work gets a send function for progress messages. When
|
||||
// work returns, the program is unregistered (so PrintAbove stops sending to
|
||||
// it), told to finish and waited for; it never receives lines after that.
|
||||
// it), told to finish and waited for, all under liveMu; it never receives
|
||||
// lines after that.
|
||||
func runLive(ctx context.Context, out io.Writer, c *console, model tea.Model, work func(send func(tea.Msg)) error) error {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
prog := tea.NewProgram(liveModel{inner: model},
|
||||
prog := tea.NewProgram(model,
|
||||
tea.WithInput(nil),
|
||||
tea.WithOutput(out),
|
||||
tea.WithoutSignalHandler(),
|
||||
@@ -364,11 +355,14 @@ func runLive(ctx context.Context, out io.Writer, c *console, model tea.Model, wo
|
||||
|
||||
err := work(prog.Send)
|
||||
|
||||
// Unregister, finish and wait as one step: a printer that arrives
|
||||
// meanwhile waits for the lock and then writes after the final frame,
|
||||
// so no line reaches a stopped program or races the last frame.
|
||||
liveMu.Lock()
|
||||
activeProg = nil
|
||||
liveMu.Unlock()
|
||||
prog.Send(finishMsg{err: err})
|
||||
<-finished
|
||||
liveMu.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -380,8 +374,8 @@ func printAboveLines(s string) bool {
|
||||
if activeProg == nil {
|
||||
return false
|
||||
}
|
||||
for _, line := range strings.Split(strings.TrimSuffix(s, "\n"), "\n") {
|
||||
activeProg.Send(printAboveMsg(line))
|
||||
}
|
||||
// One message for the whole block, sent while holding liveMu: blocks
|
||||
// from different goroutines keep the order of their calls.
|
||||
activeProg.Send(printLine(strings.TrimSuffix(s, "\n")))
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user