Skip to content

Commit

Permalink
Merge pull request #631 from subnut/subnut/posix-sed
Browse files Browse the repository at this point in the history
Improve POSIX-compatibility of picom-trans
  • Loading branch information
yshui committed Jun 6, 2021
2 parents 86c54cc + 5394b2c commit be24c0d
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions bin/picom-trans
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,15 @@ lineno=
option=
v=

# 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 recognize or errors on. This
# allows for things like `picom-trans -5` as well
# as `picom-trans -c +5 -s` (contrived example).
# We make getopts stop on any argument it doesn't recognize
# or errors on. This allows for things like `picom-trans -5`
# as well as `picom-trans -c +5 -s` (contrived example).
while test $# -gt 0; do
# Reset option index
OPTIND=1

# Read options
while getopts 'hscrtdgn:w:o:-:' option "$@"; do
while getopts ':hscrtdgn:w:o:-:' option "$@"; do
if test "$option" = '-'; then
case "$OPTARG" in
help | select | current | reset | toggle | delete | get)
Expand All @@ -102,7 +96,7 @@ while test $# -gt 0; do
OPTIND=$((OPTIND + 1))
;;
name=* | window=* | opacity=*)
v=$(echo "$OPTARG" | sed 's/^[^=]\+=//')
v=$(echo "$OPTARG" | sed -E 's/^[^=]+=//')
;;
*)
echo "$0: illegal option $OPTARG" >& 2
Expand All @@ -127,7 +121,7 @@ while test $# -gt 0; do
n) wprefix='-name'; window=$OPTARG ;;
w) wprefix='-id'; window=$OPTARG ;;
o) opacity=$OPTARG ;;
\?) exit 1 ;;
\?) break ;;
esac
done

Expand All @@ -137,10 +131,10 @@ while test $# -gt 0; do
done

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

# Validate opacity value
if test -z "$action" && ! echo "$opacity" | grep -q '^[+-]\?[0-9]\+$'; then
if test -z "$action" && ! echo "$opacity" | grep -qE '^[+-]?[0-9]+$'; then
echo "Invalid opacity specified: $opacity."
exit 1
fi
Expand Down Expand Up @@ -213,7 +207,7 @@ fi

# Get current opacity.
cur=$(xprop -id "$topmost" -notype _NET_WM_WINDOW_OPACITY \
| sed 's/^.*\b\([0-9]\+\).*$\|^.*$/\1/')
| sed -E 's/^_NET_WM_WINDOW_OPACITY = ([0-9]*)$|^.*$/\1/')

# Remove the opacity property.
if test x"$action" = x'delete' -o \( x"$action" = x'toggle' -a -n "$cur" \); then
Expand Down

0 comments on commit be24c0d

Please sign in to comment.