Support cursor capture in grimshot

Refactor argument parser

Bring back `sh` compatibility

Default to NOTIFY=no
This commit is contained in:
Alice Carroll 2022-03-31 19:28:16 +03:00 committed by Simon Ser
parent 2dace6b824
commit a5f01a0e04
3 changed files with 43 additions and 21 deletions

View file

@ -13,18 +13,32 @@
## See `man 1 grimshot` or `grimshot usage` for further details. ## See `man 1 grimshot` or `grimshot usage` for further details.
getTargetDirectory() { getTargetDirectory() {
test -f ${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs && \ test -f "${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs" && \
. ${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs . "${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs"
echo ${XDG_SCREENSHOTS_DIR:-${XDG_PICTURES_DIR:-$HOME}} echo "${XDG_SCREENSHOTS_DIR:-${XDG_PICTURES_DIR:-$HOME}}"
} }
if [ "$1" = "--notify" ]; then NOTIFY=no
NOTIFY=yes CURSOR=
shift 1
else while [ $# -gt 0 ]; do
NOTIFY=no key="$1"
fi
case $key in
-n|--notify)
NOTIFY=yes
shift # past argument
;;
-c|--cursor)
CURSOR=yes
shift # past argument
;;
*) # unknown option
break # done with parsing --flags
;;
esac
done
ACTION=${1:-usage} ACTION=${1:-usage}
SUBJECT=${2:-screen} SUBJECT=${2:-screen}
@ -32,7 +46,7 @@ FILE=${3:-$(getTargetDirectory)/$(date -Ins).png}
if [ "$ACTION" != "save" ] && [ "$ACTION" != "copy" ] && [ "$ACTION" != "check" ]; then if [ "$ACTION" != "save" ] && [ "$ACTION" != "copy" ] && [ "$ACTION" != "check" ]; then
echo "Usage:" echo "Usage:"
echo " grimshot [--notify] (copy|save) [active|screen|output|area|window] [FILE|-]" echo " grimshot [--notify] [--cursor] (copy|save) [active|screen|output|area|window] [FILE|-]"
echo " grimshot check" echo " grimshot check"
echo " grimshot usage" echo " grimshot usage"
echo "" echo ""
@ -67,7 +81,7 @@ notifyError() {
MESSAGE=${1:-"Error taking screenshot with grim"} MESSAGE=${1:-"Error taking screenshot with grim"}
notify -u critical "$TITLE" "$MESSAGE" notify -u critical "$TITLE" "$MESSAGE"
else else
echo $1 echo "$1"
fi fi
} }
@ -91,12 +105,12 @@ takeScreenshot() {
FILE=$1 FILE=$1
GEOM=$2 GEOM=$2
OUTPUT=$3 OUTPUT=$3
if [ ! -z "$OUTPUT" ]; then if [ -n "$OUTPUT" ]; then
grim -o "$OUTPUT" "$FILE" || die "Unable to invoke grim" grim ${CURSOR:+-c} -o "$OUTPUT" "$FILE" || die "Unable to invoke grim"
elif [ -z "$GEOM" ]; then elif [ -z "$GEOM" ]; then
grim "$FILE" || die "Unable to invoke grim" grim ${CURSOR:+-c} "$FILE" || die "Unable to invoke grim"
else else
grim -g "$GEOM" "$FILE" || die "Unable to invoke grim" grim ${CURSOR:+-c} -g "$GEOM" "$FILE" || die "Unable to invoke grim"
fi fi
} }
@ -147,7 +161,7 @@ else
TITLE="Screenshot of $SUBJECT" TITLE="Screenshot of $SUBJECT"
MESSAGE=$(basename "$FILE") MESSAGE=$(basename "$FILE")
notifyOk "$MESSAGE" "$TITLE" notifyOk "$MESSAGE" "$TITLE"
echo $FILE echo "$FILE"
else else
notifyError "Error taking screenshot with grim" notifyError "Error taking screenshot with grim"
fi fi

View file

@ -1,11 +1,11 @@
.\" Generated by scdoc 1.11.1 .\" Generated by scdoc 1.11.2
.\" Complete documentation for this program is not available as a GNU info page .\" Complete documentation for this program is not available as a GNU info page
.ie \n(.g .ds Aq \(aq .ie \n(.g .ds Aq \(aq
.el .ds Aq ' .el .ds Aq '
.nh .nh
.ad l .ad l
.\" Begin generated content: .\" Begin generated content:
.TH "grimshot" "1" "2021-02-23" .TH "grimshot" "1" "2022-03-31"
.P .P
.SH NAME .SH NAME
.P .P
@ -13,7 +13,7 @@ grimshot - a helper for screenshots within sway
.P .P
.SH SYNOPSIS .SH SYNOPSIS
.P .P
\fBgrimshot\fR [--notify] (copy|save) [TARGET] [FILE] \fBgrimshot\fR [--notify] [--cursor] (copy|save) [TARGET] [FILE]
.br .br
\fBgrimshot\fR check \fBgrimshot\fR check
.br .br
@ -26,12 +26,17 @@ grimshot - a helper for screenshots within sway
Show notifications to the user that a screenshot has been taken.\& Show notifications to the user that a screenshot has been taken.\&
.P .P
.RE .RE
\fB--cursor\fR
.RS 4
Include cursors in the screenshot.\&
.P
.RE
\fBsave\fR \fBsave\fR
.RS 4 .RS 4
Save the screenshot into a regular file.\& Grimshot will write images Save the screenshot into a regular file.\& Grimshot will write images
files to \fBXDG_SCREENSHOTS_DIR\fR if this is set (or defined files to \fBXDG_SCREENSHOTS_DIR\fR if this is set (or defined
in \fBuser-dirs.\&dir\fR), or otherwise fall back to \fBXDG_PICTURES_DIR\fR.\& in \fBuser-dirs.\&dir\fR), or otherwise fall back to \fBXDG_PICTURES_DIR\fR.\&
Set FILE to '-' to pipe the output to STDOUT.\& Set FILE to '\&-'\& to pipe the output to STDOUT.\&
.P .P
.RE .RE
\fBcopy\fR \fBcopy\fR

View file

@ -6,7 +6,7 @@ grimshot - a helper for screenshots within sway
# SYNOPSIS # SYNOPSIS
*grimshot* [--notify] (copy|save) [TARGET] [FILE]++ *grimshot* [--notify] [--cursor] (copy|save) [TARGET] [FILE]++
*grimshot* check++ *grimshot* check++
*grimshot* usage *grimshot* usage
@ -15,6 +15,9 @@ grimshot - a helper for screenshots within sway
*--notify* *--notify*
Show notifications to the user that a screenshot has been taken. Show notifications to the user that a screenshot has been taken.
*--cursor*
Include cursors in the screenshot.
*save* *save*
Save the screenshot into a regular file. Grimshot will write images Save the screenshot into a regular file. Grimshot will write images
files to *XDG_SCREENSHOTS_DIR* if this is set (or defined files to *XDG_SCREENSHOTS_DIR* if this is set (or defined