huh icon indicating copy to clipboard operation
huh copied to clipboard

Be able to specify output for forms

Open connorszczepaniak-wk opened this issue 10 months ago • 7 comments

Running a form always outputs the UI to stdout: https://github.com/charmbracelet/huh/blob/main/form.go#L560

This is problematic if a program ultimately produces some output to stdout that the user wants to redirect into a file or pipe into another program.

It would be awesome if there was a way exposed to change this to Stderr, or maybe it should just default to stderr like the spinner does: https://github.com/charmbracelet/huh/blob/main/spinner/spinner.go#L144

connorszczepaniak-wk avatar Apr 11 '24 15:04 connorszczepaniak-wk

Hi! There's actually support for this on main in the form of Form.WithProgamOptions:

err := huh.NewForm(/* ... */).
	WithProgramOptions(tea.WithOutput(os.Stderr)).
	Run()

That said, we may want to smooth the API out a little into something slightly more high level (i.e. Form.WithOutput()).

meowgorithm avatar Apr 11 '24 18:04 meowgorithm

Hi! There's actually support for this on main in the form of Form.WithProgamOptions:

err := huh.NewForm(/* ... */).
	WithProgramOptions(tea.WithOutput(os.Stderr)).
	Run()

That said, we may want to smooth the API out a little into something slightly more high level (i.e. Form.WithOutput()).

Would appreciate something like that as well, thank you for providing the answer :)

survivorbat avatar Apr 13 '24 11:04 survivorbat

@meowgorithm Is there an ETA on a new cut of a release from main for this? We are blocked on this feature, primarily because there's no way to make a Form work correctly when invoked with tea.NewProgram(...) directly due to the submitCmd being private (and only being set via Form.Run()) - https://github.com/charmbracelet/huh/blob/main/form.go#L549

Our use case is using huh with a Kong as a CLI wrapper, and Kong can provide its own Stdin/Stdout buffers, but obviously they are entirely disconnected from what huh.Form does.

shakefu avatar Apr 26 '24 01:04 shakefu

@meowgorithm It would be nice to direct the output of accessible mode to the specified output. Currently accessible forms use fmt.Println() always sending to stdout. With something like Form.WithOutput() we would could use the output in accessible code with fmt.Fprintln(out, ...)

What do you think?

testinfected avatar Apr 26 '24 02:04 testinfected

Hey! Just added a WithOutput for TUI mode in #201 and will later propagate it to accessible mode!

maaslalani avatar Apr 29 '24 20:04 maaslalani

Great! Thanks

testinfected avatar Apr 29 '24 22:04 testinfected

@shakefu:

@meowgorithm Is there an ETA on a new cut of a release from main for this?

We expect to cut a release soon, but in the mean time it's totally reasonable to pin to main;

go get github.com/charmbracelet/huh@main

meowgorithm avatar Apr 30 '24 00:04 meowgorithm

Closing this as we added WithOutput and will cut a new release ASAP!

maaslalani avatar May 25 '24 17:05 maaslalani