From 0453494b0a54950586b83d53dc22238657f96d9a Mon Sep 17 00:00:00 2001 From: lazypool Date: Sun, 26 Oct 2025 16:16:36 +0800 Subject: [PATCH] refactor: use ueberzugpp and improve img preview methods --- lib/file_preview.sh | 5 +++- lib/img_preview.sh | 52 ++++++++++++------------------------- omz.zsh | 1 + plugins/fzf-tab/fzf-tab.zsh | 23 ++++++++++++++++ 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/lib/file_preview.sh b/lib/file_preview.sh index efd50f2..2af777d 100755 --- a/lib/file_preview.sh +++ b/lib/file_preview.sh @@ -1,12 +1,15 @@ #! /usr/bin/env sh mime=$(file -bL --mime-type "$1") category=${mime%%/*} +command -v ueberzugpp >/dev/null 2>&1 && [ -n "$UEBERZUGPP_SOCKET" ] && ub_avail="yes" 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" 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 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 echo $1 is a $category file (bat -p --color=always "$1" || cat "$1") 2>/dev/null | head -1000 diff --git a/lib/img_preview.sh b/lib/img_preview.sh index e224c10..3ca08fa 100644 --- a/lib/img_preview.sh +++ b/lib/img_preview.sh @@ -1,44 +1,26 @@ #!/usr/bin/env bash declare -r -x DEFAULT_PREVIEW_POSITION="right" -declare -r -x UEBERZUG_FIFO="$(mktemp --dry-run --suffix "fzf-$$-ueberzug")" declare -r -x PREVIEW_ID="preview" -function start_ueberzug { - mkfifo "${UEBERZUG_FIFO}" - <"${UEBERZUG_FIFO}" \ - ueberzug layer --parser bash --silent & - 3>"${UEBERZUG_FIFO}" \ - exec -} - -function finalise { - 3>&- \ - exec - &>/dev/null \ - rm "${UEBERZUG_FIFO}" - &>/dev/null \ - kill $(jobs -p) - killall ueberzug -} +source $OMZ/cache/cursor +X=$(($COLUMNS / 2 + 2)) +Y=$((row + 2)) +if [ $Y -gt $((LINES - 11)) ]; then + Y=$((LINES - 11)) +fi function draw_preview { - source $OMZ/cache/cursor - X=$(($COLUMNS / 2 + 2)) - Y=$((row + 2)) - 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]="${@}") + ueberzugpp cmd -s $UEBERZUGPP_SOCKET -i $PREVIEW_ID -a add \ + -x $X -y $Y --max-width $(($COLUMNS / 2 - 2)) --max-height 10 \ + -f ${@} } -trap finalise EXIT -start_ueberzug -draw_preview $* -sleep 999999 +function remove_preview { + ueberzugpp cmd -s $UEBERZUGPP_SOCKET -i $PREVIEW_ID -a remove +} + +case "$1" in + draw) shift; draw_preview $* ;; + remove) remove_preview ;; +esac diff --git a/omz.zsh b/omz.zsh index 4771729..c001cde 100644 --- a/omz.zsh +++ b/omz.zsh @@ -1,4 +1,5 @@ export OMZ=$(cd $(dirname $0);pwd) +export UEBERZUGPP_PID_FILE=$OMZ/cache/ubpidfile source $OMZ/config/omz.zsh source $OMZ/config/git.zsh source $OMZ/config/fzf.zsh diff --git a/plugins/fzf-tab/fzf-tab.zsh b/plugins/fzf-tab/fzf-tab.zsh index 15413bc..23cb7d8 100644 --- a/plugins/fzf-tab/fzf-tab.zsh +++ b/plugins/fzf-tab/fzf-tab.zsh @@ -87,6 +87,27 @@ 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 # 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` @@ -132,9 +153,11 @@ -ftb-zstyle -s print-query print_query || print_query=alt-enter -ftb-zstyle -s accept-line accept_line + -ftb-start-ueberzugpp choices=("${(@f)"$(builtin print -rl -- $_ftb_headers $_ftb_complist | -ftb-fzf)"}") ret=$? # choices=(query_string expect_key returned_word) + -ftb-stop-ueberzugpp # insert query string directly if [[ $choices[2] == $print_query ]] || [[ -n $choices[1] && $#choices == 1 ]] ; then