-
Notifications
You must be signed in to change notification settings - Fork 779
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
allow custom input/output and handle eof for os.input properly #39
Conversation
f6fb5c1
to
7108b24
Compare
Thanks for the PR. This was something we've been meaning to do, so it's nice to see it happening. The functional arguments are a nice touch. I believe we also need to check and see if output is a TTY before entering raw mode, hiding the cursor and so on (see tty.go and tty_windows.go). We typically use mattn/go-isatty for this. If that sounds right to you, do you mind making that update? I'm also open for disabling input completely as an additional implementation, if that's something you're interested in doing. Just curious, what's your personal use case with this? |
That makes a lot of sense, from your description I see the following additions:
My main use case is command piping: For additional error handling we have the following options: 1.) use
2.) 3.) We could apply all options when the user calls I am going with option 3, since the error handling defer is minimal and its easier to write and read. |
Thanks for the quick, thoughtful updates — and sorry, I could have been clearer. Rather than erroring out if output's not a terminal I believe we can simply skip over I can make the change if you'd like. Either way, it would mean reverting the error handling in your last couple revisions. Let me know. |
Oh even better, this would make the implementation also more straight-forward. I'll update the PR. |
d1124ac
to
1e21f42
Compare
I updated the PR, I hope this matches your thinking. |
This looks great. Thanks for your work on this one. I'm going to test a little as well as add some comments and make a few minor stylistic changes before merging, but I'll aim to get this one in quickly. |
I'm playing with this PR a bit and I'm wondering if it's wise to disable input for your personal use case because users can't abort the application at will. If the progress bar took a long time to finish for whatever reason the user wouldn't be able exit it. Bubble Tea doesn't listen for SIGINT so ctrl+c doesn't exit by default. For cases where you're piping something in ( tty, err := os.Open("/dev/tty")
defer tty.Close()
if err != nil {
fmt.Println("could not open tty:", err)
os.Exit(1)
}
if err := tea.NewProgram(model{}, tea.WithInput(tty)).Start(); err != nil {
fmt.Println("could not run program:", err)
os.Exit(1)
} So now I'm wondering if we should be doing this for input by default so that in cases where pipes and redirection are present keyboard and mouse input just works as expected. On the other hand, after talking to my colleagues about it a bit, it probably makes more sense to simply add an option to listen for |
Thank you @meowgorithm for the great additions. I like the new interface to configure input/output. I think we should remove the sugar option 1.) err when I agree that there is a feeling that we may want to add an option for tty. At least I am not 100% sure how to write the default behavior that it makes sense for most users/command line tools. Right now, its very explicit and bubbletea stays very flexible. There are three cases that come to my mind:
In various cases it would be interesting to pass in options (eg. tty detected) to the model to render accordingly. A use case that I have in mind is to write different renderer for user input and piping. I am not sure yet if I want this to be done magic in the model or before I pass the model to bubbletea. |
Good call about removing I also agree we leave the TTY stuff in the user's hands for now, per the example. However, I will add a And yeah, to your last point, @muesli and I were talking about something along the lines of switching renderers if output isn't a TTY so that Bubble Tea programs could work as a daemon/non-TUI as well as a TUI. In that case we could probably disable the renderer altogether and let the user simply print to stdout. That's just off the top of my head though. I'm thinking we'd probably pass a special message to And thank you again for the PR! |
Signed-off-by: Christoph Hartmann <chris@lollyrock.com>
9260af5
to
2a10951
Compare
)" This reverts commit 64da3bc.
@chris-rock just a small note that we're now listening for We're planning on doing a release in a few days, which will contain the stuff in this PR, the |
That is awesome @meowgorithm Thank you |
Hey there, @chris-rock. Just a note that we've merged a PR that automatically opens a TTY for input if input is not a TTY, which means if you pipe or redirect data into a Bubble Tea application input will "just work". This works on both unix and Windows systems in well-researched, cross-platform way. All that said, if you explicitly set a custom input (per this excellent PR) Bubble Tea will respect that input and not open a TTY. |
@meowgorithm that is very cool. You guys are great. |
This PR allows the user to set a custom input and output. In addition it handles the case where the input returns an io.EOF error. There are cases eg. piping where os.Stdin may be empty when bubbletea starts.
An alternative (or even additional) implementation would be to allow rendering without reading from the input at all.
Signed-off-by: Christoph Hartmann chris@lollyrock.com