Skip to content

Commit a8c633f

Browse files
committed
Update
1 parent 788f71f commit a8c633f

File tree

5 files changed

+98
-67
lines changed

5 files changed

+98
-67
lines changed

README.md

-1
This file was deleted.
File renamed without changes.

screenshot laintools

+44-66
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ LAINTOOLS_SS_NAME=${LAINTOOLS_SS_NAME:-${save_date}.png}
4343
LAINTOOLS_SS_PATH=${LAINTOOLS_SS_PATH:-${LAINTOOLS_SS_DIR}/${LAINTOOLS_SS_NAME}}
4444
LAINTOOLS_SS_OCR_LANGS=${LAINTOOLS_SS_OCR_LANGS:-eng+rus}
4545

46-
LAINTOOLS_DRBTT_STORAGE_PATH=${LAINTOOLS_DRBTT_STORAGE_PATH:-$XDG_DATA_HOME/LAINTOOLS/drbtt_events.duckdb}
46+
LAINTOOLS_DRBTT_STORAGE_PATH=${LAINTOOLS_DRBTT_STORAGE_PATH:-$XDG_DATA_HOME/laintools/drbtt_events.duckdb}
4747

4848
# We expect word splitting here
4949
# shellcheck disable=2046
@@ -69,11 +69,10 @@ init_storage() {
6969
duckdb "${LAINTOOLS_DRBTT_STORAGE_PATH}" "
7070
CREATE TABLE IF NOT EXISTS events(
7171
timestamp TIMESTAMPTZ PRIMARY KEY,
72+
was_idle BOOLEAN,
7273
instance VARCHAR,
7374
class VARCHAR,
7475
name VARCHAR,
75-
cursor_x USMALLINT,
76-
cursor_y USMALLINT,
7776
);
7877
"
7978
}
@@ -85,56 +84,47 @@ sql_escape() {
8584
}
8685

8786
snapshot_event() (
88-
TIMESTAMP="$(date --utc --iso-8601=seconds)"
87+
PREV_TIMESTAMP="${1:-0}"
88+
TIMESTAMP="$(date +%s)"
89+
IDLE_TIME=$(( $(xprintidle) / 1000 ))
8990
eval "$(xprop -id "$(get_focused_window_id)" -notype '=$0\n' WM_NAME '=$1\nWM_INSTANCE=$0\n' WM_CLASS)"
9091
eval "$(xdotool getmouselocation --shell)"
9192
duckdb "${LAINTOOLS_DRBTT_STORAGE_PATH}" "
9293
INSERT INTO events VALUES (
93-
'$(sql_escape "${TIMESTAMP}")',
94+
to_timestamp(${TIMESTAMP}),
95+
${TIMESTAMP} - ${PREV_TIMESTAMP} < ${IDLE_TIME},
9496
'$(sql_escape "${WM_INSTANCE}")',
9597
'$(sql_escape "${WM_CLASS}")',
9698
'$(sql_escape "${WM_NAME}")',
97-
$(sql_escape "${X}"),
98-
$(sql_escape "${Y}"),
9999
);
100100
"
101+
printf %s "${TIMESTAMP}"
101102
)
102103

103104
drbttd() {
104105
init_storage
105106
while sleep 1; do
106-
snapshot_event || true
107+
TS=$(snapshot_event "${TS:-0}" || true)
107108
done
108109
}
109110

110111
usage() {
111112
printf "\
112-
Usage: screenshot COMMAND
113+
Usage: laintools COMMAND [SUBCOMMAND]
113114
DESCRIPTION
114-
Make a screenshot.
115-
COMMANDS:
116-
full
115+
Call a laintool: ScreenShot or DRBTT.
116+
COMMANDS AND SUBCOMMANDS:
117+
ss full
117118
Make a full screen screenshot.
118-
select
119+
ss select
119120
Select a screenshot area with a mouse.
120-
focused
121+
ss focused
121122
Make a screenshot of the currently focused window.
123+
drbtt
124+
Run drbtt daemon
122125
" >&2
123126
}
124127

125-
check_user_input() {
126-
if [ $# -ne 1 ] || ! {
127-
[ "$1" = "full" ] ||
128-
[ "$1" = "select" ] ||
129-
[ "$1" = "focused" ];
130-
}
131-
then
132-
usage
133-
exit 1
134-
fi
135-
}
136-
137-
138128
# https://wiki.archlinux.org/title/Screen_capture#Dedicated_software
139129
make_screenshot() {
140130
if has_selection_getter && has menyoki; then
@@ -230,7 +220,6 @@ make_screenshot() {
230220
fi
231221
}
232222

233-
234223
copy_to_clipboard() {
235224
if has xclip; then
236225
printf %s "${LAINTOOLS_SS_PATH}" | xclip -in -selection primary
@@ -242,29 +231,9 @@ copy_to_clipboard() {
242231
fi
243232
}
244233

245-
246234
# https://wiki.archlinux.org/title/List_of_applications/Multimedia#Console
247235
optimize_image() {
248-
if [ -n "${LAINTOOLS_SS_DOCKER_HOST:-}" ] && docker info >/dev/null 2>&1; then
249-
(
250-
export DOCKER_HOST=${LAINTOOLS_SS_DOCKER_HOST}
251-
# Create a container that would auto-destroy in 10 minutes
252-
container_id=$(
253-
docker run --rm -d \
254-
skhaz/compression-tools \
255-
sleep $(( 60 * 10 ))
256-
)
257-
# Destroy container on exit from brackets
258-
trap 'docker rm -f "${container_id}" >/dev/null; trap - EXIT; exit' EXIT INT HUP
259-
# Copy file to container, process it, and copy back
260-
docker cp "${LAINTOOLS_SS_PATH}" "${container_id}:/${LAINTOOLS_SS_NAME}"
261-
docker exec -ti "${container_id}" ect -9 "/${LAINTOOLS_SS_NAME}"
262-
docker container cp "${container_id}:/${LAINTOOLS_SS_NAME}" "${LAINTOOLS_SS_PATH}"
263-
264-
was_optimized
265-
)
266-
267-
elif has oxipng; then
236+
if has oxipng; then
268237
oxipng --quiet --opt max --threads 1 -- "${LAINTOOLS_SS_PATH}"
269238
was_optimized
270239

@@ -299,7 +268,6 @@ optimize_image() {
299268
fi
300269
}
301270

302-
303271
ocr_image() {
304272
if has tesseract; then
305273
tesseract \
@@ -314,23 +282,33 @@ ocr_image() {
314282
fi
315283
}
316284

317-
318285
main() {
319-
check_user_input "$@"
320-
mkdir -p "${LAINTOOLS_SS_DIR}"
321-
make_screenshot "$@"
322-
printf '%s\n' "${LAINTOOLS_SS_PATH}"
323-
copy_to_clipboard
324-
(
325-
# Execute under lock
326-
# if flock is available
327-
if has flock; then
328-
flock 9
286+
if [ "${1:-}" = "ss" ]; then
287+
if [ "${2:-}" = "full" ] || [ "${2:-}" = "select" ] || [ "${2:-}" = "focused" ]; then
288+
mkdir -p "${LAINTOOLS_SS_DIR}"
289+
make_screenshot "$@"
290+
printf '%s\n' "${LAINTOOLS_SS_PATH}"
291+
copy_to_clipboard
292+
(
293+
# Execute under lock
294+
# if flock is available
295+
if has flock; then
296+
flock 9
297+
fi
298+
optimize_image >&2
299+
ocr_image >&2
300+
) 9>"/var/lock/screenshot-$(id -u).lock"
301+
exit 0
329302
fi
330-
optimize_image >&2
331-
ocr_image >&2
332-
) 9>"/var/lock/screenshot-$(id -u).lock"
303+
304+
elif [ "${1:-}" = "drbtt" ]; then
305+
drbttd
306+
exit 0
307+
308+
fi
309+
310+
usage
311+
exit 1
333312
}
334313

335-
# main "$@"
336-
drbttd
314+
main "$@"
File renamed without changes.

pick-and-do

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/sh
2+
3+
# DESCRIPTION:
4+
# * make and save a screenshot
5+
# * [optionally] copy to clipboard
6+
# * [optionally] optimize it
7+
# * [optionally] OCR it
8+
#
9+
# ARGUMENTS:
10+
# * mode: full|selected|focused
11+
#
12+
# ENVIRONMENT VARIABLES:
13+
# * SCREENSHOT_DOCKER_HOST: set to prefer using docker for image optimization
14+
# Examples: `unix:///var/run/docker.sock` for local execution
15+
# `ssh://[email protected]` for remote
16+
#
17+
# DEPENDENCIES:
18+
# * Interpreter: POSIX shell + coreutils
19+
# * Screenshot tool: maim + xdotool | scrot | graphicsmagick + xdotool | imagemagick + xdotool
20+
# * Clipboard tool [opt]: xclip | xsel
21+
# * Image lossless optimizer [opt]: optipng | pngcrush | jpegoptim
22+
# * OCR tool [opt]: tesseract + tesseract-ocr-rus
23+
# * Locks manager [opt]: flock
24+
#
25+
26+
27+
set -o errexit # exit on fail
28+
set -o nounset # exit on undeclared variable
29+
# set -o xtrace # trace execution
30+
31+
32+
selected_option=$(
33+
printf '%s\n' "QR Code" "B" |
34+
rofi -dmenu
35+
)
36+
37+
case ${selected_option} in
38+
"QR Code")
39+
# Get file path from primary X buffer (middle mouse click to paste)
40+
# and decode the QR code
41+
zbarimg --quiet -- "$(xclip -selection primary -out)" |
42+
# Drop the `QR-Code:` prefix. Also drop the newline
43+
sed 's/^QR\-Code:\(.*\)$/\1/' |tr -d '\n' |
44+
# Paste the result to clipboard (Ctrl+V to paste)
45+
xclip -in -selection clipboard -filter
46+
;;
47+
"B")
48+
echo "You chose B!"
49+
;;
50+
*)
51+
echo "Invalid selection"
52+
exit 1
53+
;;
54+
esac

0 commit comments

Comments
 (0)