dotfiles/bin/glurp
2024-04-04 12:27:55 +02:00

81 lines
1.2 KiB
Bash
Executable file

#!/usr/bin/env sh
# Function to print an error in stderr
perror() {
>&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
}
usage() {
name=$(basename "$0")
printf %s "${name}: usage
${name} f[ull]|a[rea] c[lip]|f[ile]
${name} -h
"
}
# Function to throw an error usage
error_usage() {
perror "invalid usage"
>&2 usage
exit 1
}
screenshot_path() {
if [ -d "$XDG_RUNTIME_DIR" ]; then
base=${XDG_RUNTIME_DIR}/screenshots
else
base=/tmp/${USER}-screenshots
fi
# I literally know, shellcheck
# shellcheck disable=2174
mkdir -p -m 700 -- "$base"
printf %s "${base}/screenshot-$(date +%Y%m%d_%H%M%S%N).png"
}
while getopts :h opt; do
case "$opt" in
h)
usage
exit 0
;;
?)
error_usage
;;
esac
done
# Initial variables
slurp=
grim="grim -t png"
clipboard=
# f* = full, a* = area
case "$1" in
f*) : ;;
a*)
slurp="slurp |"
grim="${grim} -g -"
;;
*) error_usage ;;
esac
# c* = clip, f* = file
case "$2" in
c*)
grim="${grim} -o -"
clipboard="| wl-copy -t image/png"
;;
f*)
grim="${grim} -o '$(screenshot_path)'"
;;
*) error_usage ;;
esac
set -- sh -c "${slurp} ${grim} ${clipboard}"
set -x
#: "$@"
#exit
exec "$@"