chore: rename StartReturningModel to Run

Added aliases for the old functions, but deprecated them.
This commit is contained in:
Christian Muehlhaeuser 2022-10-07 23:56:12 +02:00
parent 22d15efad7
commit 6b77c8fc10
47 changed files with 66 additions and 56 deletions

View File

@ -239,7 +239,7 @@ The last step is to simply run our program. We pass our initial model to
```go ```go
func main() { func main() {
p := tea.NewProgram(initialModel()) p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err) fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -66,7 +66,7 @@ func (m model) View() string {
} }
func main() { func main() {
if err := tea.NewProgram(model{}).Start(); err != nil { if _, err := tea.NewProgram(model{}).Run(); err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -17,7 +17,7 @@ import (
func main() { func main() {
p := tea.NewProgram(initialModel()) p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -156,7 +156,7 @@ func (m *mainModel) resetSpinner() {
func main() { func main() {
p := tea.NewProgram(newModel(defaultTime)) p := tea.NewProgram(newModel(defaultTime))
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -14,7 +14,7 @@ import (
func main() { func main() {
p := tea.NewProgram(initialModel()) p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -59,7 +59,7 @@ func (m model) View() string {
} }
func main() { func main() {
if err := tea.NewProgram(model{}).Start(); err != nil { if _, err := tea.NewProgram(model{}).Run(); err != nil {
fmt.Println("uh oh:", err) fmt.Println("uh oh:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -64,7 +64,7 @@ func (m model) View() string {
func main() { func main() {
m := model{} m := model{}
if err := tea.NewProgram(m).Start(); err != nil { if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -17,7 +17,7 @@ type tickMsg time.Time
func main() { func main() {
p := tea.NewProgram(model(5), tea.WithAltScreen()) p := tea.NewProgram(model(5), tea.WithAltScreen())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -120,7 +120,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if err := tea.NewProgram(model).Start(); err != nil { if _, err := tea.NewProgram(model).Run(); err != nil {
fmt.Println("Bummer, there's been an error:", err) fmt.Println("Bummer, there's been an error:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -140,7 +140,7 @@ func main() {
} }
} }
if err := tea.NewProgram(newModel()).Start(); err != nil { if _, err := tea.NewProgram(newModel()).Run(); err != nil {
fmt.Printf("Could not start program :(\n%v\n", err) fmt.Printf("Could not start program :(\n%v\n", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -26,7 +26,7 @@ func (e errMsg) Error() string { return e.error.Error() }
func main() { func main() {
p := tea.NewProgram(model{}) p := tea.NewProgram(model{})
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -79,7 +79,7 @@ func main() {
p := tea.NewProgram(m, tea.WithAltScreen()) p := tea.NewProgram(m, tea.WithAltScreen())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -183,7 +183,7 @@ func (m model) View() string {
func main() { func main() {
rand.Seed(time.Now().UTC().UnixNano()) rand.Seed(time.Now().UTC().UnixNano())
if err := tea.NewProgram(newModel()).Start(); err != nil { if _, err := tea.NewProgram(newModel()).Run(); err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -120,7 +120,7 @@ func main() {
m := model{list: l} m := model{list: l}
if err := tea.NewProgram(m).Start(); err != nil { if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -12,7 +12,7 @@ import (
func main() { func main() {
p := tea.NewProgram(model{}, tea.WithAltScreen(), tea.WithMouseAllMotion()) p := tea.NewProgram(model{}, tea.WithAltScreen(), tea.WithMouseAllMotion())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -131,7 +131,7 @@ func max(a, b int) int {
func main() { func main() {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
if err := tea.NewProgram(newModel()).Start(); err != nil { if _, err := tea.NewProgram(newModel()).Run(); err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -141,7 +141,7 @@ func main() {
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
) )
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
fmt.Println("could not run program:", err) fmt.Println("could not run program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -70,7 +70,7 @@ func (m model) View() string {
func main() { func main() {
p := tea.NewProgram(newModel()) p := tea.NewProgram(newModel())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -45,7 +45,7 @@ func main() {
model := newModel(strings.TrimSpace(b.String())) model := newModel(strings.TrimSpace(b.String()))
if err := tea.NewProgram(model).Start(); err != nil { if _, err := tea.NewProgram(model).Run(); err != nil {
fmt.Println("Couldn't start program:", err) fmt.Println("Couldn't start program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -30,7 +30,7 @@ func main() {
progress: progress.New(progress.WithDefaultGradient()), progress: progress.New(progress.WithDefaultGradient()),
} }
if err := tea.NewProgram(m).Start(); err != nil { if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Oh no!", err) fmt.Println("Oh no!", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -100,7 +100,7 @@ func main() {
// Start the download // Start the download
go pw.Start() go pw.Start()
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
fmt.Println("error running program:", err) fmt.Println("error running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -37,7 +37,7 @@ var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")).Render
func main() { func main() {
prog := progress.New(progress.WithScaledGradient("#FF7CCB", "#FDFF8C")) prog := progress.New(progress.WithScaledGradient("#FF7CCB", "#FDFF8C"))
if err := tea.NewProgram(model{progress: prog}).Start(); err != nil { if _, err := tea.NewProgram(model{progress: prog}).Run(); err != nil {
fmt.Println("Oh no!", err) fmt.Println("Oh no!", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -85,8 +85,8 @@ func main() {
spinner: spinner.New(), spinner: spinner.New(),
}) })
if p.Start() != nil { if _, err := p.Run(); err != nil {
fmt.Println("could not start program") fmt.Println("could not start program:", err)
os.Exit(1) os.Exit(1)
} }
} }

View File

@ -72,8 +72,8 @@ func (m model) View() string {
func main() { func main() {
p := tea.NewProgram(model{}) p := tea.NewProgram(model{})
// StartReturningModel returns the model as a tea.Model. // Run returns the model as a tea.Model.
m, err := p.StartReturningModel() m, err := p.Run()
if err != nil { if err != nil {
fmt.Println("Oh no:", err) fmt.Println("Oh no:", err)
os.Exit(1) os.Exit(1)

View File

@ -117,7 +117,7 @@ func main() {
} }
}() }()
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -33,7 +33,7 @@ func (m model) View() string {
} }
func main() { func main() {
if err := tea.NewProgram(model{}).Start(); err != nil { if _, err := tea.NewProgram(model{}).Run(); err != nil {
fmt.Println("Uh oh:", err) fmt.Println("Uh oh:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -23,7 +23,7 @@ func main() {
// Initialize our program // Initialize our program
p := tea.NewProgram(model(5)) p := tea.NewProgram(model(5))
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -66,7 +66,7 @@ func (m model) View() string {
func main() { func main() {
p := tea.NewProgram(initialModel()) p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }

View File

@ -32,7 +32,7 @@ func main() {
m := model{} m := model{}
m.resetSpinner() m.resetSpinner()
if err := tea.NewProgram(m).Start(); err != nil { if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("could not run program:", err) fmt.Println("could not run program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -197,7 +197,7 @@ func (m model) View() string {
} }
func main() { func main() {
if err := tea.NewProgram(newModel(), tea.WithAltScreen()).Start(); err != nil { if _, err := tea.NewProgram(newModel(), tea.WithAltScreen()).Run(); err != nil {
fmt.Println("Error while running program:", err) fmt.Println("Error while running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -96,7 +96,7 @@ func main() {
m.keymap.start.SetEnabled(false) m.keymap.start.SetEnabled(false)
if err := tea.NewProgram(m).Start(); err != nil { if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Oh no, it didn't work:", err) fmt.Println("Oh no, it didn't work:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -177,7 +177,7 @@ func main() {
t.SetStyles(s) t.SetStyles(s)
m := model{t} m := model{t}
if err := tea.NewProgram(m).Start(); err != nil { if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -93,7 +93,7 @@ func main() {
tabs := []string{"Lip Gloss", "Blush", "Eye Shadow", "Mascara", "Foundation"} tabs := []string{"Lip Gloss", "Blush", "Eye Shadow", "Mascara", "Foundation"}
tabContent := []string{"Lip Gloss Tab", "Blush Tab", "Eye Shadow Tab", "Mascara Tab", "Foundation Tab"} tabContent := []string{"Lip Gloss Tab", "Blush Tab", "Eye Shadow Tab", "Mascara Tab", "Foundation Tab"}
m := model{Tabs: tabs, TabContent: tabContent} m := model{Tabs: tabs, TabContent: tabContent}
if err := tea.NewProgram(m).Start(); err != nil { if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -14,7 +14,7 @@ import (
func main() { func main() {
p := tea.NewProgram(initialModel()) p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -13,8 +13,7 @@ import (
func main() { func main() {
p := tea.NewProgram(initialModel()) p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
if err := p.Start(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -170,7 +170,7 @@ func (m model) View() string {
} }
func main() { func main() {
if err := tea.NewProgram(initialModel()).Start(); err != nil { if _, err := tea.NewProgram(initialModel()).Run(); err != nil {
fmt.Printf("could not start program: %s\n", err) fmt.Printf("could not start program: %s\n", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -115,7 +115,7 @@ func main() {
} }
m.keymap.start.SetEnabled(false) m.keymap.start.SetEnabled(false)
if err := tea.NewProgram(m).Start(); err != nil { if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Uh oh, we encountered an error:", err) fmt.Println("Uh oh, we encountered an error:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -45,7 +45,7 @@ func main() {
} }
p := tea.NewProgram(newModel(), opts...) p := tea.NewProgram(newModel(), opts...)
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
fmt.Println("Error starting Bubble Tea program:", err) fmt.Println("Error starting Bubble Tea program:", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -41,7 +41,7 @@ var (
func main() { func main() {
initialModel := model{0, false, 10, 0, 0, false, false} initialModel := model{0, false, 10, 0, 0, false, false}
p := tea.NewProgram(initialModel) p := tea.NewProgram(initialModel)
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
fmt.Println("could not start program:", err) fmt.Println("could not start program:", err)
} }
} }

View File

@ -38,7 +38,7 @@ func TestTeaExec(t *testing.T) {
m := &testExecModel{} m := &testExecModel{}
p := NewProgram(m, WithInput(&in), WithOutput(&buf)) p := NewProgram(m, WithInput(&in), WithOutput(&buf))
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -63,7 +63,7 @@ func WithoutCatchPanics() ProgramOption {
// Example: // Example:
// //
// p := tea.NewProgram(Model{}, tea.WithAltScreen()) // p := tea.NewProgram(Model{}, tea.WithAltScreen())
// if err := p.Start(); err != nil { // if _, err := p.Run(); err != nil {
// fmt.Println("Error running program:", err) // fmt.Println("Error running program:", err)
// os.Exit(1) // os.Exit(1)
// } // }

21
tea.go
View File

@ -335,10 +335,10 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
} }
} }
// StartReturningModel initializes the program and runs its event loops, // Run initializes the program and runs its event loops, blocking until it gets
// blocking until it gets terminated by either [Program.Quit], [Program.Kill], // terminated by either [Program.Quit], [Program.Kill], or its signal handler.
// or its signal handler. Returns the final model. // Returns the final model.
func (p *Program) StartReturningModel() (Model, error) { func (p *Program) Run() (Model, error) {
handlers := handlers{} handlers := handlers{}
cmds := make(chan Cmd) cmds := make(chan Cmd)
p.errs = make(chan error) p.errs = make(chan error)
@ -475,11 +475,22 @@ func (p *Program) StartReturningModel() (Model, error) {
return model, err return model, err
} }
// StartReturningModel initializes the program and runs its event loops,
// blocking until it gets terminated by either [Program.Quit], [Program.Kill],
// or its signal handler. Returns the final model.
//
// Deprecated: please use [Program.Run] instead.
func (p *Program) StartReturningModel() (Model, error) {
return p.Run()
}
// Start initializes the program and runs its event loops, blocking until it // Start initializes the program and runs its event loops, blocking until it
// gets terminated by either [Program.Quit], [Program.Kill], or its signal // gets terminated by either [Program.Quit], [Program.Kill], or its signal
// handler. // handler.
//
// Deprecated: please use [Program.Run] instead.
func (p *Program) Start() error { func (p *Program) Start() error {
_, err := p.StartReturningModel() _, err := p.Run()
return err return err
} }

View File

@ -34,7 +34,7 @@ func TestTeaModel(t *testing.T) {
in.Write([]byte("q")) in.Write([]byte("q"))
p := NewProgram(&testModel{}, WithInput(&in), WithOutput(&buf)) p := NewProgram(&testModel{}, WithInput(&in), WithOutput(&buf))
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -59,7 +59,7 @@ func TestTeaQuit(t *testing.T) {
} }
}() }()
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
@ -80,7 +80,7 @@ func TestTeaKill(t *testing.T) {
} }
}() }()
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }

View File

@ -200,7 +200,7 @@ The last step is to simply run our program. We pass our initial model to
```go ```go
func main() { func main() {
p := tea.NewProgram(initialModel()) p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err) fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -79,7 +79,7 @@ func (m model) View() string {
func main() { func main() {
p := tea.NewProgram(initialModel()) p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil { if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err) fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -169,7 +169,7 @@ as a `struct` with defaults.
```go ```go
func main() { func main() {
if err := tea.NewProgram(model{}).Start(); err != nil { if _, err := tea.NewProgram(model{}).Run(); err != nil {
fmt.Printf("Uh oh, there was an error: %v\n", err) fmt.Printf("Uh oh, there was an error: %v\n", err)
os.Exit(1) os.Exit(1)
} }

View File

@ -71,7 +71,7 @@ func (m model) View() string {
} }
func main() { func main() {
if err := tea.NewProgram(model{}).Start(); err != nil { if _, err := tea.NewProgram(model{}).Run(); err != nil {
fmt.Printf("Uh oh, there was an error: %v\n", err) fmt.Printf("Uh oh, there was an error: %v\n", err)
os.Exit(1) os.Exit(1)
} }