ctpv/sh/helpers.sh

85 lines
1.8 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-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; }
[ -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; }
exists chafa && { image_method="$image_method_chafa"; return 0; }
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-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 \
--place "${w}x${h}@${x}x${y}" "$1"
return 1
;;
"$image_method_chafa")
chafa --animate off -s "${w}x${h}" "$1"
;;
*)
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
}