Skip to content

Commit

Permalink
fix: more robust error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustash committed Nov 11, 2022
1 parent e668726 commit a7aa508
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions hyprshot
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env sh

set -e

AVAILABLE_MODES=(output window region)

function Help() {
Expand Down Expand Up @@ -28,19 +30,24 @@ EOF
}

function Print() {
[ $DEBUG -eq 1 ] && printf "$@" >&2
if [ $DEBUG -eq 0 ]; then
return 0
fi

1>&2 printf "$@"
}

function send_notification() {
[ $SILENT -eq 0 ] && {
notify-send "Screenshot saved" \
"Image saved in <i>${1}</i> and copied to the clipboard." \
-i "${1}"
}
if [ $SILENT -eq 1 ]; then
return 0
fi
notify-send "Screenshot saved" \
"Image saved in <i>${1}</i> and copied to the clipboard." \
-i "${1}"
}

function save_geometry() {
[ -z "${1}" ] && Print "no geometry\n" && exit 1;
Print "Geometry: %s\n" "${1}"

if [ $CLIPBOARD -eq 0 ]; then
mkdir -p "$SAVEDIR"
Expand All @@ -63,10 +70,10 @@ function begin_grab() {
local option=$1
case $option in
output)
local geometry=`slurp -or`
local geometry=`grab_output`
;;
region)
local geometry=`slurp -d`
local geometry=`grab_region`
;;
window)
local geometry=`grab_window`
Expand All @@ -75,6 +82,14 @@ function begin_grab() {
save_geometry "${geometry}"
}

function grab_output() {
slurp -or
}

function grab_region() {
slurp -d
}

function grab_window() {
local monitors=`hyprctl -j monitors`
local clients=`hyprctl -j clients | jq -r '[.[] | select(.workspace.id | contains('$(echo $monitors | jq -r 'map(.activeWorkspace.id) | join(",")')'))]'`
Expand All @@ -89,11 +104,8 @@ function grab_window() {

function args() {
local options=$(getopt -o hf:o:m:ds --long help,filename:,output-folder:,mode:,clipboard-only,debug,silent -- "$@")
[ $? -eq 0 ] || {
Print "Invalid option provided\n"
exit 2
}
eval set -- "$options"

while true; do
case "$1" in
-h | --help)
Expand All @@ -111,11 +123,6 @@ function args() {
-m | --mode)
shift;
echo "${AVAILABLE_MODES[@]}" | grep -wq $1
local check=$?
[ $check -eq 0 ] || {
Print "Unknown mode: %s\n\nAvailable modes are:\n\toutput\n\tregion\n\twindow\n" "$1"
exit 2
}
OPTION=$1;;
--clipboard-only)
CLIPBOARD=1
Expand All @@ -134,16 +141,16 @@ function args() {
shift
done

[ -z $OPTION ] && {
if [ -z $OPTION ]; then
Print "A mode is required\n\nAvailable modes are:\n\toutput\n\tregion\n\twindow\n"
exit 2
}
fi
}

[ -z $1 ] && {
if [ -z $1 ]; then
Help
exit
}
fi

CLIPBOARD=0
DEBUG=0
Expand All @@ -152,6 +159,7 @@ FILENAME="$(date +'%Y-%m-%d-%H%M%S_hyprshot.png')"
[ -z "$HYPRSHOT_DIR" ] && SAVEDIR=${XDG_PICTURES_DIR:=~} || SAVEDIR=${HYPRSHOT_DIR}

args $0 "$@"

SAVE_FULLPATH="$SAVEDIR/$FILENAME"
[ $CLIPBOARD -eq 0 ] && Print "Saving in: %s\n" "$SAVE_FULLPATH"
begin_grab $OPTION

0 comments on commit a7aa508

Please sign in to comment.