dotfiles/home/user/.local/bin/screenshot

132 lines
3.2 KiB
Bash
Executable File

#!/bin/sh
set -eu
readonly LOG_FILE="${HOME}/.screenshots.log"
readonly NOTIFY_TIME_MS=3000
readonly NOTIFY_TIME_ERROR_MS=5000
readonly NOTIFY_LABEL="Screenshot"
readonly SCREENSHOTS_DIR="${SCREENSHOTS_DIR:-${HOME}/Pictures/screenshots}"
_notify() {
notify-send --urgency normal --expire-time "${NOTIFY_TIME_MS}" "${NOTIFY_LABEL}" "${1}"
}
_notify_error() {
notify-send --urgency critical --expire-time "${NOTIFY_TIME_ERROR_MS}" "${NOTIFY_LABEL}" "Error: ${1}"
}
_make_screen_select() {
#shellcheck disable=SC2317
maim --select
}
_make_screen_entire() {
#shellcheck disable=SC2317
maim
}
_make_screen_current_window() {
#shellcheck disable=SC2317
maim --window "$(xdotool getactivewindow)"
}
_copy_image_from_stdin_to_clipboard() {
#shellcheck disable=SC2317
xclip -selection clipboard -t image/png
}
_save_image_from_stdin_to_file() {
#shellcheck disable=SC2317
cat > "${1}"
}
select_save() {
screenshot_file="${SCREENSHOTS_DIR}/$(date +'%d-%m-%Y-%T').png"
msg="Selection screenshot save to ${screenshot_file}"
if _make_screen_select | _save_image_from_stdin_to_file "${screenshot_file}"; then
_notify "${msg}"
else
_notify_error "${msg}"
fi
}
select_copy() {
msg="Selection screenshot copy to clipboard"
if _make_screen_select | _copy_image_from_stdin_to_clipboard; then
_notify "${msg}"
else
_notify_error "${msg}"
fi
}
window_save() {
screenshot_file="${SCREENSHOTS_DIR}/$(date +'%d-%m-%Y-%T').png"
msg="Current window screenshot save to ${screenshot_file}"
if _make_screen_current_window | _save_image_from_stdin_to_file "${screenshot_file}"; then
_notify "${msg}"
else
_notify_error "${msg}"
fi
}
window_copy() {
msg="Current window screenshot save to clipboard"
if _make_screen_current_window | _copy_image_from_stdin_to_clipboard; then
_notify "${msg}"
else
_notify_error "${msg}"
fi
}
entire_save() {
screenshot_file="${SCREENSHOTS_DIR}/$(date +'%d-%m-%Y-%T').png"
msg="Entire display screenshot save to ${screenshot_file}"
if _make_screen_entire | _save_image_from_stdin_to_file "${screenshot_file}"; then
_notify "${msg}"
else
_notify_error "${msg}"
fi
}
entire_copy() {
msg="Entire display screenshot save to clipboard"
if _make_screen_entire | _copy_image_from_stdin_to_clipboard; then
_notify "${msg}"
else
_notify_error "${msg}"
fi
}
main() {
if [ $# -ne 2 ]; then
echo "Usage: ${0} {select|window|entire} {save|copy}"
exit 1
fi
readonly target="${1}"
readonly action="${2}"
case "${target}-${action}" in
select-save) select_save &>> "${LOG_FILE}" ;;
select-copy) select_copy &>> "${LOG_FILE}" ;;
window-save) window_save &>> "${LOG_FILE}" ;;
window-copy) window_copy &>> "${LOG_FILE}" ;;
entire-save) entire_save &>> "${LOG_FILE}" ;;
entire-copy) entire_copy &>> "${LOG_FILE}" ;;
*)
echo "Invalid options: target=${target}, action=${action}"
echo "Available targets: select, window, entire"
echo "Available actions: save, copy"
exit 1
;;
esac
}
main "$@"