completely rewrite screenshotutil

This commit is contained in:
speedie 2023-02-07 21:03:51 +01:00
parent 07c9a9b943
commit 6dcdedef67

View file

@ -1,133 +1,88 @@
#!/bin/sh #!/bin/sh
# speedwm-screenshotutil # speedwm-screenshotutil
# Built in screenshot utility for my build of speedwm # screenshot script
# Requires curl, maim and xclip.
# #
# 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 # functions for sending notifications
"") RUNLAUNCHER=spmenu ;; SENDNOTIF() { command -v notify-send > /dev/null && notify-send "$1"; }
esac
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" printf "%s\n" "$IMAGE" && return 0 || return 1
mkdir -p "$EXPORTDIR" }
# 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 <<CONFIG > "$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) 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 FORMAT="+%T_%D" # Screenshot format
CONFIG
if [ -e "$EXPORTDIR/screenshotutilrc" ]; then # showcursor
. $EXPORTDIR/screenshotutilrc [ "$SHOWCURSOR" = "true" ] && ARG2="" || ARG2=u
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
case "$SHOWCURSOR" in # make default screenshot path
"true") ARG2="" ;; mkdir -p "$DEFAULT_SCREENSHOT_DIRECTORY"
"false") ARG2=u ;;
esac
# Help argument action # Help argument action
HELP() { HELP() {
printf "speedwm-screenshotutil\n-t | Wait <second argument> seconds and then take the screenshot." printf "speedwm-screenshotutil\n\n"
printf "\n-f | Take a full screen screenshot instead of selecting manually." printf "%s - Wait <second argument> seconds and then take the screenshot.\n" "-t"
printf "\n-s | Take a screenshot, allowing the user to select a section manually." printf "%s - Take a full screen screenshot instead of selecting manually.\n" "-f"
printf "\n-o | Select a file and allow the user to perform actions with it." printf "%s - Take a screenshot, allowing the user to select a section manually.\n" "-s"
printf "\n-tf | Wait <second argument> seconds and then take a full screen screenshot." printf "%s - Select a file and allow the user to perform actions with it.\n" "-o"
printf "\n-h | View this help screen" printf "%s - Wait <second argument> seconds and then take a full screen screenshot.\n" "-t -f"
printf "\nNo arguments will print this screen.\n" printf "%s - View this help screen\n" "-h"
printf "No arguments will print this screen.\n"
} }
# Sleep argument (-t) # -t -f argument
if [ "$1" = "-t" ]; then [ "$1" = "-t" ] && [ "$2" = "-f" ] && SLEEP_AND_FULL "$@"
sleep "$2" && maim -s${ARG2}B > /tmp/screenshot-$DATE || exit 0
fi
# Open argument (-o) # check arguments
if [ "$1" = "-o" ]; then case "$1" in
if [ -e "$2" ]; then "") HELP; exit 0 ;;
cat "$2" > /tmp/screenshot-$DATE "-t") SLEEP "/tmp/screenshot-$DATE" ;;
else "-f") FULL_SCR "/tmp/screenshot-$DATE" ;;
printf "File $2 not found.\n" "-s") SEL_SCR "/tmp/screenshot-$DATE" ;;
exit 1 "-o") cat "$2" > "/tmp/screenshot-$DATE" || printf "File %s not found.\n" "$2" ;;
fi *) HELP; exit 0 ;;
fi esac
# Sleep and full argument (-tf) # user action
if [ "$1" = "-tf" ]; then [ -e "/tmp/screenshot-$DATE" ] && U_INPUT="$(printf "Image\nURL\nSave" | $RUNLAUNCHER -l 3 -p "Copy to clipboard as an: ")" || exit 1
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
# Full argument (-f) # perform actions based on user input
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
case "$U_INPUT" in case "$U_INPUT" in
"Image") cat /tmp/screenshot-$DATE | xclip -selection clipboard -t image/png && SENDNOTIF_IMG ;; "Image") cat "/tmp/screenshot-$DATE" | xclip -selection clipboard -t image/png && SENDNOTIF "Screenshot copied to clipboard." ;;
"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 ;; "URL") printf "\n" | xclip -selection clipboard && UPLOAD_IMAGE "/tmp/screenshot-$DATE" | xclip -selection clipboard && SENDNOTIF "Screenshot copied to clipboard." ;;
"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 ;; "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 esac
rm -f /tmp/screenshot* # Remove the screenshots rm -f /tmp/screenshot* # Remove the screenshots