diff --git a/README.md b/README.md index b792d7b..a1e6a23 100755 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ Global Variables | **INTERFACE** | Override or Detected | the GUI or TUI to use | | **GUI** | Detected | Whether the interface is a GUI not TUI | | **DETECTED_DESKTOP** | Detected | Desktop in use | +| **SCRIPT_DIALOG_CANCEL_EXIT_CODE** | Optional override | Exit code when a dialog is cancelled (default: 124) | | **NOCOLORS** | Optional override | disables otherwise-detected use of colored/bolded text basic CLI | | **NOSYMBOLS** | Optional override |disables otherwise-detected use of unicode symbols in TUIs | | **ZENITY_HEIGHT** | Optional override | height of zenity dialogs | diff --git a/script-dialog.sh b/script-dialog.sh index cf2636b..a2a519f 100755 --- a/script-dialog.sh +++ b/script-dialog.sh @@ -148,6 +148,11 @@ if echo "test" | read -ri "test" 2>/dev/null; then NO_READ_DEFAULT="" fi +# Set default cancel exit code if not already set +if [ -z "${SCRIPT_DIALOG_CANCEL_EXIT_CODE+x}" ]; then + SCRIPT_DIALOG_CANCEL_EXIT_CODE=124 +fi + ################################ # Variables @@ -588,36 +593,23 @@ function pause() { _calculate-tui-size if [ "$INTERFACE" == "whiptail" ]; then - if whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --yes-button "Continue" --no-button "Quit" --yesno "${QUESTION_SYMBOL}$MESSAGE" "$RECMD_LINES" "$RECMD_COLS"; then - return 0 - else - exit 0 - fi + whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --yes-button "Continue" --no-button "Quit" --yesno "${QUESTION_SYMBOL}$MESSAGE" "$RECMD_LINES" "$RECMD_COLS" elif [ "$INTERFACE" == "dialog" ]; then - if dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --yes-label "Continue" --no-label "Quit" --yesno "${QUESTION_SYMBOL}$MESSAGE" "$RECMD_LINES" "$RECMD_COLS"; then - return 0 - else - exit 0 - fi + dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --yes-label "Continue" --no-label "Quit" --yesno "${QUESTION_SYMBOL}$MESSAGE" "$RECMD_LINES" "$RECMD_COLS" elif [ "$INTERFACE" == "zenity" ]; then - if zenity --title "$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --question --text "$MESSAGE" --ok-label="Continue" --cancel-label="Quit"; then - return 0 - else - exit 0 - fi + zenity --title "$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --question --text "$MESSAGE" --ok-label="Continue" --cancel-label="Quit" elif [ "$INTERFACE" == "kdialog" ]; then - if kdialog --title "$GUI_TITLE" --icon "$GUI_ICON" --yes-label "Continue" --no-label "Quit" --yesno "$MESSAGE"; then - return 0 - else - exit 0 - fi + kdialog --title "$GUI_TITLE" --icon "$GUI_ICON" --yes-label "Continue" --no-label "Quit" --yesno "$MESSAGE" else echo -ne "${QUESTION_SYMBOL}${bold}$MESSAGE (press Enter to continue, q to quit): ${normal}" 3>&1 1>&2 2>&3 read -r answer - if [[ "${answer,,}" == "q" ]]; then - exit 0 - fi - return 0 + [[ "${answer,,}" != "q" ]] + fi + local exit_status=$? + + # Exit script if user chose to quit + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" fi } @@ -661,9 +653,17 @@ function yesno() { elif [ "$INTERFACE" == "zenity" ]; then zenity --title "$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --question --text "$1" answer=$? + # Exit if cancelled (zenity returns 5 for timeout, -1/255 for cancel/close) + if [ $answer -gt 1 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi elif [ "$INTERFACE" == "kdialog" ]; then kdialog --title "$GUI_TITLE" --icon "$GUI_ICON" --yesno "$1" answer=$? + # Exit if cancelled (kdialog returns values > 1 for cancel/error) + if [ $answer -gt 1 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi else echo -ne "${QUESTION_SYMBOL}${bold}$1 (y/n): ${normal}" 3>&1 1>&2 2>&3 read -r answer @@ -715,16 +715,27 @@ function inputbox() { TEST_STRING="${QUESTION_SYMBOL} $1" _calculate-tui-size + local exit_status=0 if [ "$INTERFACE" == "whiptail" ]; then INPUT=$(whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --inputbox "${SYMBOL} $1" "$RECMD_LINES" "$RECMD_COLS" "$2" 3>&1 1>&2 2>&3) + exit_status=$? elif [ "$INTERFACE" == "dialog" ]; then INPUT=$(dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --inputbox "${SYMBOL} $1" "$RECMD_LINES" "$RECMD_COLS" "$2" 3>&1 1>&2 2>&3) + exit_status=$? elif [ "$INTERFACE" == "zenity" ]; then INPUT="$(zenity --entry --title="$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --text="$1" --entry-text "$2")" + exit_status=$? elif [ "$INTERFACE" == "kdialog" ]; then INPUT="$(kdialog --title "$GUI_TITLE" --icon "$GUI_ICON" --inputbox "$1" "$2")" + exit_status=$? else read ${NO_READ_DEFAULT+-i "$2"} -rep "${SYMBOL}${bold}$1: ${normal}" INPUT + exit_status=$? + fi + + # Exit script if dialog was cancelled + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" fi echo "$INPUT" @@ -784,15 +795,34 @@ function userandpassword() { mapfile -t CREDS < <( dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --insecure --mixedform "Login:" "$RECMD_LINES" "$RECMD_COLS" 0 "Username: " 1 1 "$SUGGESTED_USERNAME" 1 11 22 0 0 "Password :" 2 1 "" 2 11 22 0 1 3>&1 1>&2 2>&3 ) elif [ "$INTERFACE" == "zenity" ]; then ENTRY=$(zenity --title="$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --password --username "$SUGGESTED_USERNAME") + local exit_status=$? CREDS[0]=$(echo "$ENTRY" | cut -d'|' -f1) CREDS[1]=$(echo "$ENTRY" | cut -d'|' -f2) + # Exit script if dialog was cancelled + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi elif [ "$INTERFACE" == "kdialog" ]; then CREDS[0]=$(inputbox "$USER_TEXT" "$SUGGESTED_USERNAME") CREDS[1]=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --password "$PASS_TEXT") else read ${NO_READ_DEFAULT+-i "$SUGGESTED_USERNAME"} -rep "${QUESTION_SYMBOL}${bold}$USER_TEXT: ${normal}" "CREDS[0]" - read -srp "${bold}${PASSWORD_SYMBOL}$PASS_TEXT: ${normal}" "CREDS[1]" + local exit_status=$? + if [ $exit_status -eq 0 ]; then + read -srp "${bold}${PASSWORD_SYMBOL}$PASS_TEXT: ${normal}" "CREDS[1]" + exit_status=$? + fi echo + # Exit script if dialog was cancelled + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi + fi + local exit_status=$? + + # Exit script if dialog was cancelled + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" fi eval "$__uservar"="'${CREDS[0]}'" @@ -830,17 +860,29 @@ function password() { TEST_STRING="${PASSWORD_SYMBOL}$1" _calculate-tui-size + local exit_status=0 if [ "$INTERFACE" == "whiptail" ]; then PASSWORD=$(whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --passwordbox "$1" "$RECMD_LINES" "$RECMD_COLS" 3>&1 1>&2 2>&3) + exit_status=$? elif [ "$INTERFACE" == "dialog" ]; then PASSWORD=$(dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --passwordbox "$1" "$RECMD_LINES" "$RECMD_COLS" 3>&1 1>&2 2>&3) + exit_status=$? elif [ "$INTERFACE" == "zenity" ]; then PASSWORD=$(zenity --title="$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --password) + exit_status=$? elif [ "$INTERFACE" == "kdialog" ]; then PASSWORD=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --password "$1") + exit_status=$? else read -srp "${PASSWORD_SYMBOL}${bold}$ACTIVITY: ${normal}" PASSWORD + exit_status=$? fi + + # Exit script if dialog was cancelled + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi + echo "$PASSWORD" } @@ -878,16 +920,27 @@ function display-file() { local height=${3-${ZENITY_HEIGHT-640}} _calculate-tui-size + local exit_status=0 if [ "$INTERFACE" == "whiptail" ]; then whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" $([ "$RECMD_SCROLL" == true ] && echo "--scrolltext") --textbox "$1" "$RECMD_LINES" "$RECMD_COLS" + exit_status=$? elif [ "$INTERFACE" == "dialog" ]; then dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --textbox "$1" "$RECMD_LINES" "$RECMD_COLS" + exit_status=$? elif [ "$INTERFACE" == "zenity" ]; then zenity --title="$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" --height="$height" --width="$width" --text-info --filename="$1" + exit_status=$? elif [ "$INTERFACE" == "kdialog" ]; then kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --textbox "$1" "$width" "$height" + exit_status=$? else less "$1" 3>&1 1>&2 2>&3 + exit_status=$? + fi + + # Exit script if dialog was cancelled + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" fi } @@ -953,10 +1006,13 @@ function checklist() { fi fi + local exit_status=0 if [ "$INTERFACE" == "whiptail" ]; then mapfile -t CHOSEN_ITEMS < <( whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" $([ "$RECMD_SCROLL" == true ] && echo "--scrolltext") --checklist "${QUESTION_SYMBOL}$TEXT" $RECMD_LINES $RECMD_COLS "$NUM_OPTIONS" "$@" 3>&1 1>&2 2>&3) + exit_status=$? elif [ "$INTERFACE" == "dialog" ]; then IFS=$'\n' read -r -d '' -a CHOSEN_LIST < <( dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" $([ "$RECMD_SCROLL" == true ] && echo "--scrolltext") --separate-output --checklist "${QUESTION_SYMBOL}$TEXT" $RECMD_LINES $RECMD_COLS "$NUM_OPTIONS" "$@" 3>&1 1>&2 2>&3) + exit_status=$? local CHOSEN_ITEMS=() for value in "${CHOSEN_LIST[@]}" @@ -979,6 +1035,7 @@ function checklist() { shift done IFS=$'|' read -r -d '' -a CHOSEN_LIST < <( zenity --title "$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" --height="${ZENITY_HEIGHT-512}" ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --list --text "$TEXT" --checklist --column "" --column "Value" --column "Description" "${OPTIONS[@]}" ) + exit_status=$? local CHOSEN_ITEMS=() for value in "${CHOSEN_LIST[@]}" @@ -988,6 +1045,7 @@ function checklist() { elif [ "$INTERFACE" == "kdialog" ]; then mapfile -t CHOSEN_ITEMS < <( kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --checklist "$TEXT" "$@") + exit_status=$? else printf "%s\n $TEXT:\n" "${QUESTION_SYMBOL}$ACTIVITY" 3>&1 1>&2 2>&3 local CHOSEN_ITEMS=() @@ -1003,6 +1061,11 @@ function checklist() { done fi + # Exit script if dialog was cancelled + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi + echo "${CHOSEN_ITEMS[@]}" } @@ -1069,10 +1132,21 @@ function radiolist() { fi fi + local exit_status=0 if [ "$INTERFACE" == "whiptail" ]; then CHOSEN_ITEM=$( whiptail --clear --backtitle "$APP_NAME" --title "$ACTIVITY" $([ "$RECMD_SCROLL" == true ] && echo "--scrolltext") --radiolist "${QUESTION_SYMBOL}$TEXT" $RECMD_LINES $RECMD_COLS "$NUM_OPTIONS" "$@" 3>&1 1>&2 2>&3) + exit_status=$? + # For TUI interfaces, empty response indicates cancel + if [ $exit_status -ne 0 ] || [[ -z "$CHOSEN_ITEM" ]]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi elif [ "$INTERFACE" == "dialog" ]; then CHOSEN_ITEM=$( dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" $([ "$RECMD_SCROLL" == true ] && echo "--scrolltext") --quoted --radiolist "${QUESTION_SYMBOL}$TEXT" $RECMD_LINES $RECMD_COLS "$NUM_OPTIONS" "$@" 3>&1 1>&2 2>&3) + exit_status=$? + # For TUI interfaces, empty response indicates cancel + if [ $exit_status -ne 0 ] || [[ -z "$CHOSEN_ITEM" ]]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi elif [ "$INTERFACE" == "zenity" ]; then OPTIONS=() while test ${#} -gt 0; do @@ -1088,8 +1162,18 @@ function radiolist() { shift done CHOSEN_ITEM=$( zenity --title "$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" --height="${ZENITY_HEIGHT-512}" ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --list --text "$TEXT" --radiolist --column "" --column "Value" --column "Description" "${OPTIONS[@]}") + exit_status=$? + # For GUI interfaces, empty response indicates cancel + if [ $exit_status -ne 0 ] || [[ -z "$CHOSEN_ITEM" ]]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi elif [ "$INTERFACE" == "kdialog" ]; then CHOSEN_ITEM=$( kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --radiolist "$TEXT" "$@") + exit_status=$? + # For GUI interfaces, empty response indicates cancel + if [ $exit_status -ne 0 ] || [[ -z "$CHOSEN_ITEM" ]]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi else echo -e "${QUESTION_SYMBOL}$ACTIVITY: " 3>&1 1>&2 2>&3 OPTIONS=() @@ -1106,10 +1190,16 @@ function radiolist() { shift done read -rp "$(echo -e "${OPTIONS[*]}${QUESTION_SYMBOL}${bold}$TEXT: ${normal}")" CHOSEN_ITEM + exit_status=$? if [[ "$CHOSEN_ITEM" == "" ]]; then CHOSEN_ITEM="$DEFAULT" fi + + # For CLI interface, only check exit status + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi fi echo "$CHOSEN_ITEM" @@ -1305,40 +1395,45 @@ function filepicker() { fi fi _calculate-gui-title + local exit_status=0 if [ "$INTERFACE" == "whiptail" ]; then # shellcheck disable=SC2012 read -r -d '' -a files < <(ls -lBhpa "$1" | awk -F ' ' ' { print $9 " " $5 } ') SELECTED=$(whiptail --clear --backtitle "$APP_NAME" --title "$GUI_TITLE" --cancel-button Cancel --ok-button Select --menu "$ACTIVITY" $((8+RECMD_LINES)) $((6+RECMD_COLS)) $RECMD_LINES "${files[@]}" 3>&1 1>&2 2>&3) + exit_status=$? FILE="$1/$SELECTED" - #exitstatus=$? - #if [ $exitstatus != 0 ]; then - #echo "CANCELLED!" - #exit; - #fi - elif [ "$INTERFACE" == "dialog" ]; then FILE=$(dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --stdout --fselect "$1"/ 14 48) + exit_status=$? elif [ "$INTERFACE" == "zenity" ]; then FILE=$(zenity --title "$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --file-selection --filename "$1"/ ) + exit_status=$? elif [ "$INTERFACE" == "kdialog" ]; then if [ "$2" == "save" ]; then FILE=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --getsavefilename "$1"/ ) else #elif [ "$2" == "open" ]; then FILE=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --getopenfilename "$1"/ ) fi + exit_status=$? else read -erp "${DOCUMENT_SYMBOL}You need to $2 a file in $1/. Hit enter to browse this folder" ls -lBhpa "$1" 3>&1 1>&2 2>&3 #| less read -erp "Enter name of file to $2 in $1/: " SELECTED + exit_status=$? # TODO: Add validation - handle empty SELECTED or when SELECTED is a folder FILE=$1/$SELECTED fi + # Exit script if dialog was cancelled + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi + # Ignore choice and relaunch dialog if [[ "$SELECTED" == "./" ]]; then FILE=$(filepicker "$1" "$2") @@ -1380,24 +1475,23 @@ function folderpicker() { GUI_ICON=$XDG_ICO_FOLDER_OPEN fi _calculate-gui-title + local exit_status=0 if [ "$INTERFACE" == "whiptail" ]; then # shellcheck disable=SC2010 read -r -d '' -a files < <(ls -lBhpa "$1" | grep "^d" | awk -F ' ' ' { print $9 " " $5 } ') SELECTED=$(whiptail --clear --backtitle "$APP_NAME" --title "$GUI_TITLE" --cancel-button Cancel --ok-button Select --menu "$ACTIVITY" $((8+RECMD_LINES)) $((6+RECMD_COLS)) $RECMD_LINES "${files[@]}" 3>&1 1>&2 2>&3) + exit_status=$? FILE="$1/$SELECTED" - #exitstatus=$? - #if [ $exitstatus != 0 ]; then - #echo "CANCELLED!" - #exit; - #fi - elif [ "$INTERFACE" == "dialog" ]; then FILE=$(dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --stdout --dselect "$1"/ 14 48) + exit_status=$? elif [ "$INTERFACE" == "zenity" ]; then FILE=$(zenity --title "$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --file-selection --directory --filename "$1"/ ) + exit_status=$? elif [ "$INTERFACE" == "kdialog" ]; then FILE=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --getexistingdirectory "$1"/ ) + exit_status=$? else read -erp "${FOLDER_SYMBOL}You need to select a folder in $1/. Hit enter to browse this folder" @@ -1405,12 +1499,18 @@ function folderpicker() { ls -lBhpa "$1" | grep "^d" 3>&1 1>&2 2>&3 #| less read -erp "Enter name of file to $2 in $1/: " SELECTED + exit_status=$? # TODO: Add validation - handle empty SELECTED or parent directory (..) FILE=$1/$SELECTED fi + # Exit script if dialog was cancelled + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" + fi + echo "$FILE" } @@ -1452,19 +1552,23 @@ function datepicker() { MONTH=0 YEAR=0 + local exit_status=0 if [ "$INTERFACE" == "whiptail" ]; then local SYMBOL=$CALENDAR_SYMBOL STANDARD_DATE=$(inputbox "Input Date (DD/MM/YYYY)" "$NOW") elif [ "$INTERFACE" == "dialog" ]; then STANDARD_DATE=$(dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --stdout --calendar "${CALENDAR_SYMBOL}Choose Date" 0 40) + exit_status=$? elif [ "$INTERFACE" == "zenity" ]; then INPUT_DATE=$(zenity --title="$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --calendar "Select Date") + exit_status=$? MONTH=$(echo "$INPUT_DATE" | cut -d'/' -f1) DAY=$(echo "$INPUT_DATE" | cut -d'/' -f2) YEAR=$(echo "$INPUT_DATE" | cut -d'/' -f3) STANDARD_DATE="$DAY/$MONTH/$YEAR" elif [ "$INTERFACE" == "kdialog" ]; then INPUT_DATE=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --calendar "Select Date") + exit_status=$? TEXT_MONTH=$(echo "$INPUT_DATE" | cut -d' ' -f2) if [ "$TEXT_MONTH" == "Jan" ]; then MONTH=1 @@ -1497,6 +1601,12 @@ function datepicker() { STANDARD_DATE="$DAY/$MONTH/$YEAR" else read ${NO_READ_DEFAULT+-i "$NOW"} -rep "${CALENDAR_SYMBOL}${bold}Date (DD/MM/YYYY): ${normal}" STANDARD_DATE + exit_status=$? + fi + + # Exit script if dialog was cancelled + if [ $exit_status -ne 0 ]; then + exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE" fi echo "$STANDARD_DATE" diff --git a/test.sh b/test.sh index b9131a5..a6e2de8 100755 --- a/test.sh +++ b/test.sh @@ -1,4 +1,13 @@ #!/usr/bin/env bash +# Test script for script-dialog library +# NOTE: Cancelling any dialog will exit this script with the configured +# SCRIPT_DIALOG_CANCEL_EXIT_CODE (default: 124). This is by design to +# demonstrate the cancelability feature. +# +# Functions that return values via echo (used with command substitution) +# require the '|| exit "$?"' pattern to propagate cancellation from the +# subshell to the parent script. + SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #GUI=false; terminal=false # force relaunching as if launching from GUI without a GUI interface installed, but only do this for testing @@ -27,7 +36,7 @@ else fi ACTIVITY="Name" -NAME=$(inputbox "What's your name?" "$USER") +NAME=$(inputbox "What's your name?" "$USER") || exit "$?" message-info "Nice to meet you, $NAME" @@ -55,7 +64,7 @@ userandpassword S_USER S_PASS "$SUGGESTED_USERNAME" message-info $"So, that was:\n user: $S_USER\n password: $S_PASS" ACTIVITY="Enter Birthday" -ANSWER=$(datepicker) +ANSWER=$(datepicker) || exit "$?" message-info "Cool, it's on $ANSWER" @@ -64,7 +73,7 @@ CONFIG_OPTS=$( checklist "Select the appropriate network options for this comput "NET OUT" "Allow connections to other hosts" ON \ "NET_IN" "Allow connections from other hosts" OFF \ "LOCAL_MOUNT" "Allow mounting of local drives" OFF \ - "REMOTE_MOUNT" "Allow mounting of remote drives" OFF ) + "REMOTE_MOUNT" "Allow mounting of remote drives" OFF ) || exit "$?" message-info "So you chose to enable: ${CONFIG_OPTS[*]}" @@ -73,18 +82,18 @@ ANSWER=$(radiolist "Favorite Primary Color? " 4 \ "blue" "Blue" OFF \ "yellow" "Yellow" OFF \ "green" "Green" ON \ - "red" "Red" OFF ) + "red" "Red" OFF ) || exit "$?" message-info "So you like $ANSWER, neat." -ANSWER=$(filepicker "$HOME" "open") +ANSWER=$(filepicker "$HOME" "open") || exit "$?" message-info "File selected was ${ANSWER[*]}" ACTIVITY="Test Script" display-file "$0" -ANSWER=$(folderpicker "$HOME") +ANSWER=$(folderpicker "$HOME") || exit "$?" message-info "Folder selected was ${ANSWER[*]}"