From b1536af727e5dcb98796a9a25e368155457734a0 Mon Sep 17 00:00:00 2001 From: chenyc Date: Fri, 18 Nov 2022 11:34:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=BE=E7=89=87=E9=A2=84?= =?UTF-8?q?=E8=A7=88=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ config/fzf.zsh | 5 +++-- lib/file_preview.sh | 2 +- lib/get_cursor.sh | 12 +++++++++++ lib/img_preview.sh | 43 +++++++++++++++++++++++++++++++++++++ plugins/fzf-tab/fzf-tab.zsh | 1 + 6 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 lib/get_cursor.sh create mode 100644 lib/img_preview.sh diff --git a/README.md b/README.md index 75fd4c0..79dcb3e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ fork自 oh my zsh,更纯净 更快速 - fd - bat (可选 更好的文本预览效果) - exa (可选 更好的目录预览效果) +- ueberzug (可选 可在x11终端下预览图片) ```plaintext 请自行安装fd @@ -88,6 +89,7 @@ omz ├── cache/ -- 部分需要缓存的配置 ├── lib/ -- 部分依赖文件 │   ├── omz.sh -- omz.sh + │   ├── img_preview.sh -- 图片预览脚本 for fzf │   └── file_preview.sh -- 文件预览脚本 for fzf ├── config/ │   ├── fzf.zsh -- fzf 及 fzf-tab配置 diff --git a/config/fzf.zsh b/config/fzf.zsh index 62e53e0..8c42c6b 100644 --- a/config/fzf.zsh +++ b/config/fzf.zsh @@ -1,6 +1,6 @@ -alias fzf='fzf --preview "bat -p --color=always {} | head -100" --height 40%' +alias fzf='fzf --preview "bash $OMZ/lib/file_preview.sh {}" --height 12' -export FZF_DEFAULT_OPTS="--height 50% --layout=reverse --history=$OMZ/cache/fzfhistory" +export FZF_DEFAULT_OPTS="--height 12 --layout=reverse --history=$OMZ/cache/fzfhistory" export FZF_DEFAULT_COMMAND="fd --exclude={.git,.idea,.vscode,.sass-cache,node_modules,build,dist,vendor} --type f" export FZF_PREVIEW_COMMAND='bash $OMZ/lib/file_preview.sh {}' @@ -17,4 +17,5 @@ zstyle ':fzf-tab:complete:git-log:*' fzf-preview 'git log --color=always $word' zstyle ':fzf-tab:complete:git-show:*' fzf-preview 'git show --color=always $word' zstyle ':fzf-tab:complete:git-checkout:*' fzf-preview '[ -f "$realpath" ] && git diff --color=always $word || git log --color=always $word' zstyle ':fzf-tab:complete:*:*' fzf-preview 'less ${(Q)realpath}' +zstyle ':fzf-tab:complete:*:*' fzf-flags --height=12 export LESSOPEN='| bash $OMZ/lib/file_preview.sh %s' diff --git a/lib/file_preview.sh b/lib/file_preview.sh index b97d0a8..d8b98f4 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 -100 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..553bd60 --- /dev/null +++ b/lib/get_cursor.sh @@ -0,0 +1,12 @@ +#!/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..60dc76c --- /dev/null +++ b/lib/img_preview.sh @@ -0,0 +1,43 @@ +#!/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) +} + +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 0d3acde..a3fb396 100644 --- a/plugins/fzf-tab/fzf-tab.zsh +++ b/plugins/fzf-tab/fzf-tab.zsh @@ -208,6 +208,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