添加图片预览脚本

This commit is contained in:
chenyc 2022-11-18 11:34:21 +08:00
parent d136130771
commit b1536af727
6 changed files with 62 additions and 3 deletions

View File

@ -27,6 +27,7 @@ fork自 oh my zsh更纯净 更快速
- fd - fd
- bat (可选 更好的文本预览效果) - bat (可选 更好的文本预览效果)
- exa (可选 更好的目录预览效果) - exa (可选 更好的目录预览效果)
- ueberzug (可选 可在x11终端下预览图片)
```plaintext ```plaintext
请自行安装fd 请自行安装fd
@ -88,6 +89,7 @@ omz
├── cache/ -- 部分需要缓存的配置 ├── cache/ -- 部分需要缓存的配置
├── lib/ -- 部分依赖文件 ├── lib/ -- 部分依赖文件
│   ├── omz.sh -- omz.sh │   ├── omz.sh -- omz.sh
│   ├── img_preview.sh -- 图片预览脚本 for fzf
│   └── file_preview.sh -- 文件预览脚本 for fzf │   └── file_preview.sh -- 文件预览脚本 for fzf
├── config/ ├── config/
│   ├── fzf.zsh -- fzf 及 fzf-tab配置 │   ├── fzf.zsh -- fzf 及 fzf-tab配置

View File

@ -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_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 {}' 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-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: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-preview 'less ${(Q)realpath}'
zstyle ':fzf-tab:complete:*:*' fzf-flags --height=12
export LESSOPEN='| bash $OMZ/lib/file_preview.sh %s' export LESSOPEN='| bash $OMZ/lib/file_preview.sh %s'

View File

@ -6,7 +6,7 @@ if [ -d "$1" ]; then
elif [ "$category" = text ]; then elif [ "$category" = text ]; then
(bat -p --color=always "$1" || cat "$1") 2>/dev/null | head -100 (bat -p --color=always "$1" || cat "$1") 2>/dev/null | head -100
elif [ "$category" = image ]; then elif [ "$category" = image ]; then
img2txt "$1" command -v ueberzug 2&>/dev/null && bash $OMZ/lib/img_preview.sh "$1"|| img2txt "$1"
else else
echo $1 is a $category file echo $1 is a $category file
fi fi

12
lib/get_cursor.sh Normal file
View File

@ -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

43
lib/img_preview.sh Normal file
View File

@ -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

View File

@ -208,6 +208,7 @@ fzf-tab-debug() {
} }
fzf-tab-complete() { fzf-tab-complete() {
bash $OMZ/lib/get_cursor.sh
# this name must be ugly to avoid clashes # this name must be ugly to avoid clashes
local -i _ftb_continue=1 _ftb_continue_last=0 _ftb_accept=0 ret=0 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 # hide the cursor until finishing completion, so that users won't see cursor up and down