Mise à jour : réécriture de glurp

This commit is contained in:
Hippolyte Chauvin 2023-09-29 21:47:30 +02:00
parent 9d3897b579
commit 69d52c7ffc
2 changed files with 53 additions and 24 deletions

View file

@ -1,52 +1,84 @@
#!/bin/sh
#!/bin/bash
error_usage() {
>&2 cat << EOF
declare -A temp_files
temp_files[command_to_source]=$(mktemp)
clean() {
rm -- "${temp_files[@]}"
}
usage() {
cat << EOF
Usage:
glurp full|area clip|file
EOF
exit 1
}
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() {
printf %s "/tmp/screenshot-${USER}-$(date +%N).png"
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)"
}
grim=grim
wl_copy_args="-t image/png"
trap error_usage_clean EXIT
set -e
slurp=()
grim_args=()
wl_copy=()
# Arguments
case "$1" in
full)
:
;;
area)
slurp=slurp
pipe1='|'
grim_args="-g -"
slurp+=(slurp \|)
grim_args+=(-g -)
;;
*)
error_usage
false
;;
esac
shift
case "$1" in
clip)
pipe2='|'
wl_copy=wl-copy
wl_copy+=(\| wl-copy -t image/png)
grim_args+=(-)
;;
file)
grim_args="${grim_args} $(screenshot_path)"
grim_args+=("$(screenshot_path "/tmp/${USER}")")
;;
*)
error_usage
false
;;
esac
shift
[ -n "$wl_copy" ] && wl_copy="${wl_copy} ${wl_copy_args}"
cat > "${temp_files[command_to_source]}" << EOF
${slurp[@]} grim ${grim_args[@]} ${@} ${wl_copy[@]}
EOF
# Execution
set -xe
"$slurp" "$pipe1" "$grim" "$grim_args" "$pipe2" "$wl_copy"
trap clean EXIT
sh -s < "${temp_files[command_to_source]}"

View file

@ -1,3 +0,0 @@
#!/bin/sh
exec printf "$@" "screenshot-%s.${1:-png}" "$(date +"%Y_%m_%d-%N")"