fn: ask
Purpose
Prompt the user for one text value through the SolidGroundUX console UI.
Behavior
- Reads from /dev/tty so piped stdin remains available to callers.
- Supports readline editing and editable default values.
- Applies optional label alignment, padding, and input coloring.
- Re-prompts until the optional validation callback accepts the value.
Options
--label TEXT— Prompt label shown before the input field.--var NAME— Shell variable that receives the entered value.--default VALUE— Editable default value offered to the user.--validate FUNCTION— Optional validator callback. FUNCTION receives the value as $1 and must return 0 for valid input and non-zero for invalid input.--back— Treat a lone Q (case-insensitive) as "back" and return 1 without assigning the target variable.--echo— Display the accepted value and validation marker on/dev/tty; also write the value to stdout when--varis omitted.--labelwidth WIDTH— Visible width used to align the prompt label.--pad COUNT— Number of spaces inserted before the prompt label.--labelclr ANSI— ANSI sequence used for the label.--inputclr ANSI— ANSI sequence used for the input.--colorize MODE— Colorize mode: none, label, input, or both.
Common validators
sgnd_validate_textsgnd_validate_yesnosgnd_validate_intsgnd_validate_numericsgnd_validate_boolsgnd_validate_datesgnd_validate_ipv4sgnd_validate_cidrsgnd_validate_slugsgnd_validate_fs_namesgnd_validate_file_existssgnd_validate_file_not_existssgnd_validate_path_existssgnd_validate_dir_existssgnd_validate_executable
Outputs (globals)
Variable named by --var, when --var is supplied.
Output
Writes the value to stdout only when --echo is used without --var.
Side effects
Writes the interactive prompt, validation message, and optional echoed value to /dev/tty.
Returns
- 0 when a valid value was accepted.
- 1 when --back is enabled and Q is entered.
- 2 when /dev/tty is not available.
Usage
# Ask for a value and store it in a shell variable.
ask --label "Project name" --var PROJECT_NAME
# Offer an editable default and require an existing directory.
ask --label "Target directory" --var TARGET_DIR \
--default "/tmp" --validate sgnd_validate_dir_exists
# Validate a number using a framework callback.
ask --label "Retry count" --var RETRY_COUNT \
--default "3" --validate sgnd_validate_int
# Accept Q as a request to return to the previous menu (exit status 1).
ask --label "Remote host" --var REMOTE_HOST --back
# Read stdout when no --var destination is requested.
USER_NAME="$(ask --label "User name" --echo)"
# Customize alignment, padding and color mode.
ask --label "Database host" --var DB_HOST \
--labelwidth 28 --pad 2 --colorize label
