Simplify script significantly
This commit is contained in:
parent
a8ea8e24e5
commit
e0a2e9a240
|
@ -3,44 +3,42 @@
|
||||||
# Take screenshots using wayshot/maim with spmenu
|
# Take screenshots using wayshot/maim with spmenu
|
||||||
# See LICENSE file for copyright and license details.
|
# See LICENSE file for copyright and license details.
|
||||||
|
|
||||||
[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER="spmenu"
|
PREVIEW_IMAGE="${PREVIEW_IMAGE:-true}"
|
||||||
[ -z "$PREVIEW_IMAGE" ] && PREVIEW_IMAGE="true"
|
IMAGE_SIZE="${IMAGE_SIZE:-500}"
|
||||||
[ -z "$IMAGE_SIZE" ] && IMAGE_SIZE="500"
|
CACHE_IMAGE="${CACHE_IMAGE:-false}"
|
||||||
[ -z "$CACHE_IMAGE" ] && CACHE_IMAGE="false"
|
SCREENSHOT_CONFIG_DIR="${SCREENSHOT_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/screenshot-spmenu}"
|
||||||
[ -z "$SCREENSHOT_CONFIG_DIR" ] && SCREENSHOT_CONFIG_DIR="$HOME/.config/screenshot-spmenu"
|
SHOW_CURSOR="${SHOW_CURSOR:-true}"
|
||||||
[ -z "$IMAGEHOST" ] && IMAGE_HOST="imgbb"
|
DEFAULT_SCREENSHOT_DIRECTORY="${DEFAULT_SCREENSHOT_DIRECTORY:-$HOME/Screenshots}"
|
||||||
[ -z "$SHOW_CURSOR" ] && SHOW_CURSOR="true"
|
FORMAT="${FORMAT:-+%T_%D}"
|
||||||
[ -z "$DEFAULT_SCREENSHOT_DIRECTORY" ] && DEFAULT_SCREENSHOT_DIRECTORY=$HOME/Screenshots # Default screenshot path
|
PREFIX="${PREFIX:-screenshot-}"
|
||||||
[ -z "$FORMAT" ] && FORMAT="+%T_%D"
|
SEND_NOTIF="${SEND_NOTIF:-true}"
|
||||||
[ -z "$PREFIX" ] && PREFIX="screenshot-"
|
|
||||||
[ -z "$SEND_NOTIF" ] && SEND_NOTIF="true"
|
|
||||||
|
|
||||||
take_scr() {
|
# $1: File
|
||||||
sleep "$2"
|
# $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
|
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
|
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
|
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
|
else
|
||||||
printf "Failed to take screenshot: Appropriate screenshot tools not found.\n" > /dev/stderr
|
printf "Failed to take screenshot: Appropriate screenshot tools not found.\n" > /dev/stderr
|
||||||
fi
|
fi
|
||||||
}
|
|
||||||
|
|
||||||
take_full() {
|
[ "$CACHE_IMAGE" = "true" ] && CACHE_IMAGE="-gc" || CACHE_IMAGE="-ngc"
|
||||||
sleep "$2"
|
[ "$PREVIEW_IMAGE" = "true" ] && IMGPREFIX="img://" && IMG="/tmp/$PREFIX$DATE" && IMGARG="-is $IMAGE_SIZE $CACHE_IMAGE"
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
send_notif() {
|
send_notif() {
|
||||||
|
@ -53,33 +51,20 @@ upload_image() {
|
||||||
printf "%s\n" "$IMAGE" && return 0 || return 1
|
printf "%s\n" "$IMAGE" && return 0 || return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
write_config() {
|
||||||
mkdir -p "$SCREENSHOT_CONFIG_DIR"
|
mkdir -p "$SCREENSHOT_CONFIG_DIR"
|
||||||
|
|
||||||
[ -e "$SCREENSHOT_CONFIG_DIR/screenshotrc" ] && . "$SCREENSHOT_CONFIG_DIR/screenshotrc" || cat << CONFIG > "$SCREENSHOT_CONFIG_DIR/screenshotrc"
|
[ -e "$SCREENSHOT_CONFIG_DIR/screenshotrc" ] && . "$SCREENSHOT_CONFIG_DIR/screenshotrc" || cat << CONFIG > "$SCREENSHOT_CONFIG_DIR/screenshotrc"
|
||||||
# screenshot-spmenu config file
|
# screenshot-spmenu config file
|
||||||
RUNLAUNCHER="spmenu" # Run launcher to use
|
|
||||||
SHOW_CURSOR="true" # Show cursor or not (true/false)
|
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
|
PREFIX="screenshot-" # Screenshot prefix
|
||||||
FORMAT="+%T_%D" # Screenshot format
|
FORMAT="+%T_%D" # Screenshot format
|
||||||
PREVIEW_IMAGE=true
|
PREVIEW_IMAGE=true
|
||||||
IMAGE_SIZE="500"
|
IMAGE_SIZE="500"
|
||||||
CACHE_IMAGE="false"
|
CACHE_IMAGE="false"
|
||||||
CONFIG
|
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() {
|
print_help() {
|
||||||
cat << EOF
|
cat << EOF
|
||||||
|
@ -95,22 +80,20 @@ No arguments will print this help.
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
rm -f /tmp/screenshot*
|
check_args() {
|
||||||
|
|
||||||
# check arguments
|
# check arguments
|
||||||
case "$1" in
|
case "$1" in
|
||||||
"") print_help; exit 0 ;;
|
"") print_help; exit 0 ;;
|
||||||
"-f") take_full "/tmp/screenshot-$DATE" "0" ;;
|
"-f") screenshot_take "/tmp/$PREFIX$DATE" "false" "$SHOW_CURSOR" "0" ;;
|
||||||
"-s") take_scr "/tmp/screenshot-$DATE" "0" ;;
|
"-s") screenshot_take "/tmp/$PREFIX$DATE" "true" "$SHOW_CURSOR" "0" ;;
|
||||||
*) print_help; exit 0 ;;
|
*) print_help; exit 0 ;;
|
||||||
esac
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
[ "$CACHE_IMAGE" = "true" ] && CACHE_IMAGE="-gc" || CACHE_IMAGE="-ngc"
|
check_action() {
|
||||||
[ "$PREVIEW_IMAGE" = "true" ] && IMGPREFIX="img://" && IMG="/tmp/screenshot-$DATE" && IMGARG="-is $IMAGE_SIZE $CACHE_IMAGE"
|
|
||||||
|
|
||||||
# user action
|
# user action
|
||||||
[ ! -s "/tmp/screenshot-$DATE" ] && exit 0
|
[ ! -s "/tmp/$PREFIX$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:")"
|
[ -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:")"
|
||||||
|
|
||||||
if [ "$x11" = "true" ] && [ -x "$(command -v maim)" ]; then
|
if [ "$x11" = "true" ] && [ -x "$(command -v maim)" ]; then
|
||||||
img_clipboard_cmd="xclip -selection clipboard -t image/png"
|
img_clipboard_cmd="xclip -selection clipboard -t image/png"
|
||||||
|
@ -122,9 +105,37 @@ fi
|
||||||
|
|
||||||
# perform actions based on user input
|
# perform actions based on user input
|
||||||
case "$u_i" in
|
case "$u_i" in
|
||||||
"PNG") $img_clipboard_cmd < "/tmp/screenshot-$DATE" && send_notif "Screenshot copied to clipboard." ;;
|
"PNG") $img_clipboard_cmd < "/tmp/$PREFIX$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." ;;
|
"URL") printf "\n" | $clipboard_cmd && upload_image "/tmp/$PREFIX$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" ;;
|
"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 ;;
|
"") rm -f "/tmp/screenshot*"; exit 0 ;;
|
||||||
*) print_help "$@"; exit 0 ;;
|
*) print_help "$@"; exit 0 ;;
|
||||||
esac
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
rm -f /tmp/screenshot*
|
||||||
|
}
|
||||||
|
|
||||||
|
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 "$@"
|
||||||
|
|
Loading…
Reference in a new issue