mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
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:
committed by
GitHub
parent
29254a08f1
commit
9cc3861bab
12
tea.go
12
tea.go
@@ -97,8 +97,9 @@ type Program struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
|
||||
msgs chan Msg
|
||||
errs chan error
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user