You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"iftest$status-eq 0
echo You chose \"setup database\"endif test $status -eq 1 echo You chose \"run migrations\"endif test ($user_selection) = "create new migrations and run them" echo You chose the 3rd itemendif test $status -eq -1 echo Exiting scriptend
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:
$user_selection_indices => this might contain a space delimited sequence of numbers, eg: 0 1
$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.
The text was updated successfully, but these errors were encountered:
Use case
0..length-1
), or-1
, meaning user chose nothing (exited).rtc
, since there's already a binary calledrt
that handles a different use case.Example of using this in a fish script
The output of:
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:
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:
$user_selection_indices
=> this might contain a space delimited sequence of numbers, eg:0 1
$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:
Real world use case
The
rust-scratch
repo has asql
crate which has afish
script that can benefit from this. That script has a complex user flow that would benefit from using thisrtc
instead of the built-in fish functionread
.The text was updated successfully, but these errors were encountered: