refactor: use ueberzugpp and improve img preview methods

This commit is contained in:
lazypool 2025-10-26 16:16:36 +08:00
parent 013333420f
commit 0453494b0a
4 changed files with 45 additions and 36 deletions

View File

@ -1,12 +1,15 @@
#! /usr/bin/env sh #! /usr/bin/env sh
mime=$(file -bL --mime-type "$1") mime=$(file -bL --mime-type "$1")
category=${mime%%/*} category=${mime%%/*}
command -v ueberzugpp >/dev/null 2>&1 && [ -n "$UEBERZUGPP_SOCKET" ] && ub_avail="yes"
if [ -d "$1" ]; then if [ -d "$1" ]; then
[ "$ub_avail" = "yes" ] && bash $OMZ/lib/img_preview.sh remove
exa -l --no-user --no-time --icons --no-permissions --no-filesize "$1" 2>/dev/null || ls --color=always "$1" 2>/dev/null || ls -G "$1" exa -l --no-user --no-time --icons --no-permissions --no-filesize "$1" 2>/dev/null || ls --color=always "$1" 2>/dev/null || ls -G "$1"
elif [ "$category" = text ]; then elif [ "$category" = text ]; then
[ "$ub_avail" = "yes" ] && bash $OMZ/lib/img_preview.sh remove
(bat -p --color=always "$1" || cat "$1") 2>/dev/null | head -1000 (bat -p --color=always "$1" || cat "$1") 2>/dev/null | head -1000
elif [ "$category" = image ]; then elif [ "$category" = image ]; then
command -v ueberzug 2&>/dev/null && bash $OMZ/lib/img_preview.sh "$1"|| img2txt "$1" [ "$ub_avail" = "yes" ] && bash $OMZ/lib/img_preview.sh draw "$1"|| img2txt "$1"
else else
echo $1 is a $category file echo $1 is a $category file
(bat -p --color=always "$1" || cat "$1") 2>/dev/null | head -1000 (bat -p --color=always "$1" || cat "$1") 2>/dev/null | head -1000

View File

@ -1,44 +1,26 @@
#!/usr/bin/env bash #!/usr/bin/env bash
declare -r -x DEFAULT_PREVIEW_POSITION="right" declare -r -x DEFAULT_PREVIEW_POSITION="right"
declare -r -x UEBERZUG_FIFO="$(mktemp --dry-run --suffix "fzf-$$-ueberzug")"
declare -r -x PREVIEW_ID="preview" declare -r -x PREVIEW_ID="preview"
function start_ueberzug { source $OMZ/cache/cursor
mkfifo "${UEBERZUG_FIFO}" X=$(($COLUMNS / 2 + 2))
<"${UEBERZUG_FIFO}" \ Y=$((row + 2))
ueberzug layer --parser bash --silent & if [ $Y -gt $((LINES - 11)) ]; then
3>"${UEBERZUG_FIFO}" \ Y=$((LINES - 11))
exec fi
}
function finalise {
3>&- \
exec
&>/dev/null \
rm "${UEBERZUG_FIFO}"
&>/dev/null \
kill $(jobs -p)
killall ueberzug
}
function draw_preview { function draw_preview {
source $OMZ/cache/cursor ueberzugpp cmd -s $UEBERZUGPP_SOCKET -i $PREVIEW_ID -a add \
X=$(($COLUMNS / 2 + 2)) -x $X -y $Y --max-width $(($COLUMNS / 2 - 2)) --max-height 10 \
Y=$((row + 2)) -f ${@}
if [ $Y -gt $((LINES - 11)) ]; then
Y=$((LINES - 11))
fi
>"${UEBERZUG_FIFO}" declare -A -p cmd=( \
[action]=add [identifier]="${PREVIEW_ID}" \
[x]="${X}" [y]="${Y}" \
[width]="$(($COLUMNS / 2 - 2))" [height]="10" \
[scaler]=forced_cover [scaling_position_x]=0.5 [scaling_position_y]=0.5 \
[path]="${@}")
} }
trap finalise EXIT function remove_preview {
start_ueberzug ueberzugpp cmd -s $UEBERZUGPP_SOCKET -i $PREVIEW_ID -a remove
draw_preview $* }
sleep 999999
case "$1" in
draw) shift; draw_preview $* ;;
remove) remove_preview ;;
esac

View File

@ -1,4 +1,5 @@
export OMZ=$(cd $(dirname $0);pwd) export OMZ=$(cd $(dirname $0);pwd)
export UEBERZUGPP_PID_FILE=$OMZ/cache/ubpidfile
source $OMZ/config/omz.zsh source $OMZ/config/omz.zsh
source $OMZ/config/git.zsh source $OMZ/config/git.zsh
source $OMZ/config/fzf.zsh source $OMZ/config/fzf.zsh

View File

@ -87,6 +87,27 @@
builtin compadd -U -qS '' -R -ftb-remove-space '' builtin compadd -U -qS '' -R -ftb-remove-space ''
} }
# try to start and stop fzf and ueberzugpp simultaneously
-ftb-start-ueberzugpp() {
case "$(uname -a)" in
*Darwin*) ub_tmp_dir="$TMPDIR" ;;
*) ub_tmp_dir="/tmp" ;;
esac
if command -v ueberzugpp >/dev/null 2>&1; then
ueberzugpp layer --no-stdin -so x11 --pid-file "$UEBERZUGPP_PID_FILE"
ub_pid=$(cat "$UEBERZUGPP_PID_FILE")
export UEBERZUGPP_SOCKET="$ub_tmp_dir"/ueberzugpp-"$ub_pid".socket
fi
}
-ftb-stop-ueberzugpp() {
if [[ -n "$UEBERZUGPP_SOCKET" && -S "$UEBERZUGPP_SOCKET" ]]; then
ueberzugpp cmd -s "$UEBERZUGPP_SOCKET" -a "exit"
unset UEBERZUGPP_SOCKET
fi
}
# when insert multi results, a whitespace will be added to each result # when insert multi results, a whitespace will be added to each result
# remove left space of our fake result because I can't remove right space # remove left space of our fake result because I can't remove right space
# FIXME: what if the left char is not whitespace: `echo $widgets[\t` # FIXME: what if the left char is not whitespace: `echo $widgets[\t`
@ -132,9 +153,11 @@
-ftb-zstyle -s print-query print_query || print_query=alt-enter -ftb-zstyle -s print-query print_query || print_query=alt-enter
-ftb-zstyle -s accept-line accept_line -ftb-zstyle -s accept-line accept_line
-ftb-start-ueberzugpp
choices=("${(@f)"$(builtin print -rl -- $_ftb_headers $_ftb_complist | -ftb-fzf)"}") choices=("${(@f)"$(builtin print -rl -- $_ftb_headers $_ftb_complist | -ftb-fzf)"}")
ret=$? ret=$?
# choices=(query_string expect_key returned_word) # choices=(query_string expect_key returned_word)
-ftb-stop-ueberzugpp
# insert query string directly # insert query string directly
if [[ $choices[2] == $print_query ]] || [[ -n $choices[1] && $#choices == 1 ]] ; then if [[ $choices[2] == $print_query ]] || [[ -n $choices[1] && $#choices == 1 ]] ; then