1
0
mirror of https://github.com/frebib/dotfiles.git synced 2024-06-14 12:57:23 +00:00

scripts/screenshot: add proper arg parsing

This fixes usage of --active and --clip together. I'm not even sure how
it worked before this tbh.

Signed-off-by: Joe Groocock <me@frebib.net>
This commit is contained in:
Joe Groocock 2020-08-11 09:04:05 +01:00
parent 0bf16821d5
commit 0d2f0f12be
Signed by: frebib
GPG Key ID: E0B16BEACFBB6A86

View File

@ -13,6 +13,29 @@ URL_PATH=https://frebib.net/s
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
params=$(getopt -o ac -l active,clip -n screenshot -- "$@")
eval set -- "$params"
unset params
while true; do
case "$1" in
'-a'|'--active')
active=1
shift
continue;;
'-c'|'--clip')
clip=1
shift
continue;;
'--')
shift
break;;
*)
echo 'getopt error' >&2
exit 1;;
esac
done
send_notification() { send_notification() {
title="$1" title="$1"
body="$2" body="$2"
@ -28,24 +51,24 @@ send_notification() {
# Make the screenshot directory # Make the screenshot directory
mkdir -p "$DIRECTORY" mkdir -p "$DIRECTORY"
out_path="$DIRECTORY/$FILENAME"
# Take screenshot geometry if [ "$active" = '1' ]; then
read -r winid geom < <(slop -f "%i %g") || : args+=(-i "$(xdotool getactivewindow)")
if [ -z "$geom" -o -z "$winid" ]; then else
# Take screenshot geometry
read -r winid geom < <(slop -f "%i %g") || :
if [ -z "$geom" -o -z "$winid" ]; then
send_notification "Screenshot error" "Failed to capture screenshot area" > /dev/null send_notification "Screenshot error" "Failed to capture screenshot area" > /dev/null
exit 1 exit 1
fi fi
args=(-g "$geom")
out_path="$DIRECTORY/$FILENAME" # If selection is the root window
args=(-g "$geom") if xwininfo -id "$winid" | grep -qw root; then
# If selection is the root window
if xwininfo -id "$winid" | grep -qw root; then
isroot=1 isroot=1
args+=(-i "$winid") args+=(-i "$winid")
elif [ "$1" == "--active" -o "$1" == "-a" ]; then fi
args+=(-i "$(xdotool getactivewindow)")
shift
fi fi
# Take the screenshot and save it # Take the screenshot and save it
@ -60,10 +83,10 @@ if [ "$isroot" != "1" ]; then
convert "$out_path" \( +clone -background black -alpha set -channel Alpha -evaluate set 60% -shadow 100x16+0+0 \) +swap -background none -layers merge +repage "$out_path" || : convert "$out_path" \( +clone -background black -alpha set -channel Alpha -evaluate set 60% -shadow 100x16+0+0 \) +swap -background none -layers merge +repage "$out_path" || :
fi fi
if [ "$1" == "--clip" -o "$1" == "-c" ]; then if [ "$clip" = '1' ]; then
# Add image to clipboard # Add image to clipboard
xclip -i "$out_path" -selection clipboard -t image/png xclip -i "$out_path" -selection clipboard -t image/png
send_notification "Screenshot captured" "$FILENAME<br><br>The image is available in the clipboard" send_notification "Screenshot captured" "$FILENAME<br><br>The image is available in the clipboard" >/dev/null
exit 0 exit 0
fi fi