arch/pkg/de-p1st-installer/lib/user-input.sh

101 lines
2.7 KiB
Bash

function get_text_input {
# If variable with name $1 is zero, then ask user for input.
# Only one line user input is allowed.
# User input must not be empty.
#
# $1: name of variable to store user input
# $2: text to display (e.g. "Enter hostname:")
if [ "$#" -ne 2 ]; then
echo "get_text_input requires two args!";
return 1
fi
for i in "$@"; do
if [ -z "$i" ]; then
echo "get_text_input: all given args must not be empty";
return 1;
fi
done
local -n ptr=$1
if [ -z "$ptr" ]; then
# if ptr has no value yet, ask user for input!
echo "$2"
read -r ptr || return $?
fi
# check string length to be greater than zero!
if [ "${#ptr}" -lt 1 ]; then
echo "get_text_input must not be empty!";
return 1;
fi
}
function get_single_choice {
# If variable with name $1 is zero, then ask user to select one option.
#
# $1: name of variable to store the selected option
# $2: text to display
# $3: name of variable with array of options to display (for each option there must be two entries in the array: Item and description)
if [ "$#" -ne 3 ]; then
echo "get_single_choice requires three args!";
return 1
fi
for i in "$@"; do
if [ -z "$i" ]; then
echo "get_single_choice: all given args must not be empty";
return 1;
fi
done
local -n ptr=$1
if [ -z "$ptr" ]; then
# if ptr has no value yet, ask user for input!
local -n MENU_OPTIONS=$3
ptr=$(dialog --stdout --menu "$2" 0 0 0 "${MENU_OPTIONS[@]}") || {
echo "Error during menu selection!"
exit 1
}
clear
fi
}
function get_multi_choice {
# If variable with name $1 is zero, then ask user to select one ore more options.
#
# $1: name of variable to store array of selected options
# $2: text to display
# $3: name of variable with array of options to display (for each option there must be three entries in the array: Item, description, on/off)
if [ "$#" -ne 3 ]; then
echo "get_multi_choice requires three args!";
return 1
fi
for i in "$@"; do
if [ -z "$i" ]; then
echo "get_multi_choice: all given args must not be empty";
return 1;
fi
done
local -n ptr=$1
if [ -z "$ptr" ]; then
# if ptr has no value yet, ask user for input!
local -n MENU_OPTIONS=$3
TMP1=$(dialog --stdout --checklist "$2" 0 0 0 "${MENU_OPTIONS[@]}") || {
echo "Error during menu selection!"
exit 1
}
clear
# Result of dialog is space separated list
# Store this as an array
# Without newline at last array element: https://unix.stackexchange.com/a/519917/315162
# readarray -d " " -t ptr < <(printf '%s' "$TMP1")
#
space_separated_to_array TMP1 "$1"
fi
}