fix: more robust error checking
This commit is contained in:
parent
e66872686a
commit
a7aa508bb9
1 changed files with 30 additions and 22 deletions
52
hyprshot
52
hyprshot
|
@ -1,5 +1,7 @@
|
||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
AVAILABLE_MODES=(output window region)
|
AVAILABLE_MODES=(output window region)
|
||||||
|
|
||||||
function Help() {
|
function Help() {
|
||||||
|
@ -28,19 +30,24 @@ EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
function Print() {
|
function Print() {
|
||||||
[ $DEBUG -eq 1 ] && printf "$@" >&2
|
if [ $DEBUG -eq 0 ]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
1>&2 printf "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
function send_notification() {
|
function send_notification() {
|
||||||
[ $SILENT -eq 0 ] && {
|
if [ $SILENT -eq 1 ]; then
|
||||||
notify-send "Screenshot saved" \
|
return 0
|
||||||
"Image saved in <i>${1}</i> and copied to the clipboard." \
|
fi
|
||||||
-i "${1}"
|
notify-send "Screenshot saved" \
|
||||||
}
|
"Image saved in <i>${1}</i> and copied to the clipboard." \
|
||||||
|
-i "${1}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function save_geometry() {
|
function save_geometry() {
|
||||||
[ -z "${1}" ] && Print "no geometry\n" && exit 1;
|
Print "Geometry: %s\n" "${1}"
|
||||||
|
|
||||||
if [ $CLIPBOARD -eq 0 ]; then
|
if [ $CLIPBOARD -eq 0 ]; then
|
||||||
mkdir -p "$SAVEDIR"
|
mkdir -p "$SAVEDIR"
|
||||||
|
@ -63,10 +70,10 @@ function begin_grab() {
|
||||||
local option=$1
|
local option=$1
|
||||||
case $option in
|
case $option in
|
||||||
output)
|
output)
|
||||||
local geometry=`slurp -or`
|
local geometry=`grab_output`
|
||||||
;;
|
;;
|
||||||
region)
|
region)
|
||||||
local geometry=`slurp -d`
|
local geometry=`grab_region`
|
||||||
;;
|
;;
|
||||||
window)
|
window)
|
||||||
local geometry=`grab_window`
|
local geometry=`grab_window`
|
||||||
|
@ -75,6 +82,14 @@ function begin_grab() {
|
||||||
save_geometry "${geometry}"
|
save_geometry "${geometry}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function grab_output() {
|
||||||
|
slurp -or
|
||||||
|
}
|
||||||
|
|
||||||
|
function grab_region() {
|
||||||
|
slurp -d
|
||||||
|
}
|
||||||
|
|
||||||
function grab_window() {
|
function grab_window() {
|
||||||
local monitors=`hyprctl -j monitors`
|
local monitors=`hyprctl -j monitors`
|
||||||
local clients=`hyprctl -j clients | jq -r '[.[] | select(.workspace.id | contains('$(echo $monitors | jq -r 'map(.activeWorkspace.id) | join(",")')'))]'`
|
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() {
|
function args() {
|
||||||
local options=$(getopt -o hf:o:m:ds --long help,filename:,output-folder:,mode:,clipboard-only,debug,silent -- "$@")
|
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"
|
eval set -- "$options"
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-h | --help)
|
-h | --help)
|
||||||
|
@ -111,11 +123,6 @@ function args() {
|
||||||
-m | --mode)
|
-m | --mode)
|
||||||
shift;
|
shift;
|
||||||
echo "${AVAILABLE_MODES[@]}" | grep -wq $1
|
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;;
|
OPTION=$1;;
|
||||||
--clipboard-only)
|
--clipboard-only)
|
||||||
CLIPBOARD=1
|
CLIPBOARD=1
|
||||||
|
@ -134,16 +141,16 @@ function args() {
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
[ -z $OPTION ] && {
|
if [ -z $OPTION ]; then
|
||||||
Print "A mode is required\n\nAvailable modes are:\n\toutput\n\tregion\n\twindow\n"
|
Print "A mode is required\n\nAvailable modes are:\n\toutput\n\tregion\n\twindow\n"
|
||||||
exit 2
|
exit 2
|
||||||
}
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
[ -z $1 ] && {
|
if [ -z $1 ]; then
|
||||||
Help
|
Help
|
||||||
exit
|
exit
|
||||||
}
|
fi
|
||||||
|
|
||||||
CLIPBOARD=0
|
CLIPBOARD=0
|
||||||
DEBUG=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}
|
[ -z "$HYPRSHOT_DIR" ] && SAVEDIR=${XDG_PICTURES_DIR:=~} || SAVEDIR=${HYPRSHOT_DIR}
|
||||||
|
|
||||||
args $0 "$@"
|
args $0 "$@"
|
||||||
|
|
||||||
SAVE_FULLPATH="$SAVEDIR/$FILENAME"
|
SAVE_FULLPATH="$SAVEDIR/$FILENAME"
|
||||||
[ $CLIPBOARD -eq 0 ] && Print "Saving in: %s\n" "$SAVE_FULLPATH"
|
[ $CLIPBOARD -eq 0 ] && Print "Saving in: %s\n" "$SAVE_FULLPATH"
|
||||||
begin_grab $OPTION
|
begin_grab $OPTION
|
||||||
|
|
Loading…
Add table
Reference in a new issue