From b22faa83005f6f46dfbb032c0752427d3a440c64 Mon Sep 17 00:00:00 2001 From: Stacy Harper Date: Thu, 21 Oct 2021 11:54:18 +0200 Subject: [PATCH] Cleanup sxmo_screenshot --- scripts/appscripts/sxmo_screenshot.sh | 93 ++++++++++++++++++--------- 1 file changed, 63 insertions(+), 30 deletions(-) diff --git a/scripts/appscripts/sxmo_screenshot.sh b/scripts/appscripts/sxmo_screenshot.sh index dfeb0e7..54fce5f 100755 --- a/scripts/appscripts/sxmo_screenshot.sh +++ b/scripts/appscripts/sxmo_screenshot.sh @@ -2,41 +2,74 @@ # scrot refuses to work with double quotes # shellcheck disable=SC2016 -exitMsg(){ - echo "$1" > /dev/stderr +set -e + +exitMsg() { + printf "%s\n" "$1" > /dev/stderr notify-send "$1" exit 1 } -commandExists(){ - command -v "$1" 2>/dev/null +commandExists() { + command -v "$1" > /dev/null } -case "$(sxmo_wm.sh)" in - sway) - commandExists grim || exitMsg "grim command must be available to take a screenshot." - FILENAME="$(date +%Y-%m-%d-%T)_grim.png" - if [ "$1" = "selection" ]; then - commandExists slurp || exitMsg "slurp command must be available to make a selection." - COMMAND="notify-send 'select an area' && slurp | grim -g - ~/$FILENAME && (printf ~/$FILENAME | wl-copy)" - else - COMMAND="grim ~/$FILENAME && (printf ~/$FILENAME | wl-copy)" - fi - ;; - xorg|dwm) - commandExists scrot || exitMsg "scrot command must be available to take a screenshot" - f='$f' - if [ "$1" = "selection" ]; then - COMMAND="notify-send 'select an area' && scrot -e 'echo $f | xsel -i -b' -d 1 -s -q 1" - else - COMMAND="scrot -e 'echo $f | xsel -i -b' -d 1 -q 1" - fi - ;; - ssh) - exitMsg "cannot screenshot ssh ;)" - ;; -esac +swayscreenshot() { + commandExists grim || exitMsg "grim command must be available to take a screenshot." + if [ "$1" = selection ]; then + commandExists slurp || exitMsg "slurp command must be available to make a selection." + notify-send "select an area" + set -- grim -g "$(slurp)" + else + set -- grim + fi -eval "$COMMAND" && notify-send "screenshot saved, filename copied to clipboard" && exit 0 -exitMsg "Screenshot process failure." + "$@" "$FILENAME" +} + +xorgscreenshot() { + commandExists scrot || exitMsg "scrot command must be available to take a screenshot" + if [ "$1" = "selection" ]; then + notify-send 'select an area' + set -- scrot -d 1 -q 1 -s + else + set -- scrot -d 1 -q 1 + fi + + "$@" "$FILENAME" +} + +screenshot() { + case "$WM" in + sway) + swayscreenshot "$@" + ;; + xorg|dwm) + xorgscreenshot "$@" + ;; + ssh) + exitMsg "cannot screenshot ssh" + ;; + esac +} + +filepathoutput() { + printf %s "$FILENAME" + case "$WM" in + sway) + wl-copy "$FILENAME" + ;; + xorg|dwm) + printf %s "$FILENAME" | xsel -b -i + + ;; + esac +} + +FILENAME="${SXMO_SCREENSHOT_DIR:-$HOME/$(date +%Y-%m-%d-%T).png}" + +WM="$(sxmo_wm.sh)" + +screenshot "$@" +filepathoutput