Git Product home page Git Product logo

Comments (4)

meowgorithm avatar meowgorithm commented on July 17, 2024 1

After thinking about this some more, I think it makes the most sense to send final results back with a channel. Here's an example.

from bubbletea.

meowgorithm avatar meowgorithm commented on July 17, 2024

Hi! So if I understand correctly, you want to capture the output of a choice, exit Bubble Tea, and then do something with that value?

from bubbletea.

abtris avatar abtris commented on July 17, 2024

Yes, that is simple example of usage.

From interface perspective I think if you have possibility to extend interface func NewProgram(init Init, update Update, view View) *Program to some way that you have access to model via instance of program to process Model outside that will be useful for people using tool for example as input to process.

What I can show download?
- [ ] nodejs 
- [ ] ruby
- [ ] go

press q to quit/continue

and as output will be:

You select nodejs and I'm downloading latest nodejs from ...

in func main can be for example:

func main() {
    p := tea.NewProgram(initialize, update, view)
    if err := p.Start(); err != nil {
        fmt.Printf("Alas, there's been an error: %v", err)
        os.Exit(1)
    } else {
      m := p.GetModel() // whatever make sense
      makeDownload(m.url)
    }
    
}

or

func main() {
    p, model := tea.NewProgram(initialize, update, view)
    if err := p.Start(); err != nil {
        fmt.Printf("Alas, there's been an error: %v", err)
        os.Exit(1)
    } else {     
      makeDownload(model.url)
    }
    
}

from bubbletea.

meowgorithm avatar meowgorithm commented on July 17, 2024

That's a good idea. Since Program.Start() blocks we could potentially do something like:

func main() {
    finalModel, err := tea.NewProgram(myModel).Start()
    if err != nil {
        fmt.Printf("Oh no!: %v\n", err)
        os.Exit(1)
    }

    m, ok := finalModel.(myModel)
    if ok {
        doSomething(model.url)
    }
}

Because the Bubble Tea runtime only knows the type as tea.Model you'd need to assert the model to whatever type you're actually using for the model (same as you'd do in the update function).

Depending on what the rest of your program looks like you could also just perform I/O in a command, which is typically how you’d do I/O in Bubble Tea (quick writeup here incase you haven’t seen it). Something like:

func (m MyModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    // ...
    switch msg := msg.(type) {
    case tea.KeyMsg:
        switch msg.Type {
        case tea.KeyEnter:
            m.note = "Now downloading!"
            return m, makeDownload(m.url)
        }
    // ...
    }
}

func makeDownload(url string) tea.Cmd {
    return func() tea.Msg {
        // ... download logic
       return downloadStatusMsg{err}
    }
}

But yes, that’s definitely more involved if the goal is to simply use the library to quickly capture some input.

from bubbletea.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.