feat: tea.Wait (#722)

* feat: tea.Wait

wait for the underlying context to finish.

extract from #352

* fix: wait til the end of shutdown

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker
2023-05-04 16:30:30 -03:00
committed by GitHub
parent 29254a08f1
commit 9cc3861bab

8
tea.go
View File

@@ -99,6 +99,7 @@ type Program struct {
msgs chan Msg
errs chan error
finished chan struct{}
// where to send output, this will usually be os.Stdout.
output *termenv.Output
@@ -366,6 +367,7 @@ func (p *Program) Run() (Model, error) {
handlers := handlers{}
cmds := make(chan Cmd)
p.errs = make(chan error)
p.finished = make(chan struct{}, 1)
defer p.cancel()
@@ -555,6 +557,11 @@ func (p *Program) Kill() {
p.cancel()
}
// Wait waits/blocks until the underlying Program finished shutting down.
func (p *Program) Wait() {
<-p.finished
}
// shutdown performs operations to free up resources and restore the terminal
// to its original state.
func (p *Program) shutdown(kill bool) {
@@ -570,6 +577,7 @@ func (p *Program) shutdown(kill bool) {
if p.restoreOutput != nil {
_ = p.restoreOutput()
}
p.finished <- struct{}{}
}
// ReleaseTerminal restores the original terminal state and cancels the input