Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Can't redirect output of non-interactive application #618

Closed
tim-hilt opened this issue Dec 5, 2022 · 10 comments
Closed

Can't redirect output of non-interactive application #618

tim-hilt opened this issue Dec 5, 2022 · 10 comments

Comments

@tim-hilt
Copy link

tim-hilt commented Dec 5, 2022

I use bubbletea for the sole purpose of generating and printing a table in my application. So no interactive features there (until now!). I expected, that I could perform a benchmark-measurement with hyperfine, however it seems like execution is blocked in this case, because hyperfine redirects stdout and stderr.

Sure enough, if I redirect the output of my program to > /dev/null, then my program never exits.

Is this something I can solve by writing my application in a different way? Is this a bug with bubbletea?

@meowgorithm
Copy link
Member

@tim-hilt This doesn’t exactly answer your question, but Bubble Tea is a bit overkill for rendering a static table. What we should really do here is move the rendering portion to Lip Gloss, the style and layout library.

@tim-hilt
Copy link
Author

tim-hilt commented Dec 5, 2022

It’s overkill at the moment, but I might expand on bubbletea in the future. So you don’t have an idea of why that happens?

@bashbunni
Copy link
Member

Hey @tim-hilt we implemented a tea.ExecProcess command that uses an example of popping open your editor from Bubble Tea. It allows you to run other commands while sending Bubble Tea to the background until the command finishes. You can then pass in whatever information you need through a tea.Msg to the model to get displayed in your View.
https://github.com/charmbracelet/bubbletea/blob/6b77c8fc10d43195ab29e6e09f93272623ce4e9c/examples/exec/main.go

This is a working example, but it works with hyperfine x Bubble Tea https://gist.github.com/bashbunni/f79665e45e3ace35b344cbd6bd2442dd of course it's not perfect, but the functionality you're looking for is working here. Just did the minimal edits required from the example above

@tim-hilt
Copy link
Author

Hey @bashbunni and thanks for your response. I think what I’m looking for got lost in translation.

I have a bubbletea application that has no interactive elements. It‘s only there to print a table. I would want to time how long it takes to run the command to completion.

You can see how I start the bubbletea process here:

https://github.com/tim-hilt/tempo/blob/main/cli/tablecomponent/tablecomponent.go#L42

@bashbunni
Copy link
Member

Hey @tim-hilt, haha no problem. I'm not really understanding what you're trying to do. You've got a table being output... Are you trying to count how long it takes for the bubble tea program to start up or the duration of some other terminal command that gets triggered by some keypress after the table prints?

@tim-hilt
Copy link
Author

@bashbunni I implemented a minimal reproducable example. To run it enter the following:

git clone git@github.com:tim-hilt/bubbletea-redirection-showcasing.git && cd bubbletea-redirection-showcasing && go mod download && go run main.go

you should see the table printed to the screen. The command exits afterwards. Now try again with

go run main.go > /dev/null

you should see the command blocking forever, which is not what I'd expect. I'd expect the program to run without output and exit afterwards.

@muesli
Copy link
Member

muesli commented Jan 12, 2023

Don't use go run for this, it will interfere with the terminal communication. I haven't tried your example, but I'm fairly confident it would run fine when you actually build a binary and run it directly.

@tim-hilt
Copy link
Author

Ok, then try the following:

go build && ./bubbletea-redirection-showcasing > /dev/null

it blocks for me.

@mcastorina
Copy link

Hey @tim-hilt, I think what's happening here is your Update method is used to signal bubbletea to quit. You'll notice if you hit a key the program will exit.

Normally, there's always one message for Update on initialization to provide information on the screen size, but since we redirect I think that event isn't happening.

Changing the Init() method to return tea.Quit seems to work, though idk why it's still able to display the table when not redirecting. It seems View() is called at least once before exitting. By my crude testing, it's called twice with this solution.

@bashbunni
Copy link
Member

@mcastorina We typically do a final render before quitting. It would also do a render before executing the commands you provide. That's why in this case it would be rendering twice total

@charmbracelet charmbracelet locked and limited conversation to collaborators Feb 15, 2023
@bashbunni bashbunni converted this issue into discussion #663 Feb 15, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants