Features: 1) Improve handling of user prompts by consolidating read and printf usage;

Fixes: 1) Remove unnecessary else blocks for default values in prompt functions; 2) Ensure proper initialization of `response` variable to avoid undefined behavior;

Extra: 1) Use consistent `printf` formatting across `prompt_default` and `prompt_autogen` functions for cleaner output; 2) Minor indentation and code cleanup.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-12-08 22:38:20 +03:00
parent 8295d3f5ab
commit 42392307a4

View file

@ -8,38 +8,28 @@ get_random_hex() {
}
prompt_default() {
local response=""
if [ -t 0 ]; then
printf "Enter %s [%s]: " "$1" "$2"
if read -r response </dev/tty; then
:
else
response=""
fi
else
response=""
read -r -p "Enter $1 [$2]: " response </dev/tty || response=""
fi
if [ -z "${response//[[:space:]]/}" ]; then
echo "$2"
printf '%s\n' "$2"
else
echo "$response"
printf '%s\n' "$response"
fi
}
prompt_autogen() {
local response=""
if [ -t 0 ]; then
printf "Enter %s (leave blank to auto-generate): " "$1"
if read -r response </dev/tty; then
:
else
response=""
fi
else
response=""
read -r -p "Enter $1 (leave blank to auto-generate): " response </dev/tty || response=""
fi
if [ -z "${response//[[:space:]]/}" ]; then
get_random_hex "$2"
else
echo "$response"
printf '%s\n' "$response"
fi
}