diff --git a/lib/file_preview.sh b/lib/file_preview.sh index 20dafcd..96f10b0 100755 --- a/lib/file_preview.sh +++ b/lib/file_preview.sh @@ -6,7 +6,7 @@ if [ -d "$1" ]; then elif [ "$category" = text ]; then (bat -p --color=always "$1" || cat "$1") 2>/dev/null | head -1000 elif [ "$category" = image ]; then - img2txt "$1" + command -v ueberzug 2&>/dev/null && bash $OMZ/lib/img_preview.sh "$1"|| img2txt "$1" else echo $1 is a $category file fi diff --git a/lib/get_cursor.sh b/lib/get_cursor.sh new file mode 100644 index 0000000..c2d108a --- /dev/null +++ b/lib/get_cursor.sh @@ -0,0 +1,13 @@ +#!/bin/bash +# 用于在行为发生时获取当前的光标位置 + +exec < /dev/tty +oldstty=$(stty -g) +stty raw -echo min 0 +echo -en "\033[6n" > /dev/tty +IFS=';' read -r -d R -a pos +stty $oldstty +row=$((${pos[0]:2} - 1)) +col=$((${pos[1]} - 1)) +echo "export row=$row" > $OMZ/cache/cursor +echo "export col=$col" >> $OMZ/cache/cursor diff --git a/lib/img_preview.sh b/lib/img_preview.sh new file mode 100644 index 0000000..e224c10 --- /dev/null +++ b/lib/img_preview.sh @@ -0,0 +1,44 @@ +#!/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 +} + +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]="${@}") +} + +trap finalise EXIT +start_ueberzug +draw_preview $* +sleep 999999 diff --git a/plugins/fzf-tab/fzf-tab.zsh b/plugins/fzf-tab/fzf-tab.zsh index 3269670..15413bc 100644 --- a/plugins/fzf-tab/fzf-tab.zsh +++ b/plugins/fzf-tab/fzf-tab.zsh @@ -211,6 +211,7 @@ fzf-tab-debug() { } fzf-tab-complete() { + bash $OMZ/lib/get_cursor.sh # this name must be ugly to avoid clashes local -i _ftb_continue=1 _ftb_continue_last=0 _ftb_accept=0 ret=0 # hide the cursor until finishing completion, so that users won't see cursor up and down