mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
tea: Add StartReturningModel()
Sometimes it is useful to obtain the final model after the program has finished. Add StartReturningModel() (Model, error), so that it is possible to obtain the final model. Make sure to keep the original Start() error function, so that everything is backwards compatible.
This commit is contained in:
committed by
Christian Rocha
parent
6dc000bdbd
commit
56aa4efb60
22
tea.go
22
tea.go
@@ -249,8 +249,8 @@ func NewProgram(model Model, opts ...ProgramOption) *Program {
|
||||
return p
|
||||
}
|
||||
|
||||
// Start initializes the program.
|
||||
func (p *Program) Start() error {
|
||||
// StartReturningModel initializes the program. Returns the final model.
|
||||
func (p *Program) StartReturningModel() (Model, error) {
|
||||
p.msgs = make(chan Msg)
|
||||
|
||||
var (
|
||||
@@ -291,7 +291,7 @@ func (p *Program) Start() error {
|
||||
// Open a new TTY, by request
|
||||
f, err := openInputTTY()
|
||||
if err != nil {
|
||||
return err
|
||||
return p.initialModel, err
|
||||
}
|
||||
|
||||
defer f.Close() // nolint:errcheck
|
||||
@@ -314,7 +314,7 @@ func (p *Program) Start() error {
|
||||
|
||||
f, err := openInputTTY()
|
||||
if err != nil {
|
||||
return err
|
||||
return p.initialModel, err
|
||||
}
|
||||
|
||||
defer f.Close() // nolint:errcheck
|
||||
@@ -355,7 +355,7 @@ func (p *Program) Start() error {
|
||||
// Check if output is a TTY before entering raw mode, hiding the cursor and
|
||||
// so on.
|
||||
if err := p.initTerminal(); err != nil {
|
||||
return err
|
||||
return p.initialModel, err
|
||||
}
|
||||
|
||||
// If no renderer is set use the standard one.
|
||||
@@ -396,7 +396,7 @@ func (p *Program) Start() error {
|
||||
|
||||
cancelReader, err := newCancelReader(p.input)
|
||||
if err != nil {
|
||||
return err
|
||||
return model, err
|
||||
}
|
||||
|
||||
defer cancelReader.Close() // nolint:errcheck
|
||||
@@ -483,8 +483,8 @@ func (p *Program) Start() error {
|
||||
cancelContext()
|
||||
waitForGoroutines(cancelReader.Cancel())
|
||||
p.shutdown(false)
|
||||
return model, err
|
||||
|
||||
return err
|
||||
case msg := <-p.msgs:
|
||||
|
||||
// Handle special internal messages.
|
||||
@@ -493,7 +493,7 @@ func (p *Program) Start() error {
|
||||
cancelContext()
|
||||
waitForGoroutines(cancelReader.Cancel())
|
||||
p.shutdown(false)
|
||||
return nil
|
||||
return model, nil
|
||||
|
||||
case batchMsg:
|
||||
for _, cmd := range msg {
|
||||
@@ -537,6 +537,12 @@ func (p *Program) Start() error {
|
||||
}
|
||||
}
|
||||
|
||||
// Start initializes the program. Ignores the final model.
|
||||
func (p *Program) Start() error {
|
||||
_, err := p.StartReturningModel()
|
||||
return err
|
||||
}
|
||||
|
||||
// Send sends a message to the main update function, effectively allowing
|
||||
// messages to be injected from outside the program for interoperability
|
||||
// purposes.
|
||||
|
||||
Reference in New Issue
Block a user