script_wizard/
lib.rs

1//! script-wizard is a shell script (Bash) helper program, to delegate
2//! the responsibility of asking questions to the user, asking for
3//! confirmation, making selections, etc. The normalized response is
4//! printed to stdout for the script to consume.
5//!
6//! This is a single binary application written in Rust and it
7//! utilizes the
8//! [inquire](https://docs.rs/inquire/latest/inquire/index.html)
9//! library for providing a nicer user interface than the typical
10//! shell script is capable of.
11//!
12//! # Source code
13//! The source code is hosted at github [EnigmaCurry/script-wizard](https://github.com/EnigmaCurry/script-wizard)
14//! # Install with cargo
15//!
16//! ```bash
17//! cargo install script_wizard
18//! ```
19//!
20//! # Install without cargo
21//! [Grab the latest release from github](https://github.com/EnigmaCurry/script-wizard/releases) or run this command to download and install automatically:
22//! ```bash
23//! cd ~/Downloads
24//! ARCHIVE=script-wizard-$(uname -s)-$(uname -m).tar.gz
25//! curl -LO https://github.com/Enigmacurry/script-wizard/releases/latest/download/${ARCHIVE}
26//! tar xfv ${ARCHIVE}
27//! sudo install script-wizard /usr/local/bin/script-wizard
28//! ```
29//!
30//! ## Examples in Bash
31//! 
32//! ### ask
33//! 
34//! Ask the user a question and capture the response:
35//! 
36//! ```bash
37//! # Set the alias to make it easier to use:
38//! alias ask='script-wizard ask'
39//! 
40//! # Record the user's response into the NAME variable:
41//! NAME=$(ask "What is your name?")
42//! ```
43//! 
44//! ### confirm
45//! 
46//! Ask the user a yes/no question, with a prepared default response (eg.
47//! `yes` is the default here) :
48//! 
49//! ```bash
50//! # Set the alias to make it easier to use:
51//! alias confirm='script-wizard confirm'
52//! 
53//! # Confirm returns an exit code: 0=yes 1=no :
54//! if confirm "Do you like Linux?" yes; then
55//!   echo "Tux is great!"
56//! else
57//!   echo "Well, thats ok."
58//! fi
59//! 
60//! # But maybe you want to record a literal "true" or "false" into a variable?:
61//! LIKES_LINUX=$(confirm "Do you like Linux?" yes && echo "true" || echo "false")
62//! ```
63//! 
64//! ### choose
65//! 
66//! Present a list of options to the user and have them select a *single*
67//! response from the list:
68//! 
69//! ```bash
70//! # Set the alias to make it easier to use:
71//! alias choose='script-wizard choose'
72//! 
73//! CHOSEN=$(choose "Select your character class" "Rogue" "Wizard" "Paladin" "Cleric" "Bard")
74
75//! # You can use an option from a bash array too:
76//! options=("red" "blue" "greenish orange" "purple")
77//! COLOR=$(choose "Choose a color" "${options[@]}")
78//! ```
79//! 
80//! ### select
81//! 
82//! Present a list of options to the user and have them select *multiple*
83//! responses (zero or more) from the list:
84//! 
85//! ```bash
86//! readarray -t SELECTED < <(script-wizard select "Which games do you like?" "Rocket League" "Portal" "Quake" "Magic the Gathering")
87//! 
88//! echo "These are the games you said you like:"
89//! # Use printf to print one per line (echo would merge into one line):
90//! printf '%s\n' "${SELECTED[@]}"
91//! ```
92//! 
93//! ### date
94//! 
95//! Present a date picker to the user:
96//! 
97//! ```bash
98//! # Pick a date between 2023/10/01 and 2023/10/20:
99//! DATE=$(script-wizard date "Enter a date" --week-start monday --format "%Y-%m-%d" --min-date "2023-10-01" --max-date "2023-10-20" --help-message "yadda yadda")
100//! ```
101//! 
102//! ### editor
103//! 
104//! Present a full text editor entry to the user:
105//! 
106//! ```bash
107//! BIOGRAPHY=$(script-wizard editor "Tell me alllll about yourself" --default "# Describe yourself" --json | sed 's/^[^\"]*//')
108//! ```
109//! 
110//! Watch out: There is a potential bug here if your editor prints
111//! anything to stdout. (In the case of emacsclient, it undesirably
112//! captures the text "Waiting for Emacs...".) Using `--json` will wrap
113//! the correct editor text in double quotes, and pipeing the output
114//! through `sed 's/^[^\"]*//'` will remove the text before the first
115//! double quote.)
116//! 
117//! Set the common `EDITOR` environment variable to choose the editor it
118//! launches.
119//! 
120//! ### menu
121//! 
122//! Present a menu of command entries that the user can select and
123//! execute. The entries must be specified in the format: `ENTRY =
124//! COMMAND` where `ENTRY` is the text line of the menu entry, and
125//! `COMMAND` is the shell command to run if the entry is selected:
126//! 
127//! ```bash
128//! script-wizard menu --once "main menu" "print username = whoami"  "print all users = cat /etc/passwd | cut -d ':' -f 1"
129//! ```
130//!
131//! ## Common options
132//! 
133//!  * `--json` - the default is to print raw text even if it spans
134//!    multiple lines. If you specify `--json` it will print it as compact
135//!    JSON on a single line, splitting lines into lists of strings, or as
136//!    a quoted string if its just supposed to be one line, depending on
137//!    the subcommand.
138pub mod ask;