Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
lainiwa committed May 24, 2024
1 parent 788f71f commit a8c633f
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 67 deletions.
1 change: 0 additions & 1 deletion README.md

This file was deleted.

File renamed without changes.
110 changes: 44 additions & 66 deletions screenshot → laintools
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
);
"
}
Expand All @@ -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
Expand Down Expand Up @@ -230,7 +220,6 @@ make_screenshot() {
fi
}


copy_to_clipboard() {
if has xclip; then
printf %s "${LAINTOOLS_SS_PATH}" | xclip -in -selection primary
Expand All @@ -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

Expand Down Expand Up @@ -299,7 +268,6 @@ optimize_image() {
fi
}


ocr_image() {
if has tesseract; then
tesseract \
Expand All @@ -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 "$@"
File renamed without changes.
54 changes: 54 additions & 0 deletions pick-and-do
Original file line number Diff line number Diff line change
@@ -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://[email protected]` 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

0 comments on commit a8c633f

Please sign in to comment.