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
Provenance
- Commit
1ed623f- Tree
90a9f77- Author
Christian Muehlhaeuser <muesli@gmail.com>- Committer
Christian Muehlhaeuser <muesli@gmail.com>- Parent
fd18c14
2 files changed+20 added−15 removed
modified:tea_test.go+2 added-2 removed
90 unmodified lines91 }92 }93 }()94 95- 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 }99 100 func TestTeaBatchMsg(t *testing.T) {51 unmodified linesmodified:tea.go+18 added-13 removed
9 unmodified lines10 package tea11 12 import (13 "context"14+ "errors"15 "fmt"16 "io"17 "os"18 "os/signal"7 unmodified lines26 "github.com/muesli/termenv"27 "golang.org/x/term"28 )29 30+// ErrProgramKilled is returned by [Program.Run] when the program got killed.31+var ErrProgramKilled = errors.New("program was killed")32+33 // Msg contain data from the result of a IO operation. Msgs trigger the update34 // function and, henceforth, the UI.35 type Msg interface{}36 56 unmodified lines93 // Configuration options that will set as the program is initializing,94 // treated as bits. These options can be set via various ProgramOptions.95 startupOptions startupOptions96 93- ctx context.Context97+ ctx context.Context98+ cancel context.CancelFunc99 100 msgs chan Msg101 errs chan error102 11 unmodified lines114 // was the altscreen active before releasing the terminal?115 altScreenWasActive bool116 ignoreSignals bool117 113- killc chan bool114-118 // 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 lines139 p := &Program{140 initialModel: model,141 input: os.Stdin,142 msgs: make(chan Msg),140- killc: make(chan bool, 1),143 }144 145 // Apply all options to the program.146 for _, opt := range opts {116 unmodified lines263 // Bubble Tea messages, update the model and triggers redraws.264 func (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, nil269 270 case err := <-p.errs:271 return model, err272 70 unmodified lines343 handlers := handlers{}344 cmds := make(chan Cmd)345 p.errs = make(chan error)346 345- 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()349 350 switch {351 case p.startupOptions.has(withInputTTY):352 // Open a new TTY, by request102 unmodified lines455 handlers.add(p.handleCommands(cmds))456 457 // 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+ }463 464 // Tear down.460- cancelContext()465+ p.cancel()466 467 // Wait for input loop to finish.468 if p.cancelReader.Cancel() {469 p.waitForReadLoop()2 unmodified lines472 // Wait for all handlers to finish.473 handlers.shutdown()474 475 // Restore terminal state.471- p.shutdown(false)476+ p.shutdown(killed)477 478 return model, err479 }480 38 unmodified lines519 }520 521 // 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.524 func (p *Program) Kill() {519- p.killc <- true520- p.shutdown(true)525+ p.cancel()526 }527 528 // shutdown performs operations to free up resources and restore the terminal529 // to its original state.91 unmodified lines