fix: more robust error checking
This commit is contained in:
parent
e66872686a
commit
a7aa508bb9
1 changed files with 30 additions and 22 deletions
46
hyprshot
46
hyprshot
|
@ -1,5 +1,7 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
set -e
|
||||
|
||||
AVAILABLE_MODES=(output window region)
|
||||
|
||||
function Help() {
|
||||
|
@ -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 ] && {
|
||||
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"
|
||||
|
@ -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`
|
||||
|
@ -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(",")')'))]'`
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue