Skip to content

Commit

Permalink
refactor: reorganize grim files and modularize argument parsing
Browse files Browse the repository at this point in the history
- Moved all grim related files to their own directory
- Refactored argument parsing into the `parseArgs` function for improved
clarity.
- Added `handleUnknownSubject` to handle unknown subjects explicitly.
  • Loading branch information
Qubut committed Sep 12, 2024
1 parent 06c2267 commit 656aac0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
File renamed without changes.
80 changes: 44 additions & 36 deletions grimshot → grimshot/grimshot
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,50 @@
## See `man 1 grimshot` or `grimshot usage` for further details.
. ./functional-helpers

NOTIFY=no
CURSOR=
WAIT=no

getTargetDirectory() {
test -f "${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs" &&
. "${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs"

echo "${XDG_SCREENSHOTS_DIR:-${XDG_PICTURES_DIR:-$HOME}}"
}

NOTIFY=no
CURSOR=
WAIT=no
parseArgs() {
while [ $# -gt 0 ]; do
key="$1"

case $key in
-n | --notify)
NOTIFY=yes
shift # past argument
;;
-c | --cursor)
CURSOR=yes
shift # past argument
;;
-w | --wait)
shift
WAIT="$1"
if echo "$WAIT" | grep "[^0-9]" -q; then
echo "invalid value for wait '$WAIT'" >&2
exit 3
fi
shift
;;
*) # unknown option
break # done with parsing --flags
;;
esac
done

ACTION=${1:-usage}
SUBJECT=${2:-screen}
FILE=${3:-$(getTargetDirectory)/$(date -Ins).png}

while [ $# -gt 0 ]; do
key="$1"

case $key in
-n | --notify)
NOTIFY=yes
shift # past argument
;;
-c | --cursor)
CURSOR=yes
shift # past argument
;;
-w | --wait)
shift
WAIT="$1"
if echo "$WAIT" | grep "[^0-9]" -q; then
echo "invalid value for wait '$WAIT'" >&2
exit 3
fi
shift
;;
*) # unknown option
break # done with parsing --flags
;;
esac
done

ACTION=${1:-usage}
SUBJECT=${2:-screen}
FILE=${3:-$(getTargetDirectory)/$(date -Ins).png}
}

printUsageMsg() {
echo "Usage:"
Expand Down Expand Up @@ -218,7 +221,9 @@ handleSave() {
"handleScreenshotSuccess" \
"handleScreenshotFailure"
}

handleUnknownSubject() {
die "Unknown subject to take a screenshot from" "$SUBJECT"
}
handleScreenshot() {
actionIsInvalid='[ "$ACTION" != "save" ] && [ "$ACTION" != "copy" ] && [ "$ACTION" != "savecopy" ] && [ "$ACTION" != "check" ]'
actionIsCheck='[ "$ACTION" = "check" ]'
Expand All @@ -228,7 +233,7 @@ handleScreenshot() {
subjectIsOutput='[ "$SUBJECT" = "output" ]'
subjectIsWindow='[ "$SUBJECT" = "window" ]'
subjectIsAnything='[ "$SUBJECT" = "anything" ]'

subjectIsUnknown=true
any \
"$actionIsInvalid:printUsageMsg" \
"$actionIsCheck:checkRequiredTools" \
Expand All @@ -237,7 +242,8 @@ handleScreenshot() {
"$subjectIsScreen:selectScreen" \
"$subjectIsOutput:selectOutput" \
"$subjectIsWindow:selectWindow" \
"$subjectIsAnything:selectAnything" || die "Unknown subject to take a screenshot from" "$SUBJECT"
"$subjectIsAnything:selectAnything" \
"$subjectIsUnknown:handleUnknownSubject"

wait='[ "$WAIT" != "no" ]'
when "$wait" "sleep $WAIT"
Expand All @@ -248,4 +254,6 @@ handleScreenshot() {
"handleCopy" \
"handleSave"
}

parseArgs "$@"
handleScreenshot
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 656aac0

Please sign in to comment.