dotfiles/bin/glurp

84 lines
1.2 KiB
Text
Raw Normal View History

2023-09-29 21:47:30 +02:00
#!/bin/bash
2023-05-14 22:55:33 +02:00
2023-09-29 21:47:30 +02:00
declare -A temp_files
temp_files[command_to_source]=$(mktemp)
clean() {
rm -- "${temp_files[@]}"
}
usage() {
cat << EOF
Usage:
2023-05-14 22:55:33 +02:00
glurp full|area clip|file
EOF
2023-09-29 21:47:30 +02:00
}
error() {
>&2 printf '\033[0m\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
}
error_usage() {
error "Invalid usage"
>&2 usage
}
error_usage_clean() {
error_usage
clean
2023-04-13 00:04:20 +02:00
}
2023-05-14 22:55:33 +02:00
screenshot_path() {
2023-09-29 21:47:30 +02:00
local path
if [ -n "$1" ]; then
path="$1"
else
path=.
fi
mkdir -p "$path"
printf '%sscreenshot-%s.png' "${path}/" "$(date +%Y_%m_%d_%H_%M_%S_%N)"
2023-04-13 00:04:20 +02:00
}
2023-09-29 21:47:30 +02:00
trap error_usage_clean EXIT
set -e
slurp=()
grim_args=()
wl_copy=()
2023-04-13 00:04:20 +02:00
2023-05-14 22:55:33 +02:00
case "$1" in
2023-04-13 00:04:20 +02:00
full)
2023-09-29 21:47:30 +02:00
:
2023-04-13 00:04:20 +02:00
;;
area)
2023-09-29 21:47:30 +02:00
slurp+=(slurp \|)
grim_args+=(-g -)
2023-04-13 00:04:20 +02:00
;;
*)
2023-09-29 21:47:30 +02:00
false
2023-04-13 00:04:20 +02:00
;;
esac
2023-05-14 22:55:33 +02:00
shift
case "$1" in
2023-04-13 00:04:20 +02:00
clip)
2023-09-29 21:47:30 +02:00
wl_copy+=(\| wl-copy -t image/png)
grim_args+=(-)
2023-04-13 00:04:20 +02:00
;;
file)
grim_args+=("$(screenshot_path "${XDG_RUN_TIME_DIR:-/tmp/$(whoami)}/screenshots")")
2023-04-13 00:04:20 +02:00
;;
*)
2023-09-29 21:47:30 +02:00
false
2023-04-13 00:04:20 +02:00
;;
esac
2023-05-14 22:55:33 +02:00
shift
2023-04-13 00:04:20 +02:00
2023-09-29 21:47:30 +02:00
cat > "${temp_files[command_to_source]}" << EOF
${slurp[@]} grim ${grim_args[@]} ${@} ${wl_copy[@]}
EOF
trap clean EXIT
2023-04-13 00:04:20 +02:00
2023-09-29 21:47:30 +02:00
sh -s < "${temp_files[command_to_source]}"