ctpv/sh/helpers.sh

60 lines
1.2 KiB
Bash
Raw Normal View History

echo_err() {
echo "$@" >&2
}
2022-06-19 15:03:39 +02:00
is_kitty() {
[ -n "$KITTY_PID" ]
}
use_ueberzug() {
exists ueberzug
}
use_kitty() {
[ -z "$forcekitty" ] && use_ueberzug && return 1
2022-06-19 15:03:39 +02:00
is_kitty
}
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
}
setup_fifo() {
2022-06-19 15:03:39 +02:00
use_ueberzug || return 1
exit_code="${1:-127}"
2022-05-31 20:40:31 +02:00
[ -n "$fifo" ] || exit "$exit_code"
2022-05-26 01:48:27 +02:00
[ -e "$fifo" ] || exit "$exit_code"
fifo_open "$fifo" || exit "$exit_code"
2022-05-25 23:57:50 +02:00
}
2022-05-22 09:55:04 +02:00
exists() {
2022-06-11 10:39:19 +02:00
command -v "$1" >/dev/null
2022-05-22 09:55:04 +02:00
}
2022-05-25 23:57:50 +02:00
2022-06-17 19:50:17 +02:00
check_exists() {
exists "$@" || exit 127
}
2022-05-28 22:38:29 +02:00
send_image() {
2022-06-19 15:03:39 +02:00
if use_kitty; then
kitty +kitten icat --transfer-mode file --align left \
--place "${w}x${h}@${x}x${y}" "$1"
return 1
elif use_ueberzug; then
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
else
return 127
fi
2022-05-26 01:48:27 +02:00
}
convert_and_show_image() {
setup_fifo
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
}