diff --git a/bin/compton-trans b/bin/compton-trans index bd9f00c..c352128 100755 --- a/bin/compton-trans +++ b/bin/compton-trans @@ -7,6 +7,7 @@ # # Usage: +# $ compton-trans [options] [+|-]opacity # By window id # $ compton-trans -w "$WINDOWID" 75 # By name @@ -26,6 +27,7 @@ if test -z "$(command -v xprop)" -o -z "$(command -v xwininfo)"; then fi # Variables +active= wprefix= window= opacity= @@ -36,33 +38,51 @@ wid= topmost= lineno= -# Read options -while getopts 'scdn:w:o:' option; do - case "$option" in - s) wprefix=''; window='' ;; - c) - active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ - | sed 's/^.*\(0x\S*\).*$/\1/') - wprefix='-id'; window=$active - ;; - d) delete=1 ;; - n) wprefix='-name'; window=$OPTARG ;; - w) wprefix='-id'; window=$OPTARG ;; - o) opacity=$OPTARG ;; - \?) exit 1;; - esac -done +# This takes into account the fact that getopts stops on +# any argument it doesn't recongize or errors on. This +# allows for things like `compton-trans -5` as well +# as `compton-trans -c +5 -s` (contrived example). +while test $# -gt 0; do + # Reset option index + OPTIND=1 + LASTIND=1 -# Read positional arguments -shift $(($OPTIND - 1)) -test -n "$1" && opacity=$1 + # Read options + while getopts ':scdn:w:o:' option "$@"; do + case "$option" in + s) wprefix=''; window='' ;; + c) + active=$(xprop -root -notype _NET_ACTIVE_WINDOW \ + | sed 's/^.*\(0x\S*\).*$/\1/') + wprefix='-id'; window=$active + ;; + d) delete=1 ;; + n) wprefix='-name'; window=$OPTARG ;; + w) wprefix='-id'; window=$OPTARG ;; + o) opacity=$OPTARG ;; + \?) + # For some reason single char options `-x` stop on the OPTIND after the + # argument index, whereas two or more character options (`-foo`) stop + # on the argument's index, so we use LASTIND here instead. If OPTIND is + # not equal to LASTIND, this means -x was used instead of -xx. + OPTIND=$LASTIND + break + ;; + esac + LASTIND=$OPTIND + done + + # Read positional arguments + shift $((OPTIND - 1)) + test -n "$1" && opacity=$1 && shift +done # clean up opacity. xargs == a poor man's trim. opacity=$(echo "$opacity" | sed 's/%//g' | xargs) # Validate opacity value -if test -z "$opacity" -a -z "$delete"; then - echo 'No opacity specified.' +if test -z "$delete" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then + echo "Invalid opacity specified: $opacity." exit 1 fi