diff --git a/README.md b/README.md deleted file mode 120000 index 9adee42..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -screenshot.7.ronn \ No newline at end of file diff --git a/other/_screenshot b/completions/_screenshot similarity index 100% rename from other/_screenshot rename to completions/_screenshot diff --git a/screenshot b/laintools similarity index 83% rename from screenshot rename to laintools index 22aa1e1..e30f53a 100755 --- a/screenshot +++ b/laintools @@ -43,7 +43,7 @@ LAINTOOLS_SS_NAME=${LAINTOOLS_SS_NAME:-${save_date}.png} LAINTOOLS_SS_PATH=${LAINTOOLS_SS_PATH:-${LAINTOOLS_SS_DIR}/${LAINTOOLS_SS_NAME}} LAINTOOLS_SS_OCR_LANGS=${LAINTOOLS_SS_OCR_LANGS:-eng+rus} -LAINTOOLS_DRBTT_STORAGE_PATH=${LAINTOOLS_DRBTT_STORAGE_PATH:-$XDG_DATA_HOME/LAINTOOLS/drbtt_events.duckdb} +LAINTOOLS_DRBTT_STORAGE_PATH=${LAINTOOLS_DRBTT_STORAGE_PATH:-$XDG_DATA_HOME/laintools/drbtt_events.duckdb} # We expect word splitting here # shellcheck disable=2046 @@ -69,11 +69,10 @@ init_storage() { duckdb "${LAINTOOLS_DRBTT_STORAGE_PATH}" " CREATE TABLE IF NOT EXISTS events( timestamp TIMESTAMPTZ PRIMARY KEY, + was_idle BOOLEAN, instance VARCHAR, class VARCHAR, name VARCHAR, - cursor_x USMALLINT, - cursor_y USMALLINT, ); " } @@ -85,56 +84,47 @@ sql_escape() { } snapshot_event() ( - TIMESTAMP="$(date --utc --iso-8601=seconds)" + PREV_TIMESTAMP="${1:-0}" + TIMESTAMP="$(date +%s)" + IDLE_TIME=$(( $(xprintidle) / 1000 )) eval "$(xprop -id "$(get_focused_window_id)" -notype '=$0\n' WM_NAME '=$1\nWM_INSTANCE=$0\n' WM_CLASS)" eval "$(xdotool getmouselocation --shell)" duckdb "${LAINTOOLS_DRBTT_STORAGE_PATH}" " INSERT INTO events VALUES ( - '$(sql_escape "${TIMESTAMP}")', + to_timestamp(${TIMESTAMP}), + ${TIMESTAMP} - ${PREV_TIMESTAMP} < ${IDLE_TIME}, '$(sql_escape "${WM_INSTANCE}")', '$(sql_escape "${WM_CLASS}")', '$(sql_escape "${WM_NAME}")', - $(sql_escape "${X}"), - $(sql_escape "${Y}"), ); " + printf %s "${TIMESTAMP}" ) drbttd() { init_storage while sleep 1; do - snapshot_event || true + TS=$(snapshot_event "${TS:-0}" || true) done } usage() { printf "\ -Usage: screenshot COMMAND +Usage: laintools COMMAND [SUBCOMMAND] DESCRIPTION - Make a screenshot. -COMMANDS: -full + Call a laintool: ScreenShot or DRBTT. +COMMANDS AND SUBCOMMANDS: +ss full Make a full screen screenshot. -select +ss select Select a screenshot area with a mouse. -focused +ss focused Make a screenshot of the currently focused window. +drbtt + Run drbtt daemon " >&2 } -check_user_input() { - if [ $# -ne 1 ] || ! { - [ "$1" = "full" ] || - [ "$1" = "select" ] || - [ "$1" = "focused" ]; - } - then - usage - exit 1 - fi -} - - # https://wiki.archlinux.org/title/Screen_capture#Dedicated_software make_screenshot() { if has_selection_getter && has menyoki; then @@ -230,7 +220,6 @@ make_screenshot() { fi } - copy_to_clipboard() { if has xclip; then printf %s "${LAINTOOLS_SS_PATH}" | xclip -in -selection primary @@ -242,29 +231,9 @@ copy_to_clipboard() { fi } - # https://wiki.archlinux.org/title/List_of_applications/Multimedia#Console optimize_image() { - if [ -n "${LAINTOOLS_SS_DOCKER_HOST:-}" ] && docker info >/dev/null 2>&1; then - ( - export DOCKER_HOST=${LAINTOOLS_SS_DOCKER_HOST} - # Create a container that would auto-destroy in 10 minutes - container_id=$( - docker run --rm -d \ - skhaz/compression-tools \ - sleep $(( 60 * 10 )) - ) - # Destroy container on exit from brackets - trap 'docker rm -f "${container_id}" >/dev/null; trap - EXIT; exit' EXIT INT HUP - # Copy file to container, process it, and copy back - docker cp "${LAINTOOLS_SS_PATH}" "${container_id}:/${LAINTOOLS_SS_NAME}" - docker exec -ti "${container_id}" ect -9 "/${LAINTOOLS_SS_NAME}" - docker container cp "${container_id}:/${LAINTOOLS_SS_NAME}" "${LAINTOOLS_SS_PATH}" - - was_optimized - ) - - elif has oxipng; then + if has oxipng; then oxipng --quiet --opt max --threads 1 -- "${LAINTOOLS_SS_PATH}" was_optimized @@ -299,7 +268,6 @@ optimize_image() { fi } - ocr_image() { if has tesseract; then tesseract \ @@ -314,23 +282,33 @@ ocr_image() { fi } - main() { - check_user_input "$@" - mkdir -p "${LAINTOOLS_SS_DIR}" - make_screenshot "$@" - printf '%s\n' "${LAINTOOLS_SS_PATH}" - copy_to_clipboard - ( - # Execute under lock - # if flock is available - if has flock; then - flock 9 + if [ "${1:-}" = "ss" ]; then + if [ "${2:-}" = "full" ] || [ "${2:-}" = "select" ] || [ "${2:-}" = "focused" ]; then + mkdir -p "${LAINTOOLS_SS_DIR}" + make_screenshot "$@" + printf '%s\n' "${LAINTOOLS_SS_PATH}" + copy_to_clipboard + ( + # Execute under lock + # if flock is available + if has flock; then + flock 9 + fi + optimize_image >&2 + ocr_image >&2 + ) 9>"/var/lock/screenshot-$(id -u).lock" + exit 0 fi - optimize_image >&2 - ocr_image >&2 - ) 9>"/var/lock/screenshot-$(id -u).lock" + + elif [ "${1:-}" = "drbtt" ]; then + drbttd + exit 0 + + fi + + usage + exit 1 } -# main "$@" -drbttd +main "$@" diff --git a/screenshot.7.ronn b/man/laintools-ss.7.ronn similarity index 100% rename from screenshot.7.ronn rename to man/laintools-ss.7.ronn diff --git a/pick-and-do b/pick-and-do new file mode 100755 index 0000000..bfbe9ea --- /dev/null +++ b/pick-and-do @@ -0,0 +1,54 @@ +#!/bin/sh + +# DESCRIPTION: +# * make and save a screenshot +# * [optionally] copy to clipboard +# * [optionally] optimize it +# * [optionally] OCR it +# +# ARGUMENTS: +# * mode: full|selected|focused +# +# ENVIRONMENT VARIABLES: +# * SCREENSHOT_DOCKER_HOST: set to prefer using docker for image optimization +# Examples: `unix:///var/run/docker.sock` for local execution +# `ssh://root@your.server.com` for remote +# +# DEPENDENCIES: +# * Interpreter: POSIX shell + coreutils +# * Screenshot tool: maim + xdotool | scrot | graphicsmagick + xdotool | imagemagick + xdotool +# * Clipboard tool [opt]: xclip | xsel +# * Image lossless optimizer [opt]: optipng | pngcrush | jpegoptim +# * OCR tool [opt]: tesseract + tesseract-ocr-rus +# * Locks manager [opt]: flock +# + + +set -o errexit # exit on fail +set -o nounset # exit on undeclared variable +# set -o xtrace # trace execution + + +selected_option=$( + printf '%s\n' "QR Code" "B" | + rofi -dmenu +) + +case ${selected_option} in + "QR Code") + # Get file path from primary X buffer (middle mouse click to paste) + # and decode the QR code + zbarimg --quiet -- "$(xclip -selection primary -out)" | + # Drop the `QR-Code:` prefix. Also drop the newline + sed 's/^QR\-Code:\(.*\)$/\1/' |tr -d '\n' | + # Paste the result to clipboard (Ctrl+V to paste) + xclip -in -selection clipboard -filter + ;; + "B") + echo "You chose B!" + ;; + *) + echo "Invalid selection" + exit 1 + ;; +esac