From 0d48c462cb59fc39764b75c63727859f6d6b62f3 Mon Sep 17 00:00:00 2001 From: Colin Date: Sat, 2 Mar 2024 02:02:47 +0000 Subject: [PATCH] sane-screenshot: init --- pkgs/additional/sane-screenshot/default.nix | 6 ++ .../sane-screenshot/sane-screenshot | 65 +++++++++++++++++++ pkgs/default.nix | 1 + 3 files changed, 72 insertions(+) create mode 100644 pkgs/additional/sane-screenshot/default.nix create mode 100755 pkgs/additional/sane-screenshot/sane-screenshot diff --git a/pkgs/additional/sane-screenshot/default.nix b/pkgs/additional/sane-screenshot/default.nix new file mode 100644 index 00000000..54ad61c2 --- /dev/null +++ b/pkgs/additional/sane-screenshot/default.nix @@ -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" ]; +} diff --git a/pkgs/additional/sane-screenshot/sane-screenshot b/pkgs/additional/sane-screenshot/sane-screenshot new file mode 100755 index 00000000..1a6e07f4 --- /dev/null +++ b/pkgs/additional/sane-screenshot/sane-screenshot @@ -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 +# - + +# 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 ))" diff --git a/pkgs/default.nix b/pkgs/default.nix index cb5daca5..6a25cb6b 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -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 { };