charmbracelet / huh

Build terminal forms and prompts 🤷🏻‍♀️
MIT License
3.72k stars 94 forks source link

Add `WithInput` to specify input stream #270

Closed survivorbat closed 1 month ago

survivorbat commented 1 month ago

Is your feature request related to a problem? Please describe.

I'm a unit-test freak and I'm currently using the following construct to unit-test my cobra commands with huh:

err := form.WithProgramOptions(tea.WithInput(in), tea.WithOutput(out)).Run()

In which my tests will supply answers to the input stream based on the output received in the output stream. An answering machine, if you will.

Recently, form.WithOutput(out) was introduced which I'm grateful for, but I'd love to have an WithInput as well.

Describe the solution you'd like

I'd like WithInput(in io.Reader) to be added to the form objects to avoid having to use tea directly. Perhaps you could even introduce a piece of documentation on how to use it with 2 io.Pipe's to simulate user input based on questions in the output.

Describe alternatives you've considered

I'll keep using the above mentioned workaround.

Additional context

Related: #187