v1cont / yad

Yet Another Dialog
GNU General Public License v3.0
657 stars 58 forks source link

Way to distinguish between cancelling and an empty entry? #148

Closed bew closed 3 years ago

bew commented 3 years ago

Hello, I'm using the --entry mode to ask for a small line of text.

I'dl like to distinguish between:

I was thinking maybe a flag like --exit-ko-on-cancel that exit with a non-zero exit code when cancelling?

Misko-2083 commented 3 years ago

Default is that Cancel button always returns 1. But custom buttons with exit code can be defined.

TEXT=$(yad --entry --button="OK":2 --button="Cancel":3)
echo "exit code: $?"
[[ -z "${TEXT// }" ]] && echo "TEXT is empty or contains only whitespaces"

Checking if variable is empty:

TEXT=$(yad --entry --button="OK":2 --button="Cancel":3)
status=$?
case $status in
   0) echo "Enter you typed: ${TEXT}" ;
       [[ -z "${TEXT// }" ]] && echo "+ TEXT is empty or contains only spaces" ;;
   2) echo "Ok button press" ;
       [[ -z "${TEXT// }" ]] && echo "+ TEXT is empty or contains only spaces" ;;
   3) echo "Cancel button press" ;;
   *) echo "Window closed or escape key pressed - exit code: ${status}" ;;
esac
bew commented 3 years ago

Yess, I missed that somehow in my scripts, sorry for the noise :+1: