[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-07-15 21:03:11 +00:00
parent fb1c3f4805
commit a3ed167291
4 changed files with 1265 additions and 1273 deletions

View File

@ -43,7 +43,7 @@ repos:
- repo: https://github.com/scop/pre-commit-shfmt - repo: https://github.com/scop/pre-commit-shfmt
rev: v3.8.0-1 rev: v3.8.0-1
hooks: hooks:
- id: shfmt - id: shfmt
- repo: https://github.com/shellcheck-py/shellcheck-py - repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.10.0.1 rev: v0.10.0.1

View File

@ -1,77 +1,74 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if [ ! -f "$HOME/.config/winapps/winapps.conf" ] && [ ! -f "$HOME/.winapps" ]; then if [ ! -f "$HOME/.config/winapps/winapps.conf" ] && [ ! -f "$HOME/.winapps" ]; then
echo "You need to create a ~/.config/winapps/winapps.conf configuration. Exiting..." echo "You need to create a ~/.config/winapps/winapps.conf configuration. Exiting..."
exit exit
fi fi
DIR="$(dirname "$(readlink -f "$0")")" DIR="$(dirname "$(readlink -f "$0")")"
RUN="$(date)-$RANDOM" RUN="$(date)-$RANDOM"
if [ ! -d "$HOME/.local/share/winapps" ]; then if [ ! -d "$HOME/.local/share/winapps" ]; then
mkdir -p "$HOME/.local/share/winapps" mkdir -p "$HOME/.local/share/winapps"
fi fi
RDP_SCALE=100 RDP_SCALE=100
if [ -f "$HOME/.config/winapps/winapps.conf" ]; then if [ -f "$HOME/.config/winapps/winapps.conf" ]; then
# shellcheck source=/dev/null # shellcheck source=/dev/null
. "$HOME/.config/winapps/winapps.conf" . "$HOME/.config/winapps/winapps.conf"
else else
# shellcheck source=/dev/null # shellcheck source=/dev/null
. "$HOME/.winapps" . "$HOME/.winapps"
fi fi
function dprint() { function dprint() {
if [ "$DEBUG" = "true" ]; then if [ "$DEBUG" = "true" ]; then
echo "[$RUN] $1" >>"$HOME/.local/share/winapps/winapps.log" echo "[$RUN] $1" >>"$HOME/.local/share/winapps/winapps.log"
fi fi
} }
dprint "START" dprint "START"
if [ -f "$HOME/.local/share/winapps/run" ]; then if [ -f "$HOME/.local/share/winapps/run" ]; then
LAST_RAN=$(stat -t -c %Y "$HOME/.local/share/winapps/run") LAST_RAN=$(stat -t -c %Y "$HOME/.local/share/winapps/run")
dprint "LAST_RAN:${LAST_RAN}" dprint "LAST_RAN:${LAST_RAN}"
touch "$HOME/.local/share/winapps/run" touch "$HOME/.local/share/winapps/run"
THIS_RUN=$(stat -t -c %Y "$HOME/.local/share/winapps/run") THIS_RUN=$(stat -t -c %Y "$HOME/.local/share/winapps/run")
dprint "THIS_RUN:$THIS_RUN" dprint "THIS_RUN:$THIS_RUN"
if ((THIS_RUN - LAST_RAN < 2)); then if ((THIS_RUN - LAST_RAN < 2)); then
exit exit
fi fi
else else
touch "$HOME/.local/share/winapps/run" touch "$HOME/.local/share/winapps/run"
fi fi
if [ -z "${FREERDP_COMMAND}" ]; then if [ -z "${FREERDP_COMMAND}" ]; then
if command -v xfreerdp &> /dev/null if command -v xfreerdp &>/dev/null; then
then FREERDP_COMMAND="xfreerdp"
FREERDP_COMMAND="xfreerdp" elif command -v xfreerdp3 &>/dev/null; then
elif command -v xfreerdp3 &> /dev/null FREERDP_COMMAND="xfreerdp3"
then fi
FREERDP_COMMAND="xfreerdp3" elif command -v "$FREERDP_COMMAND" &>/dev/null; then
fi dprint "Using custom freerdp command $FREERDP_COMMAND"
elif command -v "$FREERDP_COMMAND" &> /dev/null
then
dprint "Using custom freerdp command $FREERDP_COMMAND"
else else
echo "You have supplied a custom FreeRDP command, but the command is not available." echo "You have supplied a custom FreeRDP command, but the command is not available."
exit exit
fi fi
if [ -z "$RDP_IP" ]; then if [ -z "$RDP_IP" ]; then
if groups | grep -vq libvirt; then if groups | grep -vq libvirt; then
echo "You are not a member of the libvirt group. Run the below then reboot." echo "You are not a member of the libvirt group. Run the below then reboot."
echo " sudo usermod -a -G libvirt $(whoami)" echo " sudo usermod -a -G libvirt $(whoami)"
echo " sudo usermod -a -G kvm $(whoami)" echo " sudo usermod -a -G kvm $(whoami)"
exit exit
fi fi
if ! virsh list --state-running --name | grep -q '^RDPWindows$'; then if ! virsh list --state-running --name | grep -q '^RDPWindows$'; then
echo "RDPWindows is not running. Please run:" echo "RDPWindows is not running. Please run:"
echo " virsh start RDPWindows" echo " virsh start RDPWindows"
exit exit
fi fi
RDP_IP=$(virsh net-dhcp-leases default | grep "RDPWindows" | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}') RDP_IP=$(virsh net-dhcp-leases default | grep "RDPWindows" | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}')
fi fi
dprint "1:$1" dprint "1:$1"
@ -82,128 +79,128 @@ dprint "@:${@}"
MULTI_FLAG="+span" MULTI_FLAG="+span"
if [ "$MULTIMON" = "true" ]; then if [ "$MULTIMON" = "true" ]; then
MULTI_FLAG="/multimon" MULTI_FLAG="/multimon"
fi fi
# Append additional flags or parameters to FreeRDP # Append additional flags or parameters to FreeRDP
if [[ -n "$RDP_FLAGS" ]]; then if [[ -n $RDP_FLAGS ]]; then
FREERDP_COMMAND="$FREERDP_COMMAND $RDP_FLAGS" FREERDP_COMMAND="$FREERDP_COMMAND $RDP_FLAGS"
fi fi
if [ "$1" = "check" ]; then if [ "$1" = "check" ]; then
# Open File Explorer # Open File Explorer
dprint "CHECK" dprint "CHECK"
COMMAND=( COMMAND=(
"${FREERDP_COMMAND}" "${FREERDP_COMMAND}"
"/d:${RDP_DOMAIN}" "/d:${RDP_DOMAIN}"
"/u:${RDP_USER}" "/u:${RDP_USER}"
"/p:${RDP_PASS}" "/p:${RDP_PASS}"
"/scale:${RDP_SCALE}" "/scale:${RDP_SCALE}"
"+auto-reconnect" "+auto-reconnect"
"+home-drive" "+home-drive"
"-wallpaper" "-wallpaper"
"+dynamic-resolution" "+dynamic-resolution"
"${MULTI_FLAG}" "${MULTI_FLAG}"
"/app:program:explorer.exe" "/app:program:explorer.exe"
"/v:${RDP_IP}" "/v:${RDP_IP}"
) )
"${COMMAND[@]}" "${COMMAND[@]}"
elif [ "$1" = "windows" ]; then elif [ "$1" = "windows" ]; then
# Open Virtual Machine # Open Virtual Machine
dprint "WINDOWS" dprint "WINDOWS"
COMMAND=( COMMAND=(
"${FREERDP_COMMAND}" "${FREERDP_COMMAND}"
"/d:${RDP_DOMAIN}" "/d:${RDP_DOMAIN}"
"/u:${RDP_USER}" "/u:${RDP_USER}"
"/p:${RDP_PASS}" "/p:${RDP_PASS}"
"/scale:${RDP_SCALE}" "/scale:${RDP_SCALE}"
"+dynamic-resolution" "+dynamic-resolution"
"+auto-reconnect" "+auto-reconnect"
"+home-drive" "+home-drive"
"/wm-class:\"Microsoft Windows\"" '/wm-class:"Microsoft Windows"'
"/v:${RDP_IP}" "/v:${RDP_IP}"
) )
# Run the command in the background, redirecting both stdout and stderr to /dev/null # Run the command in the background, redirecting both stdout and stderr to /dev/null
"${COMMAND[@]}" 1>/dev/null 2>&1 & "${COMMAND[@]}" 1>/dev/null 2>&1 &
elif [ "$1" = "manual" ]; then elif [ "$1" = "manual" ]; then
# Open Specified Application # Open Specified Application
dprint "MANUAL:${2}" dprint "MANUAL:${2}"
COMMAND=( COMMAND=(
"${FREERDP_COMMAND}" "${FREERDP_COMMAND}"
"/d:${RDP_DOMAIN}" "/d:${RDP_DOMAIN}"
"/u:${RDP_USER}" "/u:${RDP_USER}"
"/p:${RDP_PASS}" "/p:${RDP_PASS}"
"/scale:${RDP_SCALE}" "/scale:${RDP_SCALE}"
"+auto-reconnect" "+auto-reconnect"
"+home-drive" "+home-drive"
"+dynamic-resolution" "+dynamic-resolution"
"${MULTI_FLAG}" "${MULTI_FLAG}"
"/app:program:${2}" "/app:program:${2}"
"/v:${RDP_IP}" "/v:${RDP_IP}"
) )
# Run the command in the background, redirecting both stdout and stderr to /dev/null # Run the command in the background, redirecting both stdout and stderr to /dev/null
"${COMMAND[@]}" 1>/dev/null 2>&1 & "${COMMAND[@]}" 1>/dev/null 2>&1 &
elif [ "$1" != "install" ]; then elif [ "$1" != "install" ]; then
dprint "DIR:${DIR}" dprint "DIR:${DIR}"
if [ -e "${DIR}/../apps/$1/info" ]; then if [ -e "${DIR}/../apps/$1/info" ]; then
# shellcheck disable=SC1090 # shellcheck disable=SC1090
. "${DIR}/../apps/$1/info" . "${DIR}/../apps/$1/info"
ICON="${DIR}/../apps/$1/icon.svg" ICON="${DIR}/../apps/$1/icon.svg"
elif [ -e "$HOME/.local/share/winapps/apps/$1/info" ]; then elif [ -e "$HOME/.local/share/winapps/apps/$1/info" ]; then
# shellcheck disable=SC1090 # shellcheck disable=SC1090
. "$HOME/.local/share/winapps/apps/$1/info" . "$HOME/.local/share/winapps/apps/$1/info"
ICON="$HOME/.local/share/winapps/apps/$1/icon.svg" ICON="$HOME/.local/share/winapps/apps/$1/icon.svg"
elif [ -e "/usr/local/share/winapps/apps/$1/info" ]; then elif [ -e "/usr/local/share/winapps/apps/$1/info" ]; then
# shellcheck disable=SC1090 # shellcheck disable=SC1090
. "/usr/local/share/winapps/apps/$1/info" . "/usr/local/share/winapps/apps/$1/info"
ICON="/usr/local/share/winapps/apps/$1/icon.svg" ICON="/usr/local/share/winapps/apps/$1/icon.svg"
else else
echo "You need to run 'installer.sh' first." echo "You need to run 'installer.sh' first."
exit 1 exit 1
fi fi
if [ -n "$2" ]; then if [ -n "$2" ]; then
dprint "HOME:$HOME" dprint "HOME:$HOME"
FILE=$(echo "$2" | sed 's|'"$HOME"'|\\\\tsclient\\home|;s|/|\\|g;s|\\|\\\\|g') FILE=$(echo "$2" | sed 's|'"$HOME"'|\\\\tsclient\\home|;s|/|\\|g;s|\\|\\\\|g')
dprint "FILE:${FILE}" dprint "FILE:${FILE}"
# shellcheck disable=SC2140 # shellcheck disable=SC2140
COMMAND=( COMMAND=(
"${FREERDP_COMMAND}" "${FREERDP_COMMAND}"
"/d:${RDP_DOMAIN}" "/d:${RDP_DOMAIN}"
"/u:${RDP_USER}" "/u:${RDP_USER}"
"/p:${RDP_PASS}" "/p:${RDP_PASS}"
"/scale:${RDP_SCALE}" "/scale:${RDP_SCALE}"
"+auto-reconnect" "+auto-reconnect"
"+clipboard" "+clipboard"
"+home-drive" "+home-drive"
"-wallpaper" "-wallpaper"
"+dynamic-resolution" "+dynamic-resolution"
"${MULTI_FLAG}" "${MULTI_FLAG}"
"/wm-class:${FULL_NAME}" "/wm-class:${FULL_NAME}"
"/app:program:${WIN_EXECUTABLE},icon:${ICON},name:${FULL_NAME},cmd:\"${FILE}\"" "/app:program:${WIN_EXECUTABLE},icon:${ICON},name:${FULL_NAME},cmd:\"${FILE}\""
"/v:${RDP_IP}" "/v:${RDP_IP}"
) )
# Run the command in the background, redirecting both stdout and stderr to /dev/null # Run the command in the background, redirecting both stdout and stderr to /dev/null
echo "${COMMAND[@]}" #1>/dev/null 2>&1 & echo "${COMMAND[@]}" #1>/dev/null 2>&1 &
else else
COMMAND=( COMMAND=(
"${FREERDP_COMMAND}" "${FREERDP_COMMAND}"
"/d:${RDP_DOMAIN}" "/d:${RDP_DOMAIN}"
"/u:${RDP_USER}" "/u:${RDP_USER}"
"/p:${RDP_PASS}" "/p:${RDP_PASS}"
"/scale:${RDP_SCALE}" "/scale:${RDP_SCALE}"
"+auto-reconnect" "+auto-reconnect"
"+clipboard" "+clipboard"
"+home-drive" "+home-drive"
"-wallpaper" "-wallpaper"
"+dynamic-resolution" "+dynamic-resolution"
"${MULTI_FLAG}" "${MULTI_FLAG}"
"/wm-class:${FULL_NAME}" "/wm-class:${FULL_NAME}"
"/app:program:${WIN_EXECUTABLE},icon:${ICON},name:${FULL_NAME}" "/app:program:${WIN_EXECUTABLE},icon:${ICON},name:${FULL_NAME}"
"/v:${RDP_IP}" "/v:${RDP_IP}"
) )
# Run the command in the background, redirecting both stdout and stderr to /dev/null # Run the command in the background, redirecting both stdout and stderr to /dev/null
"${COMMAND[@]}" 1>/dev/null 2>&1 & "${COMMAND[@]}" 1>/dev/null 2>&1 &
fi fi
fi fi
dprint "END" dprint "END"

View File

@ -1,186 +1,186 @@
#!/usr/bin/env bash #!/usr/bin/env bash
### GLOBAL CONSTANTS ### ### GLOBAL CONSTANTS ###
declare -r ANSI_LIGHT_BLUE="\033[1;94m" # Light blue text. declare -r ANSI_LIGHT_BLUE="\033[1;94m" # Light blue text.
declare -r ANSI_LIGHT_GREEN="\033[92m" # Light green text. declare -r ANSI_LIGHT_GREEN="\033[92m" # Light green text.
declare -r ANSI_CLEAR_TEXT="\033[0m" # Default text. declare -r ANSI_CLEAR_TEXT="\033[0m" # Default text.
declare -r DIALOG_HEIGHT=14 # Height of dialog window. declare -r DIALOG_HEIGHT=14 # Height of dialog window.
declare -r TEXT_WIDTH_OFFSET=4 # Offset for fitting title text. declare -r TEXT_WIDTH_OFFSET=4 # Offset for fitting title text.
declare -r CHK_OPTION_WIDTH_OFFSET=10 # Offset for fitting options. declare -r CHK_OPTION_WIDTH_OFFSET=10 # Offset for fitting options.
declare -r MNU_OPTION_WIDTH_OFFSET=7 # Offset for fitting options. declare -r MNU_OPTION_WIDTH_OFFSET=7 # Offset for fitting options.
### FUNCTIONS ### ### FUNCTIONS ###
function inqMenu() { function inqMenu() {
# DECLARE VARIABLES. # DECLARE VARIABLES.
# Variables created from function arguments: # Variables created from function arguments:
declare DIALOG_TEXT="$1" # Dialog heading. declare DIALOG_TEXT="$1" # Dialog heading.
declare INPUT_OPTIONS_VAR="$2" # Input variable name. declare INPUT_OPTIONS_VAR="$2" # Input variable name.
declare RETURN_STRING_VAR="$3" # Output variable name. declare RETURN_STRING_VAR="$3" # Output variable name.
declare -n INPUT_OPTIONS="$INPUT_OPTIONS_VAR" # Input array nameref. declare -n INPUT_OPTIONS="$INPUT_OPTIONS_VAR" # Input array nameref.
declare -n RETURN_STRING="$RETURN_STRING_VAR" # Output string nameref. declare -n RETURN_STRING="$RETURN_STRING_VAR" # Output string nameref.
# Note: namerefs allow changes made through the nameref to affect the # Note: namerefs allow changes made through the nameref to affect the
# referenced variable, even across different scopes like function calls. # referenced variable, even across different scopes like function calls.
# Other variables: # Other variables:
declare TRIMMED_OPTIONS=() # Input array post-trimming. declare TRIMMED_OPTIONS=() # Input array post-trimming.
declare PADDED_OPTIONS=() # Input array with extra white space. declare PADDED_OPTIONS=() # Input array with extra white space.
declare DIALOG_OPTIONS=() # Input array for options dialog. declare DIALOG_OPTIONS=() # Input array for options dialog.
declare DIALOG_WIDTH=0 # Width of dialog window. declare DIALOG_WIDTH=0 # Width of dialog window.
declare OPTION_NUMBER=0 # Number of options in dialog window. declare OPTION_NUMBER=0 # Number of options in dialog window.
declare SELECTED_OPTIONS_STRING="" # Output value from dialog window. declare SELECTED_OPTIONS_STRING="" # Output value from dialog window.
# MAIN LOGIC. # MAIN LOGIC.
# Trim leading and trailing white space for each option. # Trim leading and trailing white space for each option.
for OPTION in "${INPUT_OPTIONS[@]}"; do for OPTION in "${INPUT_OPTIONS[@]}"; do
TRIMMED_OPTIONS+=("$(echo "$OPTION" | sed 's/^[ \t]*//;s/[ \t]*$//')") TRIMMED_OPTIONS+=("$(echo "$OPTION" | sed 's/^[ \t]*//;s/[ \t]*$//')")
done done
# Find the length of the longest option to set the dialog width. # Find the length of the longest option to set the dialog width.
for OPTION in "${TRIMMED_OPTIONS[@]}"; do for OPTION in "${TRIMMED_OPTIONS[@]}"; do
if [ "${#OPTION}" -gt "$DIALOG_WIDTH" ]; then if [ "${#OPTION}" -gt "$DIALOG_WIDTH" ]; then
DIALOG_WIDTH=${#OPTION} DIALOG_WIDTH=${#OPTION}
fi fi
done done
# Apply the offset value to the dialog width. # Apply the offset value to the dialog width.
DIALOG_WIDTH=$((DIALOG_WIDTH + MNU_OPTION_WIDTH_OFFSET)) DIALOG_WIDTH=$((DIALOG_WIDTH + MNU_OPTION_WIDTH_OFFSET))
# Adjust the dialog width again if the dialog text is longer. # Adjust the dialog width again if the dialog text is longer.
if [ "$DIALOG_WIDTH" -lt $((${#DIALOG_TEXT} + TEXT_WIDTH_OFFSET)) ]; then if [ "$DIALOG_WIDTH" -lt $((${#DIALOG_TEXT} + TEXT_WIDTH_OFFSET)) ]; then
DIALOG_WIDTH="$((${#DIALOG_TEXT} + TEXT_WIDTH_OFFSET))" DIALOG_WIDTH="$((${#DIALOG_TEXT} + TEXT_WIDTH_OFFSET))"
fi fi
# Pad option text with trailing white space to left-align all options. # Pad option text with trailing white space to left-align all options.
for OPTION in "${TRIMMED_OPTIONS[@]}"; do for OPTION in "${TRIMMED_OPTIONS[@]}"; do
local PAD_LENGTH=$((DIALOG_WIDTH - MNU_OPTION_WIDTH_OFFSET - ${#OPTION})) local PAD_LENGTH=$((DIALOG_WIDTH - MNU_OPTION_WIDTH_OFFSET - ${#OPTION}))
# shellcheck disable=SC2155 # shellcheck disable=SC2155
local PADDED_OPTION="${OPTION}$(printf '%*s' $PAD_LENGTH)" local PADDED_OPTION="${OPTION}$(printf '%*s' $PAD_LENGTH)"
PADDED_OPTIONS+=("$PADDED_OPTION") PADDED_OPTIONS+=("$PADDED_OPTION")
done done
# Convert options into the appropriate format for a 'dialog' menu. # Convert options into the appropriate format for a 'dialog' menu.
for PADDED_OPTION in "${PADDED_OPTIONS[@]}"; do for PADDED_OPTION in "${PADDED_OPTIONS[@]}"; do
DIALOG_OPTIONS+=("$PADDED_OPTION" "") DIALOG_OPTIONS+=("$PADDED_OPTION" "")
done done
# Store the number of options. # Store the number of options.
OPTION_NUMBER="${#INPUT_OPTIONS[@]}" OPTION_NUMBER="${#INPUT_OPTIONS[@]}"
# Produce checkbox. # Produce checkbox.
# The output string contains options delimited by spaces. # The output string contains options delimited by spaces.
# Each option is enclosed in double quotes within the output string. # Each option is enclosed in double quotes within the output string.
# For example: '"Option 1 " "The Second Option " " Option Number 3 "' # For example: '"Option 1 " "The Second Option " " Option Number 3 "'
SELECTED_OPTIONS_STRING=$(dialog \ SELECTED_OPTIONS_STRING=$(dialog \
--keep-tite \ --keep-tite \
--clear \ --clear \
--no-shadow \ --no-shadow \
--menu \ --menu \
"$DIALOG_TEXT" \ "$DIALOG_TEXT" \
"$DIALOG_HEIGHT" \ "$DIALOG_HEIGHT" \
"$DIALOG_WIDTH" \ "$DIALOG_WIDTH" \
"$OPTION_NUMBER" \ "$OPTION_NUMBER" \
"${DIALOG_OPTIONS[@]}" \ "${DIALOG_OPTIONS[@]}" \
2>&1 >/dev/tty) || exit 0 2>&1 >/dev/tty) || exit 0
# Remove white space added previously. # Remove white space added previously.
RETURN_STRING=$(echo "$SELECTED_OPTIONS_STRING" | \ RETURN_STRING=$(echo "$SELECTED_OPTIONS_STRING" |
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
# Remove escapes (introduced by 'dialog' if options have parentheses). # Remove escapes (introduced by 'dialog' if options have parentheses).
RETURN_STRING="${RETURN_STRING//\\/}" # ${variable//search/replace} RETURN_STRING="${RETURN_STRING//\\/}" # ${variable//search/replace}
# Display question and response. # Display question and response.
echo -e "${ANSI_LIGHT_GREEN}Q) ${ANSI_CLEAR_TEXT}${ANSI_LIGHT_BLUE}${DIALOG_TEXT}${ANSI_CLEAR_TEXT} --> ${ANSI_LIGHT_GREEN}${RETURN_STRING}${ANSI_CLEAR_TEXT}" echo -e "${ANSI_LIGHT_GREEN}Q) ${ANSI_CLEAR_TEXT}${ANSI_LIGHT_BLUE}${DIALOG_TEXT}${ANSI_CLEAR_TEXT} --> ${ANSI_LIGHT_GREEN}${RETURN_STRING}${ANSI_CLEAR_TEXT}"
} }
function inqChkBx() { function inqChkBx() {
# DECLARE VARIABLES. # DECLARE VARIABLES.
# Variables created from function arguments: # Variables created from function arguments:
declare DIALOG_TEXT="$1" # Dialog heading. declare DIALOG_TEXT="$1" # Dialog heading.
declare INPUT_OPTIONS_VAR="$2" # Input variable name. declare INPUT_OPTIONS_VAR="$2" # Input variable name.
declare RETURN_ARRAY_VAR="$3" # Output variable name. declare RETURN_ARRAY_VAR="$3" # Output variable name.
declare -n INPUT_OPTIONS="$INPUT_OPTIONS_VAR" # Input array nameref. declare -n INPUT_OPTIONS="$INPUT_OPTIONS_VAR" # Input array nameref.
declare -n RETURN_ARRAY="$RETURN_ARRAY_VAR" # Output array nameref. declare -n RETURN_ARRAY="$RETURN_ARRAY_VAR" # Output array nameref.
# Note: namerefs allow changes made through the nameref to affect the # Note: namerefs allow changes made through the nameref to affect the
# referenced variable, even across different scopes like function calls. # referenced variable, even across different scopes like function calls.
# Other variables: # Other variables:
declare TRIMMED_OPTIONS=() # Input array post-trimming. declare TRIMMED_OPTIONS=() # Input array post-trimming.
declare PADDED_OPTIONS=() # Input array with extra white space. declare PADDED_OPTIONS=() # Input array with extra white space.
declare DIALOG_OPTIONS=() # Input array for options dialog. declare DIALOG_OPTIONS=() # Input array for options dialog.
declare DIALOG_WIDTH=0 # Width of dialog window. declare DIALOG_WIDTH=0 # Width of dialog window.
declare OPTION_NUMBER=0 # Number of options in dialog window. declare OPTION_NUMBER=0 # Number of options in dialog window.
declare SELECTED_OPTIONS_STRING="" # Output value from dialog window. declare SELECTED_OPTIONS_STRING="" # Output value from dialog window.
# MAIN LOGIC. # MAIN LOGIC.
# Trim leading and trailing white space for each option. # Trim leading and trailing white space for each option.
for OPTION in "${INPUT_OPTIONS[@]}"; do for OPTION in "${INPUT_OPTIONS[@]}"; do
TRIMMED_OPTIONS+=("$(echo "$OPTION" | sed 's/^[ \t]*//;s/[ \t]*$//')") TRIMMED_OPTIONS+=("$(echo "$OPTION" | sed 's/^[ \t]*//;s/[ \t]*$//')")
done done
# Find the length of the longest option to set the dialog width. # Find the length of the longest option to set the dialog width.
for OPTION in "${TRIMMED_OPTIONS[@]}"; do for OPTION in "${TRIMMED_OPTIONS[@]}"; do
if [ "${#OPTION}" -gt "$DIALOG_WIDTH" ]; then if [ "${#OPTION}" -gt "$DIALOG_WIDTH" ]; then
DIALOG_WIDTH=${#OPTION} DIALOG_WIDTH=${#OPTION}
fi fi
done done
# Apply the offset value to the dialog width. # Apply the offset value to the dialog width.
DIALOG_WIDTH=$((DIALOG_WIDTH + CHK_OPTION_WIDTH_OFFSET)) DIALOG_WIDTH=$((DIALOG_WIDTH + CHK_OPTION_WIDTH_OFFSET))
# Adjust the dialog width again if the dialog text is longer. # Adjust the dialog width again if the dialog text is longer.
if [ "$DIALOG_WIDTH" -lt $((${#DIALOG_TEXT} + TEXT_WIDTH_OFFSET)) ]; then if [ "$DIALOG_WIDTH" -lt $((${#DIALOG_TEXT} + TEXT_WIDTH_OFFSET)) ]; then
DIALOG_WIDTH="$((${#DIALOG_TEXT} + TEXT_WIDTH_OFFSET))" DIALOG_WIDTH="$((${#DIALOG_TEXT} + TEXT_WIDTH_OFFSET))"
fi fi
# Pad option text with trailing white space to left-align all options. # Pad option text with trailing white space to left-align all options.
for OPTION in "${TRIMMED_OPTIONS[@]}"; do for OPTION in "${TRIMMED_OPTIONS[@]}"; do
local PAD_LENGTH=$((DIALOG_WIDTH - CHK_OPTION_WIDTH_OFFSET - ${#OPTION})) local PAD_LENGTH=$((DIALOG_WIDTH - CHK_OPTION_WIDTH_OFFSET - ${#OPTION}))
# shellcheck disable=SC2155 # shellcheck disable=SC2155
local PADDED_OPTION="${OPTION}$(printf '%*s' $PAD_LENGTH)" local PADDED_OPTION="${OPTION}$(printf '%*s' $PAD_LENGTH)"
PADDED_OPTIONS+=("$PADDED_OPTION") PADDED_OPTIONS+=("$PADDED_OPTION")
done done
# Convert options into the appropriate format for a 'dialog' checkbox. # Convert options into the appropriate format for a 'dialog' checkbox.
for PADDED_OPTION in "${PADDED_OPTIONS[@]}"; do for PADDED_OPTION in "${PADDED_OPTIONS[@]}"; do
DIALOG_OPTIONS+=("$PADDED_OPTION" "" off) DIALOG_OPTIONS+=("$PADDED_OPTION" "" off)
done done
# Store the number of options. # Store the number of options.
OPTION_NUMBER="${#INPUT_OPTIONS[@]}" OPTION_NUMBER="${#INPUT_OPTIONS[@]}"
# Produce checkbox. # Produce checkbox.
# The output string contains options delimited by spaces. # The output string contains options delimited by spaces.
# Each option is enclosed in double quotes within the output string. # Each option is enclosed in double quotes within the output string.
# For example: '"Option 1 " "The Second Option " " Option Number 3 "' # For example: '"Option 1 " "The Second Option " " Option Number 3 "'
SELECTED_OPTIONS_STRING=$(dialog \ SELECTED_OPTIONS_STRING=$(dialog \
--keep-tite \ --keep-tite \
--clear \ --clear \
--no-shadow \ --no-shadow \
--checklist \ --checklist \
"$DIALOG_TEXT" \ "$DIALOG_TEXT" \
"$DIALOG_HEIGHT" \ "$DIALOG_HEIGHT" \
"$DIALOG_WIDTH" \ "$DIALOG_WIDTH" \
"$OPTION_NUMBER" \ "$OPTION_NUMBER" \
"${DIALOG_OPTIONS[@]}" \ "${DIALOG_OPTIONS[@]}" \
2>&1 >/dev/tty) || exit 0 2>&1 >/dev/tty) || exit 0
# Convert the output string into an array. # Convert the output string into an array.
# shellcheck disable=SC2001 # shellcheck disable=SC2001
while IFS= read -r LINE; do while IFS= read -r LINE; do
LINE="${LINE/#\"}" # Remove leading double quote. LINE="${LINE/#\"/}" # Remove leading double quote.
LINE="${LINE/%\"}" # Remove trailing double quote. LINE="${LINE/%\"/}" # Remove trailing double quote.
RETURN_ARRAY+=("$LINE") # Add to array. RETURN_ARRAY+=("$LINE") # Add to array.
done < <(echo "$SELECTED_OPTIONS_STRING" | sed 's/\" \"/\"\n\"/g') done < <(echo "$SELECTED_OPTIONS_STRING" | sed 's/\" \"/\"\n\"/g')
# Final modifications. # Final modifications.
for (( i=0; i<${#RETURN_ARRAY[@]}; i++ )); do for ((i = 0; i < ${#RETURN_ARRAY[@]}; i++)); do
# Remove white space added previously. # Remove white space added previously.
# shellcheck disable=SC2001 # shellcheck disable=SC2001
RETURN_ARRAY[i]=$(echo "${RETURN_ARRAY[i]}" | \ RETURN_ARRAY[i]=$(echo "${RETURN_ARRAY[i]}" |
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
# Remove escapes (introduced by 'dialog' if options have parentheses). # Remove escapes (introduced by 'dialog' if options have parentheses).
RETURN_ARRAY[i]=${RETURN_ARRAY[i]//\\/} # ${variable//search/replace} RETURN_ARRAY[i]=${RETURN_ARRAY[i]//\\/} # ${variable//search/replace}
done done
} }

File diff suppressed because it is too large Load Diff