diff --git a/scripts/common.sh b/scripts/common.sh index 42454a9..c095c22 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -3,10 +3,18 @@ displayDialogBox() { case $dialogBox in whiptail) - useWhiptail "$@" + if [ "$1" = "--menu" ]; then + useWhiptailMenu "$@" + else + useWhiptail "$@" + fi ;; dialog) - useDialog "$@" + if [ "$1" = "--menu" ]; then + useDialogMenu "$@" + else + useDialog "$@" + fi ;; ?) echo "Unknown dialogBox variable" >&2 @@ -20,20 +28,49 @@ useDialog() { } useWhiptail() { - str=$(echo "$@" | grep -oP '(?<=").*?(?=")') + str="${@: -1}" width=$(calcWidth "$str") height=$(calcHeight "$str") whiptail "$@" ${height} ${width} } +formatOptions() { + options=() + for item in "$@"; do + if [ "$item" = "VALUES" ]; then + options+=("${height}") + options+=("${width}") + continue + fi + + if echo "$item" | grep -q "[0-9]" || echo "$item" | grep -q "[--]"; then + options+=("${item}") + else + options+=("\"${item}\"") + fi + done +} + +useWhiptailMenu() { + height=0; width=0 + formatOptions "$@" + whiptail "${options[@]}" +} + +useDialogMenu() { + height=9; width=60 + formatOptions "$@" + dialog "${options[@]}" +} + calcWidth() { - width=$(echo "$str" | wc -c) + width=$(echo "$1" | wc -c) echo $((${width}+8)) } calcHeight() { - newlines=$(printf "$str" | grep -c $'\n') - chars=$(echo "$str" | wc -c) + newlines=$(printf "$1" | grep -c $'\n') + chars=$(echo "$1" | wc -c) height=$(echo "$chars" "$newlines" | awk '{ x = (($1 - $2 + ($2 * 60)) / 60) printf "%d", (x == int(x)) ? x : int(x) + 1 diff --git a/scripts/linkFiles.sh b/scripts/linkFiles.sh index 190c674..0cb9a02 100755 --- a/scripts/linkFiles.sh +++ b/scripts/linkFiles.sh @@ -14,7 +14,7 @@ linkFile() { mv "$2" "${2}.backup" ln -s "$1" "$2" else - selectedOption=$(displayDialogBox --menu "File already exists: $(basename "$1"), what would you like to do?" 10 60 0 1 "Skip" 2 "Skip all" 3 "Overwrite" 4 "Overwrite all" 5 "Backup" 6 "Backup all" 3>&1 1>&2 2>&3 3>&1) + selectedOption=$(displayDialogBox --menu "File already exists: $(basename "$1"), what would you like to do?" VALUES 0 1 "Skip" 2 "Skip all" 3 "Overwrite" 4 "Overwrite all" 5 "Backup" 6 "Backup all" 3>&1 1>&2 2>&3 3>&1) if [ $? -eq 1 ]; then exit 0 fi