dotfiles/bin/glurp
2023-12-01 22:28:43 +01:00

83 lines
1.2 KiB
Bash
Executable file

#!/bin/bash
declare -A temp_files
temp_files[command_to_source]=$(mktemp)
clean() {
rm -- "${temp_files[@]}"
}
usage() {
cat << EOF
Usage:
glurp full|area clip|file
EOF
}
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
}
screenshot_path() {
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)"
}
trap error_usage_clean EXIT
set -e
slurp=()
grim_args=()
wl_copy=()
case "$1" in
full)
:
;;
area)
slurp+=(slurp \|)
grim_args+=(-g -)
;;
*)
false
;;
esac
shift
case "$1" in
clip)
wl_copy+=(\| wl-copy -t image/png)
grim_args+=(-)
;;
file)
grim_args+=("$(screenshot_path "${XDG_RUNTIME_DIR:-/tmp/$(whoami)}/screenshots")")
;;
*)
false
;;
esac
shift
cat > "${temp_files[command_to_source]}" << EOF
${slurp[@]} grim ${grim_args[@]} ${@} ${wl_copy[@]}
EOF
trap clean EXIT
sh -s < "${temp_files[command_to_source]}"