sane-screenshot: init

This commit is contained in:
Colin 2024-03-02 02:02:47 +00:00
parent f4ec09f010
commit 0d48c462cb
3 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,6 @@
{ static-nix-shell }:
static-nix-shell.mkBash {
pname = "sane-screenshot";
srcRoot = ./.;
pkgs = [ "libnotify" "swappy" "sway-contrib.grimshot" "util-linux" "wl-clipboard" ];
}

View File

@ -0,0 +1,65 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p libnotify -p swappy -p sway-contrib.grimshot -p util-linux -p wl-clipboard
# loosely inspired by reddit user u/dragobich
# - <https://www.reddit.com/r/hyprland/comments/12tal0f/comment/jh8j4sy/>
# give user this many seconds to respond before we expire the notification options
TIMEOUT=6
OUTDIR="$HOME/Pictures/Screenshots"
DATEFMT='%Y-%m-%d-%Hh%Mm%S'
notify() {
local fullPath="$1"
# replace $HOME with ~:
local shortPath="~/${fullPath#$HOME/}"
# --transient means that the notification won't appear in any notification centers (only on-screen)
action=$(notify-send --icon="$fullPath" \
--expire-time="$(( 1000*$TIMEOUT ))" --transient --urgency=normal \
--action=Edit=Edit --action=CopyPath='Copy Path' \
'Screenshot Captured' "Copied and saved to $shortPath")
# fork to handle the action so that `delayedCancel` won't try to cancel us.
handleAction "$action" "$fullPath" &
}
handleAction() {
local action="$1"
local fullPath="$2"
case "$action" in
Edit)
# the save button in swappy seems to be broken (?),
# but with --output-file it'll save on exit.
swappy --file "$fullPath" --output-file "$fullPath"
;;
CopyPath)
wl-copy "$fullPath"
;;
"")
# action was dismissed
;;
*)
printf "sane-screenshot: unknown action %q\n" "$action" >&2
;;
esac
}
delayedCancel() {
# kill the given pid after the given timeout in seconds.
# if the pid exits, then this function returns early.
local pid="$1"
waitpid --timeout="$2" "$pid" || \
kill "$pid" || \
true
}
name="$(date "+$DATEFMT").png"
dest="$OUTDIR/$name"
grimshot copy anything
wl-paste > "$dest"
notify "$dest" &
printf "screenshot copied and saved to %q\n" "$dest"
delayedCancel "$!" "$(( $TIMEOUT + 1 ))"

View File

@ -57,6 +57,7 @@ let
rtl8723cs-firmware = callPackage ./additional/rtl8723cs-firmware { };
rtl8723cs-wowlan = callPackage ./additional/rtl8723cs-wowlan { };
sane-open-desktop = callPackage ./additional/sane-open-desktop { };
sane-screenshot = callPackage ./additional/sane-screenshot { };
sane-scripts = lib.recurseIntoAttrs (callPackage ./additional/sane-scripts { });
sane-weather = callPackage ./additional/sane-weather { };
signal-desktop-from-src = callPackage ./additional/signal-desktop-from-src { };