[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
rev: v3.8.0-1
hooks:
- id: shfmt
- id: shfmt
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.10.0.1

View File

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

View File

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

File diff suppressed because it is too large Load Diff