From 6dcdedef67a2d511761e878b63ab46d6d8fe0a79 Mon Sep 17 00:00:00 2001 From: speedie Date: Tue, 7 Feb 2023 21:03:51 +0100 Subject: [PATCH] completely rewrite screenshotutil --- src/speedwm-screenshotutil | 173 ++++++++++++++----------------------- 1 file changed, 64 insertions(+), 109 deletions(-) diff --git a/src/speedwm-screenshotutil b/src/speedwm-screenshotutil index e514079..bb4a9fd 100755 --- a/src/speedwm-screenshotutil +++ b/src/speedwm-screenshotutil @@ -1,133 +1,88 @@ #!/bin/sh # speedwm-screenshotutil -# Built in screenshot utility for my build of speedwm -# Requires curl, maim and xclip. +# screenshot script # -# curl snippet by nezbednik, thank you! +# dependencies: curl, maim and xclip. +# +# curl snippet for imgbb by nezbednik, thank you! -rm -f /tmp/screenshot* +# basic functions for taking the screenshots +SLEEP() { sleep "$2" && maim -s"${ARG2}"B > "$1"; } +SLEEP_AND_FULL() { sleep "$3" && maim -"${ARG2}"B > "$1" || exit 0; } +FULL_SCR() { [ ! -e "$1" ] && maim -"${ARG2}"B > "$1" || return 0; } +SEL_SCR() { [ ! -e "$1" ] && maim -s"${ARG2}"B > "$1" || return 0; } -case "$RUNLAUNCHER" in -"") RUNLAUNCHER=spmenu ;; -esac +# functions for sending notifications +SENDNOTIF() { command -v notify-send > /dev/null && notify-send "$1"; } -BINDIR="$(dirname $(command -v speedwm-winnav))/" +# function for uploading image to imgbb or 0x0.st +UPLOAD_IMAGE() { + [ "$IMAGE_HOST" = "imgbb" ] && IMAGE="$(curl -s -F source=@"$1" -F "type=file" -F "action=upload" "https://imgbb.com/json" | sed "s/\\\\//g; s/\"/\\n/g" | grep -m 1 -A 2 url | tail -n 1)" + [ "$IMAGE_HOST" = "0x0" ] && IMAGE="$(curl -s -F file=@"$1" -F secret= https://0x0.st)" -EXPORTDIR"=$HOME/.config/speedwm" -mkdir -p "$EXPORTDIR" + printf "%s\n" "$IMAGE" && return 0 || return 1 +} -# Config +# default configuration +[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER=spmenu +[ -z "$EXPORTDIR" ] && EXPORTDIR="$HOME/.config/speedwm"; mkdir -p "$EXPORTDIR" +[ -z "$IMAGEHOST" ] && IMAGE_HOST="imgbb" # Image host (imgbb/0x0) +[ -z "$SHOWCURSOR" ] && SHOWCURSOR="true" # Show cursor or not (true/false) +[ -z "$DEFAULT_SCREENSHOT_DIRECTORY" ] && DEFAULT_SCREENSHOT_DIRECTORY=$HOME/Screenshots # Default screenshot path +[ -z "$FORMAT" ] && FORMAT="+%T_%D" # Screenshot format +[ -z "$PREFIX" ] && PREFIX="screenshot-" # Screenshot prefix + +[ -e "$EXPORTDIR/screenshotutilrc" ] && . "$EXPORTDIR/screenshotutilrc" || cat < "$EXPORTDIR/screenshotutilrc" +# speedwm-screenshotutil configuration file +RUNLAUNCHER="spmenu" # Run launcher to use +IMAGE_HOST="imgbb" # Image host (imgbb/0x0) SHOWCURSOR="true" # Show cursor or not (true/false) -DEFAULTSCREENSHOTPATH=$HOME/Screenshots # Default screenshot path +DEFAULT_SCREENSHOT_DIRECTORY="$HOME/Screenshots" # Default screenshot path +PREFIX="screenshot-" # Screenshot prefix FORMAT="+%T_%D" # Screenshot format +CONFIG -if [ -e "$EXPORTDIR/screenshotutilrc" ]; then - . $EXPORTDIR/screenshotutilrc -else - printf "SHOWCURSOR=$SHOWCURSOR # Show cursor or not (true/false)" > $EXPORTDIR/screenshotutilrc - printf "\nDEFAULTSCREENSHOTPATH=$DEFAULTSCREENSHOTPATH # Where screenshots are saved\n" >> $EXPORTDIR/screenshotutilrc - echo "FORMAT='$FORMAT' # Where screenshots are saved. : and / will be replaced with _ and nothing respectively." >> $EXPORTDIR/screenshotutilrc -fi +# showcursor +[ "$SHOWCURSOR" = "true" ] && ARG2="" || ARG2=u -case "$SHOWCURSOR" in -"true") ARG2="" ;; -"false") ARG2=u ;; -esac +# make default screenshot path +mkdir -p "$DEFAULT_SCREENSHOT_DIRECTORY" # Help argument action HELP() { - printf "speedwm-screenshotutil\n-t | Wait seconds and then take the screenshot." - printf "\n-f | Take a full screen screenshot instead of selecting manually." - printf "\n-s | Take a screenshot, allowing the user to select a section manually." - printf "\n-o | Select a file and allow the user to perform actions with it." - printf "\n-tf | Wait seconds and then take a full screen screenshot." - printf "\n-h | View this help screen" - printf "\nNo arguments will print this screen.\n" + printf "speedwm-screenshotutil\n\n" + printf "%s - Wait seconds and then take the screenshot.\n" "-t" + printf "%s - Take a full screen screenshot instead of selecting manually.\n" "-f" + printf "%s - Take a screenshot, allowing the user to select a section manually.\n" "-s" + printf "%s - Select a file and allow the user to perform actions with it.\n" "-o" + printf "%s - Wait seconds and then take a full screen screenshot.\n" "-t -f" + printf "%s - View this help screen\n" "-h" + printf "No arguments will print this screen.\n" } -# Sleep argument (-t) -if [ "$1" = "-t" ]; then - sleep "$2" && maim -s${ARG2}B > /tmp/screenshot-$DATE || exit 0 -fi +# -t -f argument +[ "$1" = "-t" ] && [ "$2" = "-f" ] && SLEEP_AND_FULL "$@" -# Open argument (-o) -if [ "$1" = "-o" ]; then - if [ -e "$2" ]; then - cat "$2" > /tmp/screenshot-$DATE - else - printf "File $2 not found.\n" - exit 1 - fi -fi +# check arguments +case "$1" in + "") HELP; exit 0 ;; + "-t") SLEEP "/tmp/screenshot-$DATE" ;; + "-f") FULL_SCR "/tmp/screenshot-$DATE" ;; + "-s") SEL_SCR "/tmp/screenshot-$DATE" ;; + "-o") cat "$2" > "/tmp/screenshot-$DATE" || printf "File %s not found.\n" "$2" ;; + *) HELP; exit 0 ;; +esac -# Sleep and full argument (-tf) -if [ "$1" = "-tf" ]; then - sleep "$2" && maim -${ARG2}B > /tmp/screenshot-$DATE || exit 0 -elif [ "$1" = "-ft" ]; then - sleep "$2" && maim -${ARG2}B > /tmp/screenshot-$DATE || exit 0 -fi +# user action +[ -e "/tmp/screenshot-$DATE" ] && U_INPUT="$(printf "Image\nURL\nSave" | $RUNLAUNCHER -l 3 -p "Copy to clipboard as an: ")" || exit 1 -# Full argument (-f) -if [ "$1" = "" ]; then - HELP ; exit 0 -else - if [ "$1" = "-f" ]; then - if [ -e "/tmp/screenshot-$DATE" ]; then - exists=true - else - maim -${ARG2}B > /tmp/screenshot-$DATE || exit 0 - fi - fi - - if [ "$1" = "-s" ]; then - if [ -e "/tmp/screenshot-$DATE" ]; then - exists=true - else - maim -s${ARG2}B > /tmp/screenshot-$DATE || exit 0 - fi - fi -fi - -# Help argument (-h) -if [ "$1" = "-h" ]; then - HELP ; exit 0 -fi - -# User action -if [ -e "/tmp/screenshot-$DATE" ]; then - U_INPUT="$(printf "Image\nURL\nSave" | $RUNLAUNCHER -l 3 -p "Copy to clipboard as an: ")" -else - exit 0 -fi - -# Send notification for URL -SENDNOTIF_URL() { - if [ -e "${BINDIR}notify-send" ]; then - notify-send "Screenshot copied to clipboard." - fi -} - -# Send notifcation for local image save -SENDNOTIF_SAVE() { - if [ -e "${BINDIR}notify-send" ]; then - notify-send "Screenshot saved to $SAVEDIR." - fi -} - -# Send notification for image copied to clipboard -SENDNOTIF_IMG() { - if [ -e "${BINDIR}notify-send" ]; then - notify-send "Screenshot copied to clipboard." - fi -} - -mkdir -p $DEFAULTSCREENSHOTPATH - -# Perform actions based on user input +# perform actions based on user input case "$U_INPUT" in -"Image") cat /tmp/screenshot-$DATE | xclip -selection clipboard -t image/png && SENDNOTIF_IMG ;; -"URL") printf "\n" | xclip -selection clipboard && curl -s -F source=@"/tmp/screenshot-$DATE" -F "type=file" -F "action=upload" "https://imgbb.com/json" | sed "s/\\\\//g; s/\"/\\n/g" | grep -m 1 -A 2 url | tail -n 1 | xclip -selection clipboard && SENDNOTIF_URL ;; -"Save") SAVEDIR=$(printf "$DEFAULTSCREENSHOTPATH/screenshot-$(date "$FORMAT" | sed "s|:|-|g" | sed "s|/||g").png" | $RUNLAUNCHER -l 1 -g 1 -p "Where do you want to save it? (Including filename)") && cat /tmp/screenshot-$DATE > $SAVEDIR && SENDNOTIF_SAVE ;; + "Image") cat "/tmp/screenshot-$DATE" | xclip -selection clipboard -t image/png && SENDNOTIF "Screenshot copied to clipboard." ;; + "URL") printf "\n" | xclip -selection clipboard && UPLOAD_IMAGE "/tmp/screenshot-$DATE" | xclip -selection clipboard && SENDNOTIF "Screenshot copied to clipboard." ;; + "Save") SAVEDIR=$(printf "%s/screenshot-%s.png" "$DEFAULT_SCREENSHOT_DIRECTORY" "$(date "$FORMAT" | sed "s|:|-|g" | sed "s|/||g")" | $RUNLAUNCHER -l 1 -g 1 -p "Where do you want to save it? (Including filename)") && cat /tmp/screenshot-"$DATE" > "$SAVEDIR" && SENDNOTIF "Screenshot saved to $SAVEDIR" ;; + "") rm -f "/tmp/screenshot*"; exit 0 ;; + *) HELP "$@"; exit 0 ;; esac rm -f /tmp/screenshot* # Remove the screenshots