i3 screenshots script refactor

This commit is contained in:
thek4n 2025-05-19 16:28:35 +03:00
parent a10f949729
commit 03848abdb3

View File

@ -1,107 +1,123 @@
#!/bin/sh #!/bin/sh
set -eu set -eu
readonly LOG_FILE="${HOME}/.screenshots.log" readonly LOG_FILE="${HOME}/.screenshots.log"
readonly NOTIFY_TIME_MS=3000 readonly NOTIFY_TIME_MS=3000
readonly NOTIFY_TIME_ERROR_MS=5000 readonly NOTIFY_TIME_ERROR_MS=5000
readonly NOTIFY_LABEL="Screenshot" readonly NOTIFY_LABEL="Screenshot"
readonly SCREENSHOTS_DIR="${SCREENSHOTS_DIR:-${HOME}/Pictures/screenshots}" readonly SCREENSHOTS_DIR="${SCREENSHOTS_DIR:-${HOME}/Pictures/screenshots}"
readonly TIMESTAMP_FORMAT="%H-%M-%S_%Y-%m-%d"
mkdir -p "${SCREENSHOTS_DIR}"
_notify() { _notify() {
notify-send --urgency normal --expire-time "${NOTIFY_TIME_MS}" "${NOTIFY_LABEL}" "${1}" notify-send --urgency normal \
--expire-time "${NOTIFY_TIME_MS}" \
"${NOTIFY_LABEL}" \
"${1}"
} }
_notify_error() { _notify_error() {
notify-send --urgency critical --expire-time "${NOTIFY_TIME_ERROR_MS}" "${NOTIFY_LABEL}" "Error: ${1}" notify-send --urgency critical \
--expire-time "${NOTIFY_TIME_ERROR_MS}" \
"${NOTIFY_LABEL}" \
"Error: ${1}"
} }
_make_screen_select() { _capture_select() {
#shellcheck disable=SC2317
maim --select maim --select
} }
_make_screen_entire() { _capture_entire() {
#shellcheck disable=SC2317
maim maim
} }
_make_screen_current_window() { _capture_window() {
#shellcheck disable=SC2317
maim --window "$(xdotool getactivewindow)" maim --window "$(xdotool getactivewindow)"
} }
_copy_image_from_stdin_to_clipboard() {
#shellcheck disable=SC2317 _copy_to_clipboard() {
xclip -selection clipboard -t image/png xclip -selection clipboard -t image/png
} }
_save_image_from_stdin_to_file() { _save_to_file() {
#shellcheck disable=SC2317
cat > "${1}" cat > "${1}"
} }
_process_screenshot() {
capture_func="${1}"
output_func="${2}"
success_msg="${3}"
error_msg="${4}"
output_arg="${5:-}"
select_save() { if [ -n "${output_arg}" ]; then
screenshot_file="${SCREENSHOTS_DIR}/$(date +'%d-%m-%Y-%T').png" if ! ${capture_func} | ${output_func} "${output_arg}"; then
msg="Selection screenshot save to ${screenshot_file}" _notify_error "${error_msg}"
if _make_screen_select | _save_image_from_stdin_to_file "${screenshot_file}"; then return 1
_notify "${msg}"
else
_notify_error "${msg}"
fi fi
else
if ! ${capture_func} | ${output_func}; then
_notify_error "${error_msg}"
return 1
fi
fi
_notify "${success_msg}"
return 0
} }
select_copy() { _select_save() {
msg="Selection screenshot copy to clipboard" filename="${SCREENSHOTS_DIR}/selection_$(date +"${TIMESTAMP_FORMAT}").png"
if _make_screen_select | _copy_image_from_stdin_to_clipboard; then _process_screenshot _capture_select \
_notify "${msg}" _save_to_file \
else "Selection saved to ${filename}" \
_notify_error "${msg}" "Failed to save selection screenshot" \
fi "${filename}"
} }
window_save() { _select_copy() {
screenshot_file="${SCREENSHOTS_DIR}/$(date +'%d-%m-%Y-%T').png" _process_screenshot _capture_select \
msg="Current window screenshot save to ${screenshot_file}" _copy_to_clipboard \
if _make_screen_current_window | _save_image_from_stdin_to_file "${screenshot_file}"; then "Selection copied to clipboard" \
_notify "${msg}" "Failed to copy selection to clipboard"
else
_notify_error "${msg}"
fi
} }
window_copy() { _window_save() {
msg="Current window screenshot save to clipboard" filename="${SCREENSHOTS_DIR}/window_$(date +"${TIMESTAMP_FORMAT}").png"
if _make_screen_current_window | _copy_image_from_stdin_to_clipboard; then _process_screenshot _capture_window \
_notify "${msg}" _save_to_file \
else "Current window saved to ${filename}" \
_notify_error "${msg}" "Failed to save Current window screenshot" \
fi "${filename}"
} }
entire_save() { _window_copy() {
screenshot_file="${SCREENSHOTS_DIR}/$(date +'%d-%m-%Y-%T').png" _process_screenshot _capture_window \
msg="Entire display screenshot save to ${screenshot_file}" _copy_to_clipboard \
if _make_screen_entire | _save_image_from_stdin_to_file "${screenshot_file}"; then "Current window copied to clipboard" \
_notify "${msg}" "Failed to copy current window to clipboard"
else
_notify_error "${msg}"
fi
} }
entire_copy() { _entire_save() {
msg="Entire display screenshot save to clipboard" filename="${SCREENSHOTS_DIR}/screen_$(date +"${TIMESTAMP_FORMAT}").png"
if _make_screen_entire | _copy_image_from_stdin_to_clipboard; then _process_screenshot _capture_entire \
_notify "${msg}" _save_to_file \
else "Entire screen saved to ${filename}" \
_notify_error "${msg}" "Failed to save entire screen" \
fi "${filename}"
} }
_entire_copy() {
_process_screenshot _capture_entire \
_copy_to_clipboard \
"Entire screen copied to clipboard" \
"Failed to copy entire screen to clipboard"
}
main() { main() {
if [ $# -ne 2 ]; then if [ $# -ne 2 ]; then
@ -113,12 +129,12 @@ main() {
readonly action="${2}" readonly action="${2}"
case "${target}-${action}" in case "${target}-${action}" in
select-save) select_save &>> "${LOG_FILE}" ;; select-save) _select_save &>> "${LOG_FILE}" ;;
select-copy) select_copy &>> "${LOG_FILE}" ;; select-copy) _select_copy &>> "${LOG_FILE}" ;;
window-save) window_save &>> "${LOG_FILE}" ;; window-save) _window_save &>> "${LOG_FILE}" ;;
window-copy) window_copy &>> "${LOG_FILE}" ;; window-copy) _window_copy &>> "${LOG_FILE}" ;;
entire-save) entire_save &>> "${LOG_FILE}" ;; entire-save) _entire_save &>> "${LOG_FILE}" ;;
entire-copy) entire_copy &>> "${LOG_FILE}" ;; entire-copy) _entire_copy &>> "${LOG_FILE}" ;;
*) *)
echo "Invalid options: target=${target}, action=${action}" echo "Invalid options: target=${target}, action=${action}"
echo "Available targets: select, window, entire" echo "Available targets: select, window, entire"