dotfiles/bin/glurp

82 lines
1.2 KiB
Text
Raw Normal View History

2024-04-04 12:27:55 +02:00
#!/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' "$*"
2023-09-29 21:47:30 +02:00
}
usage() {
2024-04-04 12:27:55 +02:00
name=$(basename "$0")
2023-09-29 21:47:30 +02:00
2024-04-04 12:27:55 +02:00
printf %s "${name}: usage
${name} f[ull]|a[rea] c[lip]|f[ile]
${name} -h
"
2023-09-29 21:47:30 +02:00
}
2024-04-04 12:27:55 +02:00
# Function to throw an error usage
2023-09-29 21:47:30 +02:00
error_usage() {
2024-04-04 12:27:55 +02:00
perror "invalid usage"
>&2 usage
2023-09-29 21:47:30 +02:00
2024-04-04 12:27:55 +02:00
exit 1
2023-04-13 00:04:20 +02:00
}
2023-05-14 22:55:33 +02:00
screenshot_path() {
2024-04-04 12:27:55 +02:00
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"
2023-04-13 00:04:20 +02:00
}
2024-04-04 12:27:55 +02:00
while getopts :h opt; do
case "$opt" in
h)
usage
exit 0
;;
?)
error_usage
;;
esac
done
2023-09-29 21:47:30 +02:00
2024-04-04 12:27:55 +02:00
# Initial variables
slurp=
grim="grim -t png"
clipboard=
2023-04-13 00:04:20 +02:00
2024-04-04 12:27:55 +02:00
# f* = full, a* = area
2023-05-14 22:55:33 +02:00
case "$1" in
2024-04-04 12:27:55 +02:00
f*) : ;;
a*)
slurp="slurp |"
grim="${grim} -g -"
;;
*) error_usage ;;
2023-04-13 00:04:20 +02:00
esac
2023-05-14 22:55:33 +02:00
2024-04-04 12:27:55 +02:00
# c* = clip, f* = file
case "$2" in
c*)
2024-04-05 19:00:27 +02:00
grim="${grim} -"
2024-04-04 12:27:55 +02:00
clipboard="| wl-copy -t image/png"
;;
f*)
2024-04-05 19:00:27 +02:00
grim="${grim} '$(screenshot_path)'"
2024-04-04 12:27:55 +02:00
;;
*) error_usage ;;
2023-04-13 00:04:20 +02:00
esac
2024-04-04 12:27:55 +02:00
set -- sh -c "${slurp} ${grim} ${clipboard}"
set -x
#: "$@"
#exit
exec "$@"