-
Notifications
You must be signed in to change notification settings - Fork 3
/
screenshot-area-scan-qr.nix
59 lines (57 loc) · 1.58 KB
/
screenshot-area-scan-qr.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{
writeShellApplication,
grimblast,
libnotify,
tesseract,
wl-clipboard,
yq,
zbar,
}:
writeShellApplication {
name = "screenshot-area-scan-qr";
runtimeInputs = [
grimblast
libnotify
tesseract
wl-clipboard
yq
zbar
];
text = ''
umask 077
# Create in-memory tmpfile
TMPFILE=$(mktemp)
exec 3<>"$TMPFILE"
rm "$TMPFILE" # still open in-memory as /dev/fd/3
TMPFILE=/dev/fd/3
if grimblast --freeze save area - \
| zbarimg --xml - > "$TMPFILE"; then
N=$(xq -r '.barcodes.source.index.symbol | if type == "array" then length else 1 end' < "$TMPFILE")
# Append codes Copy data separated by ---
DATA=$(xq -r '.barcodes.source.index.symbol | if type == "array" then .[0].data else .data end' < "$TMPFILE")
for ((i=1;i<N;++i)); do
DATA="$DATA"$'\n'"---"$'\n'"$(xq -r ".barcodes.source.index.symbol[$i].data" < "$TMPFILE")"
done
wl-copy <<< "$DATA"
notify-send \
"🔍 QR Code scan" "✅ $N codes detected\n📋 copied ''${#DATA} bytes" \
--hint="string:image-path:"${./assets}/qr-scan.png \
|| true
else
case "$?" in
"4")
notify-send \
"🔍 QR Code scan" "❌ 0 codes detected" \
--hint="string:image-path:"${./assets}/qr-scan.png \
|| true
;;
*)
notify-send \
"🔍 QR Code scan" "❌ Error while processing image: zbarimg exited with code $?" \
--hint="string:image-path:"${./assets}/qr-scan.png \
|| true
;;
esac
fi
'';
}