Skip to content

Commit

Permalink
compton-trans: use a different workaround with getopts.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed May 4, 2013
1 parent e6b973f commit c9bf88c
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions bin/compton-trans
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@ wid=
topmost=
lineno=

# Workaround: replace '-5' with '~5' so as not to confuse getopts.
for v in "$@"; do
shift && set -- "$@" "$(echo "$v" | sed 's/^-\([0-9]\+%\?\)$/~\1/')"
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 options
while getopts ':scdgn:w:o:' option "$@"; do
while getopts 'scdgn:w:o:' option "$@"; do
case "$option" in
s) wprefix=''; window='' ;;
c)
Expand All @@ -61,16 +65,8 @@ while test $# -gt 0; do
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
;;
\?) exit 1 ;;
esac
LASTIND=$OPTIND
done

# Read positional arguments
Expand All @@ -79,7 +75,7 @@ while test $# -gt 0; do
done

# clean up opacity. xargs == a poor man's trim.
opacity=$(echo "$opacity" | sed 's/%//g' | xargs)
opacity=$(echo "$opacity" | xargs | sed 's/%//g' | sed 's/^~\([0-9]\+\)$/-\1/')

# Validate opacity value
if test -z "$action" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then
Expand Down

0 comments on commit c9bf88c

Please sign in to comment.