Skip to content
Myr

fix: kill should result in Start returning an error

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This fixes Kill resulting in a final nil model being returned. We can also drop the kill channel and rely on our existing context channel.

authored by Christian Muehlhaeuser · committed by Christian Muehlhaeuser · parent fd18c14 · Browse files · 2 files+20 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+20 added−15 removed

  • other:+20 added−15 removed

2 files+20 added−15 removed

modified:tea_test.go+2 added-2 removed
90 unmodified lines
91 }92 }93 }()9495 if _, err := p.Run(); err != nil {96 t.Fatal(err)95 if _, err := p.Run(); err != ErrProgramKilled {96 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)97 }98}99100func TestTeaBatchMsg(t *testing.T) {
51 unmodified lines
modified:tea.go+18 added-13 removed
9 unmodified lines
10package tea1112import (13 "context"14 "errors"15 "fmt"16 "io"17 "os"18 "os/signal"
7 unmodified lines
26 "github.com/muesli/termenv"27 "golang.org/x/term"28)2930// ErrProgramKilled is returned by [Program.Run] when the program got killed.31var ErrProgramKilled = errors.New("program was killed")3233// Msg contain data from the result of a IO operation. Msgs trigger the update34// function and, henceforth, the UI.35type Msg interface{}36
56 unmodified lines
93 // Configuration options that will set as the program is initializing,94 // treated as bits. These options can be set via various ProgramOptions.95 startupOptions startupOptions9693 ctx context.Context97 ctx context.Context98 cancel context.CancelFunc99100 msgs chan Msg101 errs chan error102
11 unmodified lines
114 // was the altscreen active before releasing the terminal?115 altScreenWasActive bool116 ignoreSignals bool117113 killc chan bool114118 // Stores the original reference to stdin for cases where input is not a119 // TTY on windows and we've automatically opened CONIN$ to receive input.120 // When the program exits this will be restored.121 //
17 unmodified lines
139 p := &Program{140 initialModel: model,141 input: os.Stdin,142 msgs: make(chan Msg),140 killc: make(chan bool, 1),143 }144145 // Apply all options to the program.146 for _, opt := range opts {
116 unmodified lines
263// Bubble Tea messages, update the model and triggers redraws.264func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {265 for {266 select {265 case <-p.killc:266 return nil, nil267 case <-p.ctx.Done():268 return model, nil269270 case err := <-p.errs:271 return model, err272
70 unmodified lines
343 handlers := handlers{}344 cmds := make(chan Cmd)345 p.errs = make(chan error)346345 var cancelContext context.CancelFunc346 p.ctx, cancelContext = context.WithCancel(context.Background())347 defer cancelContext()347 p.ctx, p.cancel = context.WithCancel(context.Background())348 defer p.cancel()349350 switch {351 case p.startupOptions.has(withInputTTY):352 // Open a new TTY, by request
102 unmodified lines
455 handlers.add(p.handleCommands(cmds))456457 // Run event loop, handle updates and draw.458 model, err := p.eventLoop(model, cmds)459 killed := p.ctx.Err() != nil460 if killed {461 err = ErrProgramKilled462 }463464 // Tear down.460 cancelContext()465 p.cancel()466467 // Wait for input loop to finish.468 if p.cancelReader.Cancel() {469 p.waitForReadLoop()
2 unmodified lines
472 // Wait for all handlers to finish.473 handlers.shutdown()474475 // Restore terminal state.471 p.shutdown(false)476 p.shutdown(killed)477478 return model, err479}480
38 unmodified lines
519}520521// Kill stops the program immediately and restores the former terminal state.522// The final render that you would normally see when quitting will be skipped.523// [program.Run] returns a [ErrProgramKilled] error.524func (p *Program) Kill() {519 p.killc <- true520 p.shutdown(true)525 p.cancel()526}527528// shutdown performs operations to free up resources and restore the terminal529// to its original state.
91 unmodified lines