Skip to content

Commit

Permalink
Update result example to use StartReturningModel
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Jan 11, 2022
1 parent 3a1b9fb commit d266bc1
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions examples/result/main.go
Expand Up @@ -2,8 +2,6 @@ package main

// A simple example that shows how to retrieve a value from a Bubble Tea
// program after the Bubble Tea has exited.
//
// Thanks to Treilik for this one.

import (
"fmt"
Expand All @@ -17,7 +15,7 @@ var choices = []string{"Taro", "Coffee", "Lychee"}

type model struct {
cursor int
choice chan string
choice string
}

func (m model) Init() tea.Cmd {
Expand All @@ -29,12 +27,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
close(m.choice) // If we're quitting just close the channel.
return m, tea.Quit

case "enter":
// Send the choice on the channel and exit.
m.choice <- choices[m.cursor]
m.choice = choices[m.cursor]
return m, tea.Quit

case "down", "j":
Expand Down Expand Up @@ -74,20 +71,17 @@ func (m model) View() string {
}

func main() {
// This is where we'll listen for the choice the user makes in the Bubble
// Tea program.
result := make(chan string, 1)

// Pass the channel to the initialize function so our Bubble Tea program
// can send the final choice along when the time comes.
p := tea.NewProgram(model{cursor: 0, choice: result})
if err := p.Start(); err != nil {
p := tea.NewProgram(model{})

// StartReturningModel returns the model as a tea.Model.
m, err := p.StartReturningModel()
if err != nil {
fmt.Println("Oh no:", err)
os.Exit(1)
}

// Print out the final choice.
if r := <-result; r != "" {
fmt.Printf("\n---\nYou chose %s!\n", r)
// Assert the final tea.Model to our local model and print the choice.
if m, ok := m.(model); ok && m.choice != "" {
fmt.Printf("\n---\nYou chose %s!\n", m.choice)
}
}

0 comments on commit d266bc1

Please sign in to comment.