refactor(screenshots): use separate scripts package

This commit is contained in:
oddlama 2023-10-16 00:46:30 +02:00
parent 3e0e03fc31
commit 6e8aae7d8d
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A
15 changed files with 216 additions and 109 deletions

7
pkgs/scripts/default.nix Normal file
View file

@ -0,0 +1,7 @@
_final: prev: {
scripts = {
screenshot-area = prev.callPackage ./screenshot-area.nix {};
screenshot-area-scan-qr = prev.callPackage ./screenshot-area-scan-qr.nix {};
screenshot-screen = prev.callPackage ./screenshot-screen.nix {};
};
}

View file

@ -0,0 +1,58 @@
{
writeShellApplication,
libnotify,
xclip,
maim,
zbar,
yq,
}:
writeShellApplication {
name = "screenshot-area-scan-qr";
text = ''
set -euo pipefail
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
date=$(date +"%Y-%m-%dT%H:%M:%S%:z")
out="''${XDG_PICTURES_DIR-$HOME/Pictures}/screenshots/$date-selection.png"
mkdir -p "$(dirname "$out")"
if ${maim}/bin/maim --color=.4,.7,1 --bordersize=1.0 --nodecorations=1 --hidecursor --format=png --quality=10 --noopengl --select \
| ${zbar}/bin/zbarimg --xml - > "$TMPFILE"; then
N=$(${yq}/bin/xq -r '.barcodes.source.index.symbol | if type == "array" then length else 1 end' < "$TMPFILE")
# Append codes Copy data separated by ---
DATA=$(${yq}/bin/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'"$(${yq}/bin/xq -r ".barcodes.source.index.symbol[$i].data" < "$TMPFILE")"
done
${xclip}/bin/xclip -selection clipboard <<< "$DATA"
${libnotify}/bin/notify-send \
"🔍 QR Code scan" " $N codes detected\n📋 copied ''${#DATA} bytes" \
--hint="string:image-path:$out" \
--hint="string:wired-tag:screenshot-$date" \
|| true
else
case "$?" in
"4")
${libnotify}/bin/notify-send \
"🔍 QR Code scan" " 0 codes detected" \
--hint="string:image-path:$out" \
--hint="string:wired-tag:screenshot-$date" \
|| true
;;
*)
${libnotify}/bin/notify-send \
"🔍 QR Code scan" " Error while processing image: zbarimg exited with code $?" \
--hint="string:image-path:$out" \
--hint="string:wired-tag:screenshot-$date" \
|| true
;;
esac
fi
'';
}

View file

@ -0,0 +1,46 @@
{
writeShellApplication,
libnotify,
xclip,
tesseract,
maim,
}:
writeShellApplication {
name = "screenshot-area";
text = ''
set -euo pipefail
umask 077
date=$(date +"%Y-%m-%dT%H:%M:%S%:z")
out="''${XDG_PICTURES_DIR-$HOME/Pictures}/screenshots/$date-selection.png"
mkdir -p "$(dirname "$out")"
${maim}/bin/maim --color=.4,.7,1,0.2 --bordersize=1.0 --nodecorations=1 \
--hidecursor --format=png --quality=10 --noopengl --select "$out"
${xclip}/bin/xclip -selection clipboard -t image/png < "$out"
action=$(${libnotify}/bin/notify-send \
"📷 Screenshot captured" "📋 copied to clipboard" \
--hint="string:wired-tag:screenshot-$date" \
--action=ocr=OCR) \
|| true
if [[ "$action" == "ocr" ]]; then
${libnotify}/bin/notify-send \
"📷 Screenshot captured" " Running OCR ..." \
--hint="string:wired-tag:screenshot-$date" \
|| true
if ${tesseract}/bin/tesseract "$out" - -l eng+deu | ${xclip}/bin/xclip -selection clipboard; then
${libnotify}/bin/notify-send \
"📷 Screenshot captured" "🔠 OCR copied to clipboard" \
--hint="string:wired-tag:screenshot-$date" \
|| true
else
${libnotify}/bin/notify-send \
"📷 Screenshot captured" " Error while running OCR" \
--hint="string:wired-tag:screenshot-$date" \
|| true
fi
fi
'';
}

View file

@ -0,0 +1,20 @@
{
writeShellApplication,
libnotify,
maim,
}:
writeShellApplication {
name = "screenshot-screen";
text = ''
set -euo pipefail
umask 077
out="''${XDG_PICTURES_DIR-$HOME/Pictures}/screenshots/$(date +"%Y-%m-%dT%H:%M:%S%:z")-fullscreen.png"
mkdir -p "$(dirname "$out")"
${maim}/bin/maim --hidecursor --format=png --quality=10 --noopengl "$out"
${libnotify}/bin/notify-send \
"📷 Screenshot captured" "💾 Saved to $out" \
|| true
'';
}