Skip to content

Commit a7aa508

Browse files
committed
fix: more robust error checking
1 parent e668726 commit a7aa508

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

‎hyprshot‎

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env sh
22

3+
set -e
4+
35
AVAILABLE_MODES=(output window region)
46

57
function Help() {
@@ -28,19 +30,24 @@ EOF
2830
}
2931

3032
function Print() {
31-
[ $DEBUG -eq 1 ] && printf "$@" >&2
33+
if [ $DEBUG -eq 0 ]; then
34+
return 0
35+
fi
36+
37+
1>&2 printf "$@"
3238
}
3339

3440
function send_notification() {
35-
[ $SILENT -eq 0 ] && {
36-
notify-send "Screenshot saved" \
37-
"Image saved in ${1} and copied to the clipboard." \
38-
-i "${1}"
39-
}
41+
if [ $SILENT -eq 1 ]; then
42+
return 0
43+
fi
44+
notify-send "Screenshot saved" \
45+
"Image saved in ${1} and copied to the clipboard." \
46+
-i "${1}"
4047
}
4148

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

4552
if [ $CLIPBOARD -eq 0 ]; then
4653
mkdir -p "$SAVEDIR"
@@ -63,10 +70,10 @@ function begin_grab() {
6370
local option=$1
6471
case $option in
6572
output)
66-
local geometry=`slurp -or`
73+
local geometry=`grab_output`
6774
;;
6875
region)
69-
local geometry=`slurp -d`
76+
local geometry=`grab_region`
7077
;;
7178
window)
7279
local geometry=`grab_window`
@@ -75,6 +82,14 @@ function begin_grab() {
7582
save_geometry "${geometry}"
7683
}
7784

85+
function grab_output() {
86+
slurp -or
87+
}
88+
89+
function grab_region() {
90+
slurp -d
91+
}
92+
7893
function grab_window() {
7994
local monitors=`hyprctl -j monitors`
8095
local clients=`hyprctl -j clients | jq -r '[.[] | select(.workspace.id | contains('$(echo $monitors | jq -r 'map(.activeWorkspace.id) | join(",")')'))]'`
@@ -89,11 +104,8 @@ function grab_window() {
89104

90105
function args() {
91106
local options=$(getopt -o hf:o:m:ds --long help,filename:,output-folder:,mode:,clipboard-only,debug,silent -- "$@")
92-
[ $? -eq 0 ] || {
93-
Print "Invalid option provided\n"
94-
exit 2
95-
}
96107
eval set -- "$options"
108+
97109
while true; do
98110
case "$1" in
99111
-h | --help)
@@ -111,11 +123,6 @@ function args() {
111123
-m | --mode)
112124
shift;
113125
echo "${AVAILABLE_MODES[@]}" | grep -wq $1
114-
local check=$?
115-
[ $check -eq 0 ] || {
116-
Print "Unknown mode: %s\n\nAvailable modes are:\n\toutput\n\tregion\n\twindow\n" "$1"
117-
exit 2
118-
}
119126
OPTION=$1;;
120127
--clipboard-only)
121128
CLIPBOARD=1
@@ -134,16 +141,16 @@ function args() {
134141
shift
135142
done
136143

137-
[ -z $OPTION ] && {
144+
if [ -z $OPTION ]; then
138145
Print "A mode is required\n\nAvailable modes are:\n\toutput\n\tregion\n\twindow\n"
139146
exit 2
140-
}
147+
fi
141148
}
142149

143-
[ -z $1 ] && {
150+
if [ -z $1 ]; then
144151
Help
145152
exit
146-
}
153+
fi
147154

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

154161
args $0 "$@"
162+
155163
SAVE_FULLPATH="$SAVEDIR/$FILENAME"
156164
[ $CLIPBOARD -eq 0 ] && Print "Saving in: %s\n" "$SAVE_FULLPATH"
157165
begin_grab $OPTION

0 commit comments

Comments
 (0)