Add chafa support

This commit is contained in:
Nikita Ivanov 2022-06-23 16:54:23 +05:00
parent 7e5a4ad7e4
commit 9aec6f82cb
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
11 changed files with 85 additions and 63 deletions

View File

@ -50,7 +50,7 @@ For example, you only need either `elinks`, `lynx` or
| diff | `colordiff` `delta` `diff-so-fancy` | | diff | `colordiff` `delta` `diff-so-fancy` |
| directory | `ls` | | directory | `ls` |
| html | `elinks` `lynx` `w3m` | | html | `elinks` `lynx` `w3m` |
| image | `ueberzug` | | image | `ueberzug` `chafa` |
| json | `jq` | | json | `jq` |
| markdown | `mdcat` | | markdown | `mdcat` |
| odt | `libreoffice` | | odt | `libreoffice` |

View File

@ -116,7 +116,7 @@ html T{
elinks lynx w3m elinks lynx w3m
T} T}
image T{ image T{
ueberzug ueberzug chafa
T} T}
json T{ json T{
jq jq

View File

@ -24,7 +24,7 @@ Preview b_previews[] = {
PR(NULL, "text", NULL, source_highlight), PR(NULL, "text", NULL, source_highlight),
PR(NULL, "text", NULL, cat), PR(NULL, "text", NULL, cat),
PR(NULL, "image", NULL, ueberzug), PR(NULL, "image", NULL, image),
PR(NULL, "video", NULL, video), PR(NULL, "video", NULL, video),
PR(NULL, "application", "pdf", pdf), PR(NULL, "application", "pdf", pdf),

View File

@ -1,7 +1,10 @@
setup_fifo 1 setup_image 1
if use_kitty; then case "$image_method" in
kitty +kitten icat --clear --transfer-mode file "$image_method_ueberzug")
elif use_ueberzug; then
printf '{"action": "remove", "identifier": "preview"}\n' > "$fifo" printf '{"action": "remove", "identifier": "preview"}\n' > "$fifo"
fi ;;
"$image_method_kitty")
kitty +kitten icat --clear --transfer-mode file
;;
esac

View File

@ -1,4 +1,4 @@
setup_fifo 1 setup_image 1
# tell ctpv server to exit # tell ctpv server to exit
use_ueberzug && printf '\0' > "$fifo" [ "$image_method" = "$image_method_ueberzug" ] && printf '\0' > "$fifo"

View File

@ -1,40 +1,11 @@
image_method_ueberzug='U'
image_method_kitty='K'
image_method_chafa='C'
echo_err() { echo_err() {
echo "$@" >&2 echo "$@" >&2
} }
is_kitty() {
[ -n "$KITTY_PID" ]
}
use_ueberzug() {
[ -n "$WAYLAND_DISPLAY" ] && return 1
[ -n "$DISPLAY" ] && exists ueberzug
}
use_kitty() {
[ -z "$forcekitty" ] && use_ueberzug && return 1
is_kitty
}
noimages() {
[ -n "$noimages" ]
}
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() {
use_ueberzug || return 1
exit_code="${1:-127}"
[ -n "$fifo" ] || exit "$exit_code"
[ -e "$fifo" ] || exit "$exit_code"
fifo_open "$fifo" || exit "$exit_code"
}
exists() { exists() {
command -v "$1" >/dev/null command -v "$1" >/dev/null
} }
@ -43,25 +14,71 @@ check_exists() {
exists "$@" || exit 127 exists "$@" || exit 127
} }
noimages() {
[ -n "$noimages" ]
}
is_kitty() {
[ -n "$KITTY_PID" ]
}
fifo_open() {
# https://unix.stackexchange.com/a/522940/183147
dd oflag=nonblock conv=notrunc,nocreat count=0 of="$1" \
>/dev/null 2>/dev/null
}
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; }
[ -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; }
}
setup_fifo() {
fifo_open "$fifo" || exit "${1:-127}"
}
setup_image() {
set_image_method
[ "$image_method" = "$image_method_ueberzug" ] && setup_fifo "$@"
}
send_image() { send_image() {
noimages && return 127 noimages && return 127
if use_kitty; then case "$image_method" in
kitty +kitten icat --transfer-mode file --align left \ "$image_method_ueberzug")
--place "${w}x${h}@${x}x${y}" "$1"
return 1
elif use_ueberzug; then
path="$(printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g')" 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" 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 return 1
else ;;
"$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 return 127
fi ;;
esac
} }
convert_and_show_image() { convert_and_show_image() {
noimages && exit 127 noimages && return 127
setup_fifo setup_image
[ -n "$cache_valid" ] || "$@" || exit "$?" [ -n "$cache_valid" ] || "$@" || exit "$?"
send_image "$cache_f" send_image "$cache_f"
} }

5
sh/prev/image.sh Normal file
View File

@ -0,0 +1,5 @@
# image: ueberzug chafa
setup_image
send_image "$f"

View File

@ -1,5 +0,0 @@
# image: ueberzug
setup_fifo
send_image "$f"

View File

@ -42,6 +42,7 @@ static VectorPreview *previews;
static struct Option options[] = { static struct Option options[] = {
{ "forcekitty", OPTION_BOOL, { .i = &ctpv.opts.forcekitty } }, { "forcekitty", OPTION_BOOL, { .i = &ctpv.opts.forcekitty } },
{ "forcechafa", OPTION_BOOL, { .i = &ctpv.opts.forcechafa } },
{ "noimages", OPTION_BOOL, { .i = &ctpv.opts.noimages } }, { "noimages", OPTION_BOOL, { .i = &ctpv.opts.noimages } },
}; };

View File

@ -14,7 +14,7 @@ struct CTPV {
} mode; } mode;
char *server_id_s; char *server_id_s;
struct { struct {
int forcekitty, noimages; int forcekitty, forcechafa, noimages;
} opts; } opts;
}; };

View File

@ -35,6 +35,7 @@ int run_script(char *script, size_t script_len, int *exitcode, int *signal,
SpawnProg sp, void *sp_arg) SpawnProg sp, void *sp_arg)
{ {
ERRCHK_RET_ERN(setenv("forcekitty", ctpv.opts.forcekitty ? "1" : "", 1) == -1); ERRCHK_RET_ERN(setenv("forcekitty", ctpv.opts.forcekitty ? "1" : "", 1) == -1);
ERRCHK_RET_ERN(setenv("forcechafa", ctpv.opts.forcechafa ? "1" : "", 1) == -1);
ERRCHK_RET_ERN(setenv("noimages", ctpv.opts.noimages ? "1" : "", 1) == -1); ERRCHK_RET_ERN(setenv("noimages", ctpv.opts.noimages ? "1" : "", 1) == -1);
char *scr = prepend_helpers(script, script_len); char *scr = prepend_helpers(script, script_len);