mirror of
https://github.com/ItsDrike/dotfiles.git
synced 2024-12-26 13:14:35 +00:00
Format hyprland-screenshot script
This commit is contained in:
parent
f871b7b427
commit
db58721f45
|
@ -1,19 +1,18 @@
|
||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
|
|
||||||
# Inspired by grimblast (https://github.com/hyprwm/contrib/blob/main/grimblast/grimblast)
|
# Inspired by grimblast (https://github.com/hyprwm/contrib/blob/main/grimblast/grimblast)
|
||||||
|
|
||||||
# Helper functions
|
# Helper functions
|
||||||
|
|
||||||
die() {
|
die() {
|
||||||
MSG="${1}"
|
MSG="${1}"
|
||||||
ERR_CODE="${2:-1}"
|
ERR_CODE="${2:-1}"
|
||||||
URGENCY="${3:-critical}"
|
URGENCY="${3:-critical}"
|
||||||
|
|
||||||
>&2 echo "$MSG"
|
if [ "$NOTIFY" = "yes" ]; then
|
||||||
if [ "$NOTIFY" = "yes" ]; then
|
notify-send -a screenshot -u "$URGENCY" "Error ($ERR_CODE)" "$MSG"
|
||||||
notify-send -a screenshot -u "$URGENCY" "Error ($ERR_CODE)" "$MSG"
|
fi
|
||||||
fi
|
exit "$ERR_CODE"
|
||||||
exit "$ERR_CODE"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Argument parsing
|
# Argument parsing
|
||||||
|
@ -27,9 +26,9 @@ EDIT=no
|
||||||
DELAY=0
|
DELAY=0
|
||||||
|
|
||||||
while [ "${1-}" ]; do
|
while [ "${1-}" ]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-h | --help)
|
-h | --help)
|
||||||
>&2 cat <<EOF
|
>&2 cat <<EOF
|
||||||
screenshot taking utility script, allowing for easy all-in-one solution for
|
screenshot taking utility script, allowing for easy all-in-one solution for
|
||||||
controlling how a screenshot should be taken.
|
controlling how a screenshot should be taken.
|
||||||
|
|
||||||
|
@ -54,163 +53,163 @@ Variables:
|
||||||
- all: Everything (all visible monitors/outputs)
|
- all: Everything (all visible monitors/outputs)
|
||||||
- area: Manually select a region
|
- area: Manually select a region
|
||||||
EOF
|
EOF
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
--notify)
|
--notify)
|
||||||
NOTIFY=yes
|
NOTIFY=yes
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
--cursor)
|
--cursor)
|
||||||
CURSOR=yes
|
CURSOR=yes
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
--edit)
|
--edit)
|
||||||
EDIT=yes
|
EDIT=yes
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
--target)
|
--target)
|
||||||
if [ -z "$TARGET" ]; then
|
if [ -z "$TARGET" ]; then
|
||||||
case "$2" in
|
case "$2" in
|
||||||
activewin | window | activemon | monitor | all | area)
|
activewin | window | activemon | monitor | all | area)
|
||||||
TARGET="$2"
|
TARGET="$2"
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
die "Invalid target (see TARGET variable in --help)"
|
die "Invalid target (see TARGET variable in --help)"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
else
|
else
|
||||||
die "Only one target can be passed."
|
die "Only one target can be passed."
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
--delay)
|
--delay)
|
||||||
case "$2" in
|
case "$2" in
|
||||||
'' | *[!0-9]*)
|
'' | *[!0-9]*)
|
||||||
die "Argument after --delay must be an amount of MILISECONDS"
|
die "Argument after --delay must be an amount of MILISECONDS"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
DELAY="$2"
|
DELAY="$2"
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
;;
|
;;
|
||||||
|
|
||||||
--copy)
|
--copy)
|
||||||
if [ -z "$SAVE_METHOD" ]; then
|
if [ -z "$SAVE_METHOD" ]; then
|
||||||
SAVE_METHOD="copy"
|
SAVE_METHOD="copy"
|
||||||
shift
|
shift
|
||||||
else
|
else
|
||||||
die "Only one method can be passed."
|
die "Only one method can be passed."
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
--save)
|
--save)
|
||||||
if [ -z "$SAVE_METHOD" ]; then
|
if [ -z "$SAVE_METHOD" ]; then
|
||||||
SAVE_METHOD="save"
|
SAVE_METHOD="save"
|
||||||
SAVE_FILE="$2"
|
SAVE_FILE="$2"
|
||||||
shift 2
|
shift 2
|
||||||
else
|
else
|
||||||
die "Only one method can be passed."
|
die "Only one method can be passed."
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
--copysave)
|
--copysave)
|
||||||
if [ -z "$SAVE_METHOD" ]; then
|
if [ -z "$SAVE_METHOD" ]; then
|
||||||
SAVE_METHOD="copysave"
|
SAVE_METHOD="copysave"
|
||||||
SAVE_FILE="$2"
|
SAVE_FILE="$2"
|
||||||
shift 2
|
shift 2
|
||||||
else
|
else
|
||||||
die "Only one method can be passed."
|
die "Only one method can be passed."
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
die "Unrecognized argument: $1"
|
die "Unrecognized argument: $1"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
# Screenshot functions
|
# Screenshot functions
|
||||||
|
|
||||||
takeScreenshot() {
|
takeScreenshot() {
|
||||||
FILE="$1"
|
FILE="$1"
|
||||||
GEOM="$2"
|
GEOM="$2"
|
||||||
|
|
||||||
ARGS=()
|
ARGS=()
|
||||||
[ "$CURSOR" = "yes" ] && ARGS+=("-c")
|
[ "$CURSOR" = "yes" ] && ARGS+=("-c")
|
||||||
[ -n "$GEOM" ] && ARGS+=("-g" "$GEOM")
|
[ -n "$GEOM" ] && ARGS+=("-g" "$GEOM")
|
||||||
ARGS+=("$FILE")
|
ARGS+=("$FILE")
|
||||||
|
|
||||||
sleep "$DELAY"e-3
|
sleep "$DELAY"e-3
|
||||||
grim "${ARGS[@]}" || die "Unable to invoke grim"
|
grim "${ARGS[@]}" || die "Unable to invoke grim"
|
||||||
}
|
}
|
||||||
|
|
||||||
takeEditedScreenshot() {
|
takeEditedScreenshot() {
|
||||||
FILE="$1"
|
FILE="$1"
|
||||||
GEOM="$2"
|
GEOM="$2"
|
||||||
|
|
||||||
if [ "$EDIT" = "yes" ]; then
|
if [ "$EDIT" = "yes" ]; then
|
||||||
takeScreenshot - "$GEOM" | swappy -f - -o "$FILE" || die "Unable to invoke swappy"
|
takeScreenshot - "$GEOM" | swappy -f - -o "$FILE" || die "Unable to invoke swappy"
|
||||||
else
|
else
|
||||||
takeScreenshot "$FILE" "$GEOM"
|
takeScreenshot "$FILE" "$GEOM"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Obtain the geometry for screenshot to be taken at
|
# Obtain the geometry for screenshot to be taken at
|
||||||
|
|
||||||
if [ "$TARGET" = "area" ]; then
|
if [ "$TARGET" = "area" ]; then
|
||||||
GEOM="$(slurp -d)"
|
GEOM="$(slurp -d)"
|
||||||
if [ -z "$GEOM" ]; then
|
if [ -z "$GEOM" ]; then
|
||||||
die "No area selected" 2 normal
|
die "No area selected" 2 normal
|
||||||
fi
|
fi
|
||||||
WHAT="Area"
|
WHAT="Area"
|
||||||
elif [ "$TARGET" = "all" ]; then
|
elif [ "$TARGET" = "all" ]; then
|
||||||
GEOM=""
|
GEOM=""
|
||||||
WHAT="Screen"
|
WHAT="Screen"
|
||||||
elif [ "$TARGET" = "activewin" ]; then
|
elif [ "$TARGET" = "activewin" ]; then
|
||||||
FOCUSED="$(hyprctl activewindow -j)"
|
FOCUSED="$(hyprctl activewindow -j)"
|
||||||
GEOM="$(echo "$FOCUSED" | jq -r '"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"')"
|
GEOM="$(echo "$FOCUSED" | jq -r '"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"')"
|
||||||
APP_ID="$(echo "$FOCUSED" | jq -r '.class')"
|
APP_ID="$(echo "$FOCUSED" | jq -r '.class')"
|
||||||
WHAT="$APP_ID window"
|
WHAT="$APP_ID window"
|
||||||
elif [ "$TARGET" = "window" ]; then
|
elif [ "$TARGET" = "window" ]; then
|
||||||
WORKSPACES="$(hyprctl monitors -j | jq -r 'map(.activeWorkspace.id)')"
|
WORKSPACES="$(hyprctl monitors -j | jq -r 'map(.activeWorkspace.id)')"
|
||||||
WINDOWS="$(hyprctl clients -j | jq -r --argjson workspaces "$WORKSPACES" 'map(select([.workspace.id] | inside($workspaces)))')"
|
WINDOWS="$(hyprctl clients -j | jq -r --argjson workspaces "$WORKSPACES" 'map(select([.workspace.id] | inside($workspaces)))')"
|
||||||
GEOM=$(echo "$WINDOWS" | jq -r '.[] | "\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"' | slurp -r)
|
GEOM=$(echo "$WINDOWS" | jq -r '.[] | "\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"' | slurp -r)
|
||||||
if [ -z "$GEOM" ]; then
|
if [ -z "$GEOM" ]; then
|
||||||
die "No window selected" 2 normal
|
die "No window selected" 2 normal
|
||||||
fi
|
fi
|
||||||
WHAT="Window"
|
WHAT="Window"
|
||||||
elif [ "$TARGET" = "activemon" ]; then
|
elif [ "$TARGET" = "activemon" ]; then
|
||||||
ACTIVEMON="$(hyprctl monitors -j | jq -r '.[] | select(.focused == true)')"
|
ACTIVEMON="$(hyprctl monitors -j | jq -r '.[] | select(.focused == true)')"
|
||||||
GEOM="$(echo "$ACTIVEMON" | jq -r '"\(.x),\(.y) \(.width)x\(.height)"')"
|
GEOM="$(echo "$ACTIVEMON" | jq -r '"\(.x),\(.y) \(.width)x\(.height)"')"
|
||||||
WHAT="$(echo "$ACTIVEMON" | jq -r '.name')"
|
WHAT="$(echo "$ACTIVEMON" | jq -r '.name')"
|
||||||
elif [ "$TARGET" = "monitor" ]; then
|
elif [ "$TARGET" = "monitor" ]; then
|
||||||
GEOM="$(slurp -o)"
|
GEOM="$(slurp -o)"
|
||||||
if [ -z "$GEOM" ]; then
|
if [ -z "$GEOM" ]; then
|
||||||
die "No monitor selected" 2 normal
|
die "No monitor selected" 2 normal
|
||||||
fi
|
fi
|
||||||
WHAT="Monitor"
|
WHAT="Monitor"
|
||||||
else
|
else
|
||||||
if [ -z "$TARGET" ]; then
|
if [ -z "$TARGET" ]; then
|
||||||
die "No target specified!"
|
die "No target specified!"
|
||||||
else
|
else
|
||||||
die "Unknown target: $SAVE_METHOD"
|
die "Unknown target: $SAVE_METHOD"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Invoke grim and capture the screenshot
|
# Invoke grim and capture the screenshot
|
||||||
|
|
||||||
if [ "$SAVE_METHOD" = "save" ]; then
|
if [ "$SAVE_METHOD" = "save" ]; then
|
||||||
takeEditedScreenshot "$SAVE_FILE" "$GEOM"
|
takeEditedScreenshot "$SAVE_FILE" "$GEOM"
|
||||||
[ "$NOTIFY" = "yes" ] && notify-send -a screenshot "Success" "$WHAT screenshot saved" -i "$(realpath "$SAVE_FILE")"
|
[ "$NOTIFY" = "yes" ] && notify-send -a screenshot "Success" "$WHAT screenshot saved" -i "$(realpath "$SAVE_FILE")"
|
||||||
elif [ "$SAVE_METHOD" = "copy" ]; then
|
elif [ "$SAVE_METHOD" = "copy" ]; then
|
||||||
TEMP_FILE="$(mktemp --suffix=.png)"
|
TEMP_FILE="$(mktemp --suffix=.png)"
|
||||||
takeEditedScreenshot "-" "$GEOM" | tee "$TEMP_FILE" | wl-copy --type image/png || die "Clipboard error"
|
takeEditedScreenshot "-" "$GEOM" | tee "$TEMP_FILE" | wl-copy --type image/png || die "Clipboard error"
|
||||||
[ "$NOTIFY" = "yes" ] && notify-send -a screenshot "Success" "$WHAT screenshot copied" -i "$(realpath "$TEMP_FILE")" && rm "$TEMP_FILE"
|
[ "$NOTIFY" = "yes" ] && notify-send -a screenshot "Success" "$WHAT screenshot copied" -i "$(realpath "$TEMP_FILE")" && rm "$TEMP_FILE"
|
||||||
elif [ "$SAVE_METHOD" = "copysave" ]; then
|
elif [ "$SAVE_METHOD" = "copysave" ]; then
|
||||||
takeEditedScreenshot "-" "$GEOM" | tee "$SAVE_FILE" | wl-copy --type image/png || die "Clipboard error"
|
takeEditedScreenshot "-" "$GEOM" | tee "$SAVE_FILE" | wl-copy --type image/png || die "Clipboard error"
|
||||||
[ "$NOTIFY" = "yes" ] && notify-send -a screenshot "Success" "$WHAT screenshot copied and saved" -i "$(realpath "$SAVE_FILE")"
|
[ "$NOTIFY" = "yes" ] && notify-send -a screenshot "Success" "$WHAT screenshot copied and saved" -i "$(realpath "$SAVE_FILE")"
|
||||||
else
|
else
|
||||||
if [ -z "$SAVE_METHOD" ]; then
|
if [ -z "$SAVE_METHOD" ]; then
|
||||||
die "No save method specified!"
|
die "No save method specified!"
|
||||||
else
|
else
|
||||||
die "Unknown save method: $SAVE_METHOD"
|
die "Unknown save method: $SAVE_METHOD"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue