ctpv/sh/helpers.sh

117 lines
2.5 KiB
Bash
Raw Normal View History

2022-06-23 13:54:23 +02:00
image_method_ueberzug='U'
image_method_kitty='K'
image_method_chafa='C'
echo_err() {
echo "$@" >&2
}
2022-06-23 13:54:23 +02:00
exists() {
command -v "$1" >/dev/null
2022-06-19 15:03:39 +02:00
}
2022-06-23 13:54:23 +02:00
check_exists() {
exists "$@" || exit 127
2022-06-19 15:03:39 +02:00
}
2022-06-20 23:06:49 +02:00
noimages() {
[ -n "$noimages" ]
}
2022-06-23 13:54:23 +02:00
is_kitty() {
[ -n "$KITTY_PID" ]
}
2022-07-17 11:00:59 +02:00
kitty_clear() {
kitty +kitten icat --clear --transfer-mode file
}
2022-05-26 01:48:27 +02:00
fifo_open() {
# https://unix.stackexchange.com/a/522940/183147
dd oflag=nonblock conv=notrunc,nocreat count=0 of="$1" \
>/dev/null 2>/dev/null
}
2022-06-23 13:54:23 +02:00
set_image_method() {
image_method=
[ -n "$forcekitty" ] && is_kitty && { image_method="$image_method_kitty"; return 0; }
2022-07-07 15:55:13 +02:00
[ -n "$forcekittyanim" ] && is_kitty && is_anim_image && { image_method="$image_method_kitty"; return 0; }
2022-06-23 13:54:23 +02:00
[ -n "$forcechafa" ] && exists chafa && { image_method="$image_method_chafa"; return 0; }
2022-06-19 15:03:39 +02:00
2022-06-23 13:54:23 +02:00
[ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && exists ueberzug &&
[ -n "$fifo" ] && [ -e "$fifo" ] &&
{ image_method="$image_method_ueberzug"; return 0; }
is_kitty && { image_method="$image_method_kitty"; return 0; }
2022-06-24 09:57:06 +02:00
exists chafa && exists convert && { image_method="$image_method_chafa"; return 0; }
}
is_anim_image() {
case "$m" in
image/apng|image/gif|image/avif|image/webp)
return 0 ;;
*)
return 1 ;;
esac
}
prepare_anim_img() {
if [ "$1" != "$cache_f" ] && is_anim_image "$1"; then
convert "${1}[0]" "jpg:${cache_f}" && printf '%s\n' "$cache_f"
else
printf '%s\n' "$1"
fi
}
chafa_run() {
_f="$(prepare_anim_img "$1")" && chafa -s "${w}x${h}" "$_f"
2022-05-25 23:57:50 +02:00
}
2022-06-23 13:54:23 +02:00
setup_fifo() {
fifo_open "$fifo" || exit "${1:-127}"
2022-05-22 09:55:04 +02:00
}
2022-05-25 23:57:50 +02:00
2022-06-23 13:54:23 +02:00
setup_image() {
set_image_method
[ "$image_method" = "$image_method_ueberzug" ] && setup_fifo "$@"
2022-06-17 19:50:17 +02:00
}
2022-07-17 11:00:59 +02:00
kitty_icat_pid() {
printf '/tmp/ctpvicat.%d' "$id"
}
2022-05-28 22:38:29 +02:00
send_image() {
2022-06-20 23:06:49 +02:00
noimages && return 127
2022-06-23 13:54:23 +02:00
case "$image_method" in
"$image_method_ueberzug")
path="$(printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g')"
printf '{ "action": "add", "identifier": "preview", "x": %d, "y": %d, "width": %d, "height": %d, "scaler": "contain", "scaling_position_x": 0.5, "scaling_position_y": 0.5, "path": "%s"}\n' "$x" "$y" "$w" "$h" "$path" > "$fifo"
return 1
;;
"$image_method_kitty")
kitty +kitten icat --transfer-mode file --align left \
2022-07-17 11:00:59 +02:00
--place "${w}x${h}@${x}x${y}" "$1" &
printf '%d\n' "$!" > "$(kitty_icat_pid)"
wait
2022-06-23 13:54:23 +02:00
return 1
;;
"$image_method_chafa")
2022-06-24 09:57:06 +02:00
chafa_run "$1"
2022-06-23 13:54:23 +02:00
;;
*)
return 127
;;
esac
2022-05-26 01:48:27 +02:00
}
convert_and_show_image() {
2022-06-23 13:54:23 +02:00
noimages && return 127
setup_image
2022-05-31 20:47:04 +02:00
[ -n "$cache_valid" ] || "$@" || exit "$?"
2022-05-28 22:38:29 +02:00
send_image "$cache_f"
2022-05-25 23:57:50 +02:00
}