140 lines
4 KiB
Bash
Executable file
140 lines
4 KiB
Bash
Executable file
#!/usr/bin/env sh
|
|
|
|
AVAILABLE_MODES=(output window region)
|
|
|
|
function Help() {
|
|
cat <<EOF
|
|
Usage: hyprshot [options ..] -m [mode] -- [command]
|
|
|
|
Hyprshot is an utility to easily take screenshot in Hyprland using your mouse.
|
|
|
|
It allows taking screenshots of windows, regions and monitors which are saved to a folder of your choosing and copied to your clipboard.
|
|
|
|
Options:
|
|
-h, --help show help message
|
|
-m, --mode one of: output, window, region
|
|
-o, --output-folder directory in which to save screenshot
|
|
-f, --filename the file name of the resulting screenshot
|
|
-d, --debug print debug information
|
|
--clipboard-only copy screenshot to clipboard and don't save image in disk
|
|
-- [command] open screenshot with a command of your choosing. e.g. hyprshot -m window -- mirage
|
|
|
|
Modes:
|
|
output take screenshot of an entire monitor
|
|
window take screenshot of an open window
|
|
region take screenshot of selected region
|
|
EOF
|
|
}
|
|
|
|
function Print() {
|
|
[ $DEBUG -eq 1 ] && printf "$@" >&2
|
|
}
|
|
|
|
function save_geometry() {
|
|
[ -z "${1}" ] && Print "no geometry\n" && exit 1;
|
|
|
|
if [ $CLIPBOARD -eq 0 ]; then
|
|
mkdir -p "$SAVEDIR"
|
|
grim -g "${1}" "$SAVE_FULLPATH"
|
|
local output="$SAVE_FULLPATH"
|
|
wl-copy < "$output"
|
|
notify-send "Screenshot saved" \
|
|
"Image saved in <i>$output</i> and copied to the clipboard." \
|
|
-i "$output"
|
|
[ -z "$COMMAND" ] || {
|
|
"$COMMAND" "$output"
|
|
}
|
|
else
|
|
wl-copy < <(grim -g "${1}" -)
|
|
fi
|
|
}
|
|
|
|
function begin_grab() {
|
|
local option=$1
|
|
case $option in
|
|
output)
|
|
local geometry=`slurp -or`
|
|
;;
|
|
region)
|
|
local geometry=`slurp -d`
|
|
;;
|
|
window)
|
|
local geometry=`grab_window`
|
|
;;
|
|
esac
|
|
save_geometry "${geometry}"
|
|
}
|
|
|
|
function grab_window() {
|
|
local clients=`hyprctl -j clients | jq -r '[.[] | select(.workspace.id | contains('$(hyprctl -j monitors | jq -r 'map(.activeWorkspace.id) | join(",")')'))] | reverse'`
|
|
# Generate boxes for each visible window and send that to slurp
|
|
# through stdin
|
|
local boxes="$(echo $clients | jq -r '.[] | "\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"')"
|
|
Print "Boxes:\n%s\n" "$boxes"
|
|
slurp -r <<< "$boxes" 2>/dev/null
|
|
}
|
|
|
|
function args() {
|
|
local options=$(getopt -o hf:o:m:d --long help,filename:,output-folder:,mode:,clipboard-only,debug -- "$@")
|
|
[ $? -eq 0 ] || {
|
|
Print "Invalid option provided\n"
|
|
exit 2
|
|
}
|
|
eval set -- "$options"
|
|
while true; do
|
|
case "$1" in
|
|
-h | --help)
|
|
Help
|
|
exit
|
|
;;
|
|
-o | --output-folder)
|
|
shift;
|
|
SAVEDIR=$1
|
|
;;
|
|
-f | --filename)
|
|
shift;
|
|
FILENAME=$1
|
|
;;
|
|
-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
|
|
;;
|
|
-d | --debug)
|
|
DEBUG=1
|
|
;;
|
|
--)
|
|
shift # Skip -- argument
|
|
COMMAND=${@:2}
|
|
break;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
[ -z $OPTION ] && {
|
|
Print "A mode is required\n\nAvailable modes are:\n\toutput\n\tregion\n\twindow\n"
|
|
exit 2
|
|
}
|
|
}
|
|
|
|
[ -z $1 ] && {
|
|
Help
|
|
exit
|
|
}
|
|
|
|
CLIPBOARD=0
|
|
DEBUG=0
|
|
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
|