Skip to content
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

[tuify] create a new binary that allows strings to be passed in and the selection is returned as an int (index of what the user chose) #364

Open
nazmulidris opened this issue Nov 1, 2024 · 0 comments

Comments

@nazmulidris
Copy link
Member

nazmulidris commented Nov 1, 2024

Use case

  • A script runs and it needs the user to choose between 3 choices.
  • These choices are all in the form of strings that are surrounded by quotes.
  • The choice of the user can be in the form of the index of the string array (0..length-1), or -1, meaning user chose nothing (exited).
  • The binary name for this is rtc, since there's already a binary called rt that handles a different use case.

Example of using this in a fish script

The output of:

rtc "setup database" "run migrations" "create new migrations and run them"

Could be one of the following, in the form of an exit code:

  • -1 => this means that the user chose nothing (they pressed Esc)
  • 0 => the user chose the 1st option "setup database"
  • 1 => the user chose the 2nd option "run migrations"
  • 2 => the user chose the 3rd option "create new migrations and run them"

Additionally the string that is selected can be stored in an environment variable $user_selection.

This can then be used in a fish script to do something like this:

echo (set_color blue)Welcome to db setup!(set_color normal)
echo (set_color green)Please select one from the following:(set_color normal)
rtc "setup database" "run migrations" "create new migrations and run them"

if test $status -eq 0
  echo You chose \"setup database\"
end

if test $status -eq 1
  echo You chose \"run migrations\"
end

if test ($user_selection) = "create new migrations and run them"
  echo You chose the 3rd item
end

if test $status -eq -1
  echo Exiting script
end

Advanced use cases

In the previous example, only a single selection is made by the user. Perhaps a flag can be passed that would allow them to make multiple choices, eg: --multiple.

In this case the exit code could be -2, which means that the user has made multiple selections.

Two environment variables could be used to capture the selection indices and text:

  1. $user_selection_indices => this might contain a space delimited sequence of numbers, eg: 0 1
  2. $user_selection_choices => this might contain a space delimited sequence of strings, eg: "foo" "bar" "baz"

Here's an example of the syntax for this use case:

rtc --multiple "setup database" "run migrations" "create new migrations and run them"

Real world use case

The rust-scratch repo has a sql crate which has a fish script that can benefit from this. That script has a complex user flow that would benefit from using this rtc instead of the built-in fish function read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant