From 5a2465fbab887139bd6f808ffcda011ae897c5fb Mon Sep 17 00:00:00 2001 From: kat witch Date: Fri, 26 Mar 2021 02:21:48 +0000 Subject: [PATCH] augumented grimshot with upload --- hosts/athame/nixos/virtualhosts.nix | 1 + pkgs/default.nix | 4 +- pkgs/kat-scrot/default.nix | 8 ++ pkgs/kat-scrot/kat-scrot.sh | 166 ++++++++++++++++++++++++++++ profiles/gui/home/packages.nix | 1 + profiles/sway/home/grimshot.sh | 151 ------------------------- profiles/sway/home/sway.nix | 12 +- 7 files changed, 188 insertions(+), 155 deletions(-) create mode 100644 pkgs/kat-scrot/default.nix create mode 100755 pkgs/kat-scrot/kat-scrot.sh delete mode 100755 profiles/sway/home/grimshot.sh diff --git a/hosts/athame/nixos/virtualhosts.nix b/hosts/athame/nixos/virtualhosts.nix index b297d799..d2efccbf 100644 --- a/hosts/athame/nixos/virtualhosts.nix +++ b/hosts/athame/nixos/virtualhosts.nix @@ -9,5 +9,6 @@ in { services.nginx.virtualHosts = { "kittywit.ch" = { root = "/var/www/kittywitch"; } // common; "athame.kittywit.ch" = { root = "/var/www/athame"; } // common; + "files.kittywit.ch" = { root = "/var/www/files"; } // common; } // witch.secrets.virtualHosts.athame; } diff --git a/pkgs/default.nix b/pkgs/default.nix index 87008dcc..0c61b484 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -35,7 +35,9 @@ let kat-weather = super.callPackage ./kat-weather { }; - kat-gpg-status =super.callPackage ./kat-gpg-status { }; + kat-gpg-status = super.callPackage ./kat-gpg-status { }; + + kat-scrot = super.callPackage ./kat-scrot { }; linuxPackagesFor = kernel: (super.linuxPackagesFor kernel).extend (_: ksuper: { diff --git a/pkgs/kat-scrot/default.nix b/pkgs/kat-scrot/default.nix new file mode 100644 index 00000000..dad75bc5 --- /dev/null +++ b/pkgs/kat-scrot/default.nix @@ -0,0 +1,8 @@ +{ + wrapShellScriptBin, + pkgs +}: + +wrapShellScriptBin "kat-scrot" ./kat-scrot.sh { + depsRuntimePath = with pkgs; [coreutils wl-clipboard slurp grim sway jq]; +} diff --git a/pkgs/kat-scrot/kat-scrot.sh b/pkgs/kat-scrot/kat-scrot.sh new file mode 100755 index 00000000..32f6b889 --- /dev/null +++ b/pkgs/kat-scrot/kat-scrot.sh @@ -0,0 +1,166 @@ +#!/bin/sh + +## Requirements: +## - `grim`: screenshot utility for wayland +## - `slurp`: to select an area +## - `swaymsg`: to read properties of current window +## - `wl-copy`: clipboard utility +## - `jq`: json utility to parse swaymsg output +## - `notify-send`: to show notifications + +getTargetDirectory() { + echo "/home/kat/media/scrots" +} + +if [ "$1" = "--notify" ]; then + NOTIFY=yes + shift 1 +else + NOTIFY=no +fi + +ACTION=${1:-usage} +SUBJECT=${2:-screen} +FILENAME="$(date -Ins).png" +FILE=${3:-$(getTargetDirectory)/$FILENAME} + +REMOTE_USER="kat" +REMOTE_SERVER="kittywit.ch" +REMOTE_PORT="62954" +REMOTE_PATH="/var/www/files/" +REMOTE_URL="https://files.kittywit.ch/" + +if [ "$ACTION" != "save" ] && [ "$ACTION" != "copy" ] && [ "$ACTION" != "check" ] && [ "$ACTION" != "upload" ]; then + echo "Usage:" + echo " kat-scrot [--notify] (copy|save|upload) [active|screen|output|area|window] [FILE]" + echo " kat-scrot check" + echo " kat-scrot usage" + echo "" + echo "Commands:" + echo " copy: Copy the screenshot data into the clipboard." + echo " upload: Uses SCP to transfer the screenshot to a remote server." + echo " save: Save the screenshot to a regular file." + echo " check: Verify if required tools are installed and exit." + echo " usage: Show this message and exit." + echo "" + echo "Targets:" + echo " active: Currently active window." + echo " screen: All visible outputs." + echo " output: Currently active output." + echo " area: Manually select a region." + echo " window: Manually select a window." + exit +fi + +notify() { + notify-send -t 3000 -a grimshot "$@" +} +notifyOk() { + [ "$NOTIFY" = "no" ] && return + + TITLE=${2:-"Screenshot"} + MESSAGE=${1:-"OK"} + notify "$TITLE" "$MESSAGE" +} +notifyError() { + if [ $NOTIFY = "yes" ]; then + TITLE=${2:-"Screenshot"} + MESSAGE=${1:-"Error taking screenshot with grim"} + notify -u critical "$TITLE" "$MESSAGE" + else + echo $1 + fi +} + +die() { + MSG=${1:-Bye} + notifyError "Error: $MSG" + exit 2 +} + +check() { + COMMAND=$1 + if command -v "$COMMAND" > /dev/null 2>&1; then + RESULT="OK" + else + RESULT="NOT FOUND" + fi + echo " $COMMAND: $RESULT" +} + +takeScreenshot() { + FILE=$1 + GEOM=$2 + OUTPUT=$3 + if [ ! -z "$OUTPUT" ]; then + grim -o "$OUTPUT" "$FILE" || die "Unable to invoke grim" + elif [ -z "$GEOM" ]; then + grim "$FILE" || die "Unable to invoke grim" + else + grim -g "$GEOM" "$FILE" || die "Unable to invoke grim" + fi +} + +if [ "$ACTION" = "check" ] ; then + echo "Checking if required tools are installed. If something is missing, install it to your system and make it available in PATH..." + check grim + check slurp + check swaymsg + check wl-copy + check jq + check notify-send + exit +elif [ "$SUBJECT" = "area" ] ; then + GEOM=$(slurp -d) + # Check if user exited slurp without selecting the area + if [ -z "$GEOM" ]; then + exit + fi + WHAT="Area" +elif [ "$SUBJECT" = "active" ] ; then + FOCUSED=$(swaymsg -t get_tree | jq -r 'recurse(.nodes[]?, .floating_nodes[]?) | select(.focused)') + GEOM=$(echo "$FOCUSED" | jq -r '.rect | "\(.x),\(.y) \(.width)x\(.height)"') + APP_ID=$(echo "$FOCUSED" | jq -r '.app_id') + WHAT="$APP_ID window" +elif [ "$SUBJECT" = "screen" ] ; then + GEOM="" + WHAT="Screen" +elif [ "$SUBJECT" = "output" ] ; then + GEOM="" + OUTPUT=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused)' | jq -r '.name') + WHAT="$OUTPUT" +elif [ "$SUBJECT" = "window" ] ; then + GEOM=$(swaymsg -t get_tree | jq -r '.. | select(.pid? and .visible?) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp) + # Check if user exited slurp without selecting the area + if [ -z "$GEOM" ]; then + exit + fi + WHAT="Window" +else + die "Unknown subject to take a screen shot from" "$SUBJECT" +fi + +if [ "$ACTION" = "copy" ] ; then + takeScreenshot - "$GEOM" "$OUTPUT" | wl-copy --type image/png || die "Clipboard error" + notifyOk "$WHAT copied to buffer" +elif [ "$ACTION" = "upload" ]; then + if takeScreenshot "$FILE" "$GEOM" "$OUTPUT"; then + if scp -P $REMOTE_PORT $FILE $REMOTE_USER@$REMOTE_SERVER:$REMOTE_PATH$FILENAME; then + echo -n $REMOTE_URL$FILENAME | wl-copy + notifyOk "Uploaded: $REMOTE_URL$FILENAME" + else + notifyError "Error uploading screenshot" + fi + else + notifyError "Error taking screenshot with grim" + fi +else + if takeScreenshot "$FILE" "$GEOM" "$OUTPUT"; then + TITLE="Screenshot of $SUBJECT" + MESSAGE=$(basename "$FILE") + notifyOk "$MESSAGE" "$TITLE" + echo $FILE + else + notifyError "Error taking screenshot with grim" + fi +fi diff --git a/profiles/gui/home/packages.nix b/profiles/gui/home/packages.nix index ddf6f6ec..ad8f7ca3 100644 --- a/profiles/gui/home/packages.nix +++ b/profiles/gui/home/packages.nix @@ -17,6 +17,7 @@ in { obs-studio niv feh + kat-scrot duc exiftool audacity diff --git a/profiles/sway/home/grimshot.sh b/profiles/sway/home/grimshot.sh deleted file mode 100755 index 58a0e66c..00000000 --- a/profiles/sway/home/grimshot.sh +++ /dev/null @@ -1,151 +0,0 @@ -#!/bin/sh - -## Grimshot: a helper for screenshots within sway -## Requirements: -## - `grim`: screenshot utility for wayland -## - `slurp`: to select an area -## - `swaymsg`: to read properties of current window -## - `wl-copy`: clipboard utility -## - `jq`: json utility to parse swaymsg output -## - `notify-send`: to show notifications -## Those are needed to be installed, if unsure, run `grimshot check` -## -## See `man 1 grimshot` or `grimshot usage` for further details. - -getTargetDirectory() { - echo "/home/kat/media/scrots" -} - -if [ "$1" = "--notify" ]; then - NOTIFY=yes - shift 1 -else - NOTIFY=no -fi - -ACTION=${1:-usage} -SUBJECT=${2:-screen} -FILE=${3:-$(getTargetDirectory)/$(date -Ins).png} - -if [ "$ACTION" != "save" ] && [ "$ACTION" != "copy" ] && [ "$ACTION" != "check" ]; then - echo "Usage:" - echo " grimshot [--notify] (copy|save) [active|screen|output|area|window] [FILE]" - echo " grimshot check" - echo " grimshot usage" - echo "" - echo "Commands:" - echo " copy: Copy the screenshot data into the clipboard." - echo " save: Save the screenshot to a regular file." - echo " check: Verify if required tools are installed and exit." - echo " usage: Show this message and exit." - echo "" - echo "Targets:" - echo " active: Currently active window." - echo " screen: All visible outputs." - echo " output: Currently active output." - echo " area: Manually select a region." - echo " window: Manually select a window." - exit -fi - -notify() { - notify-send -t 3000 -a grimshot "$@" -} -notifyOk() { - [ "$NOTIFY" = "no" ] && return - - TITLE=${2:-"Screenshot"} - MESSAGE=${1:-"OK"} - notify "$TITLE" "$MESSAGE" -} -notifyError() { - if [ $NOTIFY = "yes" ]; then - TITLE=${2:-"Screenshot"} - MESSAGE=${1:-"Error taking screenshot with grim"} - notify -u critical "$TITLE" "$MESSAGE" - else - echo $1 - fi -} - -die() { - MSG=${1:-Bye} - notifyError "Error: $MSG" - exit 2 -} - -check() { - COMMAND=$1 - if command -v "$COMMAND" > /dev/null 2>&1; then - RESULT="OK" - else - RESULT="NOT FOUND" - fi - echo " $COMMAND: $RESULT" -} - -takeScreenshot() { - FILE=$1 - GEOM=$2 - OUTPUT=$3 - if [ ! -z "$OUTPUT" ]; then - grim -o "$OUTPUT" "$FILE" || die "Unable to invoke grim" - elif [ -z "$GEOM" ]; then - grim "$FILE" || die "Unable to invoke grim" - else - grim -g "$GEOM" "$FILE" || die "Unable to invoke grim" - fi -} - -if [ "$ACTION" = "check" ] ; then - echo "Checking if required tools are installed. If something is missing, install it to your system and make it available in PATH..." - check grim - check slurp - check swaymsg - check wl-copy - check jq - check notify-send - exit -elif [ "$SUBJECT" = "area" ] ; then - GEOM=$(slurp -d) - # Check if user exited slurp without selecting the area - if [ -z "$GEOM" ]; then - exit - fi - WHAT="Area" -elif [ "$SUBJECT" = "active" ] ; then - FOCUSED=$(swaymsg -t get_tree | jq -r 'recurse(.nodes[]?, .floating_nodes[]?) | select(.focused)') - GEOM=$(echo "$FOCUSED" | jq -r '.rect | "\(.x),\(.y) \(.width)x\(.height)"') - APP_ID=$(echo "$FOCUSED" | jq -r '.app_id') - WHAT="$APP_ID window" -elif [ "$SUBJECT" = "screen" ] ; then - GEOM="" - WHAT="Screen" -elif [ "$SUBJECT" = "output" ] ; then - GEOM="" - OUTPUT=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused)' | jq -r '.name') - WHAT="$OUTPUT" -elif [ "$SUBJECT" = "window" ] ; then - GEOM=$(swaymsg -t get_tree | jq -r '.. | select(.pid? and .visible?) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp) - # Check if user exited slurp without selecting the area - if [ -z "$GEOM" ]; then - exit - fi - WHAT="Window" -else - die "Unknown subject to take a screen shot from" "$SUBJECT" -fi - -if [ "$ACTION" = "copy" ] ; then - takeScreenshot - "$GEOM" "$OUTPUT" | wl-copy --type image/png || die "Clipboard error" - notifyOk "$WHAT copied to buffer" -else - if takeScreenshot "$FILE" "$GEOM" "$OUTPUT"; then - TITLE="Screenshot of $SUBJECT" - MESSAGE=$(basename "$FILE") - notifyOk "$MESSAGE" "$TITLE" - echo $FILE - else - notifyError "Error taking screenshot with grim" - fi -fi diff --git a/profiles/sway/home/sway.nix b/profiles/sway/home/sway.nix index 8c8b4e36..e2313c3c 100644 --- a/profiles/sway/home/sway.nix +++ b/profiles/sway/home/sway.nix @@ -147,11 +147,17 @@ "${cfg.modifier}+x" = "exec ${lockCommand}"; "${cfg.modifier}+Print" = - "exec ${./grimshot.sh} --notify save screen"; + "exec ${pkgs.kat-scrot}/bin/kat-scrot --notify upload screen"; "${cfg.modifier}+Shift+Print" = - "exec ${./grimshot.sh} --notify save area"; + "exec ${pkgs.kat-scrot}/bin/kat-scrot --notify upload area"; "${cfg.modifier}+Mod1+Print" = - "exec ${./grimshot.sh} --notify save window"; + "exec ${pkgs.kat-scrot}/bin/kat-scrot --notify upload window"; + "Print" = + "exec ${pkgs.kat-scrot}/bin/kat-scrot --notify save screen"; + "Shift+Print" = + "exec ${pkgs.kat-scrot}/bin/kat-scrot --notify save area"; + "Mod1+Print" = + "exec ${pkgs.kat-scrot}/bin/kat-scrot --notify save window"; "${cfg.modifier}+i" = "move workspace to output left"; "${cfg.modifier}+o" = "move workspace to output right";