From 69d52c7ffc1b2c89acd9c6f576239e339ce74472 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Fri, 29 Sep 2023 21:47:30 +0200 Subject: [PATCH] =?UTF-8?q?Mise=20=C3=A0=20jour=20:=20r=C3=A9=C3=A9criture?= =?UTF-8?q?=20de=20glurp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/glurp | 74 ++++++++++++++++++++++++++++++++++++-------------- bin/sshot-file | 3 -- 2 files changed, 53 insertions(+), 24 deletions(-) delete mode 100755 bin/sshot-file diff --git a/bin/glurp b/bin/glurp index 4cc648f..bc584ba 100755 --- a/bin/glurp +++ b/bin/glurp @@ -1,52 +1,84 @@ -#!/bin/sh +#!/bin/bash -error_usage() { - >&2 cat << EOF -Usage : +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]}" diff --git a/bin/sshot-file b/bin/sshot-file deleted file mode 100755 index dbf517b..0000000 --- a/bin/sshot-file +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -exec printf "$@" "screenshot-%s.${1:-png}" "$(date +"%Y_%m_%d-%N")" -