flake/flakes/home-manager/guanranwang/common/dotfiles/bin/wrapped-slurp

57 lines
1.7 KiB
Text
Raw Normal View History

2023-09-19 00:17:43 +00:00
#!/bin/sh
# Original script based on some early version of https://github.com/moverest/sway-interactive-screenshot
# modified for use with key bindings. For now supports sway & Hyprland.
SWAY=$(echo "$SWAYSOCK")
HYPR=$(echo "$HYPRLAND_INSTANCE_SIGNATURE")
if [[ -n "$SWAY" && -n "$HYPR" ]]; then
echo "This script only works on sway or Hyprland, terminating.";
exit 1
fi
list_geometry()
{
swaymsg -t get_tree | jq -r '.. | try select(.'"$1"' and .pid) | "\(.rect.x),\(.rect.y) \(.rect.width)x\(.rect.height)''"'
}
list_geometry_hypr()
{
hyprctl -j activewindow | jq -r '"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])''"'
}
CHOICE=$1
EDIT=$2
DIR=${SCREENSHOT_DIR:=~/Pictures/Screenshots}
mkdir -p "$DIR"
if [[ -n "$SWAY" ]]; then
FOCUSED=$(list_geometry focused)
FILENAME="${DIR/#\~/$HOME}/$(date +'%Y-%m-%d-%H%M%S_sway_screenshot.png')"
case $CHOICE in
fullscreen) grim "$FILENAME" ;;
region) grim -g "$(slurp)" "$FILENAME" ;;
focused) grim -g "$FOCUSED" "$FILENAME" ;;
display) grim -o "$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name')" "$FILENAME" ;;
esac
fi
if [[ -n "$HYPR" ]]; then
FOCUSED=$(list_geometry_hypr)
FILENAME="${DIR/#\~/$HOME}/$(date +'%Y-%m-%d-%H%M%S_hypr_screenshot.png')"
case $CHOICE in
fullscreen) grim "$FILENAME" ;;
region) grim -g "$(slurp)" "$FILENAME" ;;
focused) grim -g "$FOCUSED" "$FILENAME" ;;
display) grim -o "$(hyprctl -j monitors | jq -r '.[] | select(.focused) | .name')" "$FILENAME" ;;
esac
fi
wl-copy < "$FILENAME"
notify-send "Screenshot" "File saved as <i>'$FILENAME'</i> and copied to the clipboard." -i "$FILENAME"
if [[ "$EDIT" = "edit" ]]; then
swappy -f "$FILENAME"
fi