Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using tea.Printf before tea.Quit #413

Closed
asdine opened this issue Aug 21, 2022 · 3 comments · Fixed by #415
Closed

Using tea.Printf before tea.Quit #413

asdine opened this issue Aug 21, 2022 · 3 comments · Fixed by #415

Comments

@asdine
Copy link

asdine commented Aug 21, 2022

it is quite difficult to use the tea.Printf and tea.Println functions right before tea.Quit.
When using it with tea.Batch and tea.Quit for example, there is a race between tea.Printf and tea.Quit.

Example:

// in this example, we are dealing with a list of tasks,
// display the latest state then start the next task.

  case foo:
     m.current++
     // if there is still work to do
     return m, tea.Batch( // this works fine
       tea.Println(m.tasks[m.current-1].View()),
       m.tasks[m.current].Update(),
     )
     // ...
     // Last element of the list, print it and leave the program
     return m, tea.Batch( // this doesn't work, race between Println and Quit
       tea.Println(m.tasks[m.current-1].View()),
       tea.Quit,
     )

By design tea.Batch doesn't provide any ordering guarantees, however its use with tea.Printf and tea.Quit makes it difficult to display all messages before quitting the program.

(If I understand correctly, the tea.Print* example has the same issue, although it misses the first entry in the list (it's more visible if you deactivate the randomness of the packages function))

I tried with tea.Sequentially but it returns the first non-nil message, this ignores the tea.Quit message.

Ideally I would like to use tea.Printf, then once it's rendered, use tea.Quit. Example:

  return m, tea.QuitAfter(tea.Printf("...."))

What is the proper way of using tea.Printf and then tea.Quit?

@meowgorithm
Copy link
Member

This is a really good catch and, unfortunately, there's no solution yet. It may actually be possible to adjust tea.Sequentially internally so that it works commands that return non-nil messages. If that doesn't work I'll return with some other possible solutions.

meowgorithm added a commit that referenced this issue Aug 24, 2022
meowgorithm added a commit that referenced this issue Aug 24, 2022
@meowgorithm
Copy link
Member

meowgorithm commented Aug 24, 2022

Alright, so it still needs to be vetted, but I just pushed a sequence branch which introduces the Sequence command that allows you to run commands in order.

cmd := tea.Sequence(tea.Println("A"), tea.Println("B"), tea.Println("C"), tea.Quit)

Full example here. Would love hear what you think.

@asdine
Copy link
Author

asdine commented Aug 25, 2022

It works great! I managed to run tea.Println and tea.Quit sequentially with no issue.

meowgorithm added a commit that referenced this issue Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants