125 lines
2.9 KiB
Bash
Executable File
125 lines
2.9 KiB
Bash
Executable File
#!/bin/sh
|
|
set -euf
|
|
|
|
OUTFILE="${HOME}/pics/screenshots/$(date +"%Y-%m-%d_%H-%M-%S")" # output file template, change as needed
|
|
# menu + args, split on spaces
|
|
MENU="fzfmenu -p screenshot"
|
|
|
|
clean() {
|
|
[ -n "${freeze_pid:-}" ] && kill "$freeze_pid"
|
|
[ -f "${tmpfreeze:-}" ] && rm -f "$tmpfreeze"
|
|
[ -f "${tmpfp:-}" ] && rm -f "$tmpfp"
|
|
}
|
|
|
|
save() {
|
|
case "$1" in
|
|
*png*) oxipng -o max "$tmpfp" && mv "$tmpfp" "$OUTFILE.png" ;;
|
|
*jxl*) cjxl "$tmpfp" -d 0.0 "$OUTFILE.jxl" ;;
|
|
*) exit 0 ;;
|
|
esac
|
|
notify-send -u low "$OUTFILE"
|
|
exit 0
|
|
}
|
|
|
|
upload() {
|
|
case "$1" in
|
|
litterbox*)
|
|
time="${1##* }"
|
|
url=$(curl -F "reqtype=fileupload" -F "time=$time" -F "fileToUpload=@$tmpfp" 'https://litterbox.catbox.moe/resources/internals/api.php')
|
|
;;
|
|
owo) url=$(owo -u "$tmpfp") ;;
|
|
*) return ;;
|
|
esac
|
|
[ -n "$url" ] && notify-send -u low "$url" && $clip_cmd <<-EOF
|
|
$url
|
|
EOF
|
|
[ $? -ne 0 ] && notify-send -u low "ERROR! Not uploaded or something ! uh oh"
|
|
}
|
|
|
|
choose() {
|
|
while true; do
|
|
c="$(printf 'save (jxl)\nsave (png)\npreview\nedit\nupload\nscan' | $MENU)"
|
|
case "$c" in
|
|
save*) save "$c" ;;
|
|
edit) eval "$editor"' "$tmpfp"' ;;
|
|
scan)
|
|
zdata="$(zbarimg -1 "$tmpfp")" || zdata="error: scan failed with exit code $?"
|
|
export zdata
|
|
$TERMINAL -e sh -c 'printf "%s\n" "${zdata#*:}" | less' &
|
|
printf '%s\n' "${zdata#*:}"
|
|
;;
|
|
upload)
|
|
set +e
|
|
upload "$(printf 'owo\nlitterbox 1h\nlitterbox 12h\nlitterbox 24h\nlitterbox 72h\n' | $MENU)"
|
|
set -e
|
|
;;
|
|
preview)
|
|
"$image_viewer" "$tmpfp" &
|
|
;;
|
|
*) break ;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
freeze() {
|
|
tmpfreeze="$(mktemp -t screenshot_freeze_XXXX.png)"
|
|
$freeze_cmd "$tmpfreeze"
|
|
$freeze_img "$tmpfreeze" &
|
|
freeze_pid=$!
|
|
sleep 0.1
|
|
# x11: window manager makes it fullscreen
|
|
# doesn't work too well on cwm but idc
|
|
}
|
|
|
|
case "${XDG_SESSION_TYPE:-}" in
|
|
wayland)
|
|
editor='swappy -o "$tmpfp" -f'
|
|
freeze_img="swayimg -g 0,0,1920,1080"
|
|
image_viewer="nsxiv"
|
|
freeze_cmd="grim -l 0"
|
|
clip_cmd="wl-copy"
|
|
ss() {
|
|
if [ -n "$REGION" ]; then
|
|
reg="$(slurp -f '%x,%y %wx%h')" || return 1
|
|
fi
|
|
grim ${REGION:+-g "$reg"} "$tmpfp"
|
|
wl-copy < "$tmpfp"
|
|
}
|
|
;;
|
|
*)
|
|
editor='gimp -n'
|
|
freeze_img="nsxiv -b -N SCREENSHOT_FREEZE"
|
|
image_viewer="nsxiv"
|
|
freeze_cmd="shotgun"
|
|
clip_cmd="xclip -sel clipboard"
|
|
ss() {
|
|
if [ -n "$REGION" ]; then
|
|
reg="$(hacksaw -f '-i %i -g %g')" || return 1
|
|
fi
|
|
shotgun ${SCREEN:+-s} ${REGION:+$reg} \
|
|
${WINDOW:+-i $(xdotool getactivewindow)} "$tmpfp"
|
|
xclip -sel clipboard -t image/png < "$tmpfp"
|
|
}
|
|
;;
|
|
esac
|
|
|
|
tmpfp="$(mktemp -t screenshot_XXXX.png)"
|
|
trap 'clean' EXIT
|
|
|
|
SCREEN="" REGION="" WINDOW=""
|
|
while getopts :srwf OPT; do
|
|
case "$OPT" in
|
|
s) SCREEN=1 ;; # screen
|
|
r) REGION=1 ;; # region
|
|
w) WINDOW=1 ;; # active window (don't use with region)
|
|
f) freeze ;; # freeze screen, only useful with -r
|
|
*) echo "invalid option: -$OPTARG" && exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
ss
|
|
ec=$?
|
|
|
|
[ -n "${freeze_pid:-}" ] && kill "$freeze_pid" && freeze_pid="" # unfreeze
|
|
[ "$ec" -eq 0 ] && choose
|