dotfiles/bin/glurp
2023-09-29 21:47:30 +02:00

84 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 "/tmp/${USER}")")
;;
*)
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]}"