feature suggestion: make callbacks configurable #280
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm using
huh
to make some internal tooling with neat little TUI's.In this case, we have suite of tools that are their own individual
huh
forms. These can be used as top level forms, but I've also set things up in a way where forms can compose other forms that are otherwise top level. This uses a pattern commonly referred to as Nested Tea. Some of our tools are sometimes the top level form, other times they're forms within a form and this pattern makes them super composable.It's a bit tricky, to compose them in this way, however. It often involves a line that looks sort of like this:
The tricky part is
if-statements
in the control flow of update. Missing simple statements likem.subFormFoo = nil
can cause a bad loop. When callbacks are configurable things become much easier and everything can stay in the msg/update pattern:Initializing a new form in this way is pretty straight forward
Notice that in these cases we aren't using
form.Run()
to run a form because it's undesirable to have it calltea.Quit
when that form is submitted.For this reason it isn't necessary to use the
RunWithCallbacks
function I added here. That would be used for top level forms where we want to stay in the "TEA mode" when the form is exited.