diff --git a/screenshot-spmenu b/screenshot-spmenu index 18c3e04..b223022 100755 --- a/screenshot-spmenu +++ b/screenshot-spmenu @@ -3,44 +3,42 @@ # Take screenshots using wayshot/maim with spmenu # See LICENSE file for copyright and license details. -[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER="spmenu" -[ -z "$PREVIEW_IMAGE" ] && PREVIEW_IMAGE="true" -[ -z "$IMAGE_SIZE" ] && IMAGE_SIZE="500" -[ -z "$CACHE_IMAGE" ] && CACHE_IMAGE="false" -[ -z "$SCREENSHOT_CONFIG_DIR" ] && SCREENSHOT_CONFIG_DIR="$HOME/.config/screenshot-spmenu" -[ -z "$IMAGEHOST" ] && IMAGE_HOST="imgbb" -[ -z "$SHOW_CURSOR" ] && SHOW_CURSOR="true" -[ -z "$DEFAULT_SCREENSHOT_DIRECTORY" ] && DEFAULT_SCREENSHOT_DIRECTORY=$HOME/Screenshots # Default screenshot path -[ -z "$FORMAT" ] && FORMAT="+%T_%D" -[ -z "$PREFIX" ] && PREFIX="screenshot-" -[ -z "$SEND_NOTIF" ] && SEND_NOTIF="true" +PREVIEW_IMAGE="${PREVIEW_IMAGE:-true}" +IMAGE_SIZE="${IMAGE_SIZE:-500}" +CACHE_IMAGE="${CACHE_IMAGE:-false}" +SCREENSHOT_CONFIG_DIR="${SCREENSHOT_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/screenshot-spmenu}" +SHOW_CURSOR="${SHOW_CURSOR:-true}" +DEFAULT_SCREENSHOT_DIRECTORY="${DEFAULT_SCREENSHOT_DIRECTORY:-$HOME/Screenshots}" +FORMAT="${FORMAT:-+%T_%D}" +PREFIX="${PREFIX:-screenshot-}" +SEND_NOTIF="${SEND_NOTIF:-true}" -take_scr() { - sleep "$2" +# $1: File +# $2: Select or full boolean +# $3: Cursor visibility boolean +# $4: Delay in seconds +screenshot_take() { + sleep "$4" + + [ "$2" = "true" ] && [ "$x11" != "true" ] && POS="$(slurp)" if [ "$x11" = "true" ] && [ -x "$(command -v maim)" ]; then - maim -s"${ARG2}"B > "$1" || exit 1 + [ "$2" = "true" ] && sel_arg="-s $POS" + [ "$3" = "true" ] && [ "$2" != "true" ] && cursor_arg=u + + maim -s"${cursor_arg}"B > "$1" || exit 1 elif [ -x "$(command -v slurp)" ] && [ -x "$(command -v grim)" ]; then - grim -g "$(slurp)" - > "$1" || exit 1 + [ "$2" = "true" ] && sel_arg="-g $POS" + grim "$sel_arg" - > "$1" || exit 1 elif [ -x "$(command -v slurp)" ] && [ -x "$(command -v wayshot)" ]; then - wayshot --stdout -s "$(slurp)" > "$1" || exit 1 + [ "$2" = "true" ] && sel_arg="-s $POS" + wayshot --stdout "$sel_arg" > "$1" || exit 1 else printf "Failed to take screenshot: Appropriate screenshot tools not found.\n" > /dev/stderr fi -} -take_full() { - sleep "$2" - - if [ "$x11" = "true" ] && [ -x "$(command -v maim)" ]; then - maim "${ARG2}"B > "$1" || exit 1 - elif [ -x "$(command -v grim)" ]; then - grim - > "$1" || exit 1 - elif [ -x "$(command -v wayshot)" ]; then - wayshot --stdout > "$1" || exit 1 - else - printf "Failed to take screenshot: Appropriate screenshot tools not found.\n" > /dev/stderr - fi + [ "$CACHE_IMAGE" = "true" ] && CACHE_IMAGE="-gc" || CACHE_IMAGE="-ngc" + [ "$PREVIEW_IMAGE" = "true" ] && IMGPREFIX="img://" && IMG="/tmp/$PREFIX$DATE" && IMGARG="-is $IMAGE_SIZE $CACHE_IMAGE" } send_notif() { @@ -53,33 +51,20 @@ upload_image() { printf "%s\n" "$IMAGE" && return 0 || return 1 } +write_config() { mkdir -p "$SCREENSHOT_CONFIG_DIR" [ -e "$SCREENSHOT_CONFIG_DIR/screenshotrc" ] && . "$SCREENSHOT_CONFIG_DIR/screenshotrc" || cat << CONFIG > "$SCREENSHOT_CONFIG_DIR/screenshotrc" # screenshot-spmenu config file -RUNLAUNCHER="spmenu" # Run launcher to use SHOW_CURSOR="true" # Show cursor or not (true/false) -DEFAULT_SCREENSHOT_DIRECTORY="$HOME/Screenshots" # Default screenshot path +DEFAULT_SCREENSHOT_DIRECTORY="\$HOME/Screenshots" # Default screenshot path PREFIX="screenshot-" # Screenshot prefix FORMAT="+%T_%D" # Screenshot format PREVIEW_IMAGE=true IMAGE_SIZE="500" CACHE_IMAGE="false" CONFIG - -# get display protocol -if [ -n "$WAYLAND_DISPLAY" ]; then - x11=false -elif [ -n "$DISPLAY" ]; then - x11=true -else - printf "no valid display server running, is \$DISPLAY or \$WAYLAND_DISPLAY set?\n" && exit 1 -fi - -# showcursor -[ "$SHOW_CURSOR" = "true" ] && ARG2="" || ARG2=u - -mkdir -p "$DEFAULT_SCREENSHOT_DIRECTORY" +} print_help() { cat << EOF @@ -95,36 +80,62 @@ No arguments will print this help. EOF } -rm -f /tmp/screenshot* +check_args() { + # check arguments + case "$1" in + "") print_help; exit 0 ;; + "-f") screenshot_take "/tmp/$PREFIX$DATE" "false" "$SHOW_CURSOR" "0" ;; + "-s") screenshot_take "/tmp/$PREFIX$DATE" "true" "$SHOW_CURSOR" "0" ;; + *) print_help; exit 0 ;; + esac +} -# check arguments -case "$1" in - "") print_help; exit 0 ;; - "-f") take_full "/tmp/screenshot-$DATE" "0" ;; - "-s") take_scr "/tmp/screenshot-$DATE" "0" ;; - *) print_help; exit 0 ;; -esac +check_action() { + # user action + [ ! -s "/tmp/$PREFIX$DATE" ] && exit 0 + [ -e "/tmp/$PREFIX$DATE" ] && u_i="$(printf "${IMGPREFIX}${IMG}\tPNG\n${IMGPREFIX}${IMG}\tURL\n${IMGPREFIX}${IMG}\tSave\n" | spmenu -l 3 -g 1 $IMGARG -p "Copy to clipboard as:")" -[ "$CACHE_IMAGE" = "true" ] && CACHE_IMAGE="-gc" || CACHE_IMAGE="-ngc" -[ "$PREVIEW_IMAGE" = "true" ] && IMGPREFIX="img://" && IMG="/tmp/screenshot-$DATE" && IMGARG="-is $IMAGE_SIZE $CACHE_IMAGE" + if [ "$x11" = "true" ] && [ -x "$(command -v maim)" ]; then + img_clipboard_cmd="xclip -selection clipboard -t image/png" + clipboard_cmd="xclip -selection clipboard" + else + img_clipboard_cmd="wl-copy" + clipboard_cmd="wl-copy" + fi -# user action -[ ! -s "/tmp/screenshot-$DATE" ] && exit 0 -[ -e "/tmp/screenshot-$DATE" ] && u_i="$(printf "${IMGPREFIX}${IMG}\tPNG\n${IMGPREFIX}${IMG}\tURL\n${IMGPREFIX}${IMG}\tSave\n" | $RUNLAUNCHER -l 3 -g 1 $IMGARG -p "Copy to clipboard as:")" + # perform actions based on user input + case "$u_i" in + "PNG") $img_clipboard_cmd < "/tmp/$PREFIX$DATE" && send_notif "Screenshot copied to clipboard." ;; + "URL") printf "\n" | $clipboard_cmd && upload_image "/tmp/$PREFIX$DATE" | $clipboard_cmd && send_notif "Screenshot copied to clipboard." ;; + "Save") SAVEDIR=$(printf "%s/$PREFIX%s.png" "$DEFAULT_SCREENSHOT_DIRECTORY" "$DATE" | spmenu -l 1 -g 1 -p "Where do you want to save it? (Including filename)" -im) && cat /tmp/"$PREFIX$DATE" > "$SAVEDIR" && send_notif "Screenshot saved to $SAVEDIR" ;; + "") rm -f "/tmp/screenshot*"; exit 0 ;; + *) print_help "$@"; exit 0 ;; + esac +} -if [ "$x11" = "true" ] && [ -x "$(command -v maim)" ]; then - img_clipboard_cmd="xclip -selection clipboard -t image/png" - clipboard_cmd="xclip -selection clipboard" -else - img_clipboard_cmd="wl-copy" - clipboard_cmd="wl-copy" -fi +cleanup() { + rm -f /tmp/screenshot* +} -# perform actions based on user input -case "$u_i" in - "PNG") $img_clipboard_cmd < "/tmp/screenshot-$DATE" && send_notif "Screenshot copied to clipboard." ;; - "URL") printf "\n" | $clipboard_cmd && upload_image "/tmp/screenshot-$DATE" | $clipboard_cmd && send_notif "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)" -im) && cat /tmp/screenshot-"$DATE" > "$SAVEDIR" && send_notif "Screenshot saved to $SAVEDIR" ;; - "") rm -f "/tmp/screenshot*"; exit 0 ;; - *) print_help "$@"; exit 0 ;; -esac +main() { + write_config + + # get display protocol + if [ -n "$WAYLAND_DISPLAY" ]; then + x11=false + elif [ -n "$DISPLAY" ]; then + x11=true + else + printf "No valid display server running, is \$DISPLAY or \$WAYLAND_DISPLAY set?\n" > /dev/stderr && exit 1 + fi + + mkdir -p "$DEFAULT_SCREENSHOT_DIRECTORY" + + DATE="$(date "$FORMAT" | sed "s|:|-|g" | sed "s|/||g")" + + check_args "$@" + + check_action "$@" +} + +main "$@"