Merge pull request #9 from lazypool/refactor-img-preview
使用 ueberzugpp 改善 fzf 对图片的预览功能
This commit is contained in:
commit
69c4556318
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,8 @@
|
||||||
#!/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 {
|
|
||||||
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
|
source $OMZ/cache/cursor
|
||||||
X=$(($COLUMNS / 2 + 2))
|
X=$(($COLUMNS / 2 + 2))
|
||||||
Y=$((row + 2))
|
Y=$((row + 2))
|
||||||
|
|
@ -30,15 +10,17 @@ function draw_preview {
|
||||||
Y=$((LINES - 11))
|
Y=$((LINES - 11))
|
||||||
fi
|
fi
|
||||||
|
|
||||||
>"${UEBERZUG_FIFO}" declare -A -p cmd=( \
|
function draw_preview {
|
||||||
[action]=add [identifier]="${PREVIEW_ID}" \
|
ueberzugpp cmd -s $UEBERZUGPP_SOCKET -i $PREVIEW_ID -a add \
|
||||||
[x]="${X}" [y]="${Y}" \
|
-x $X -y $Y --max-width $(($COLUMNS / 2 - 2)) --max-height 10 \
|
||||||
[width]="$(($COLUMNS / 2 - 2))" [height]="10" \
|
-f ${@}
|
||||||
[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
|
||||||
|
|
|
||||||
1
omz.zsh
1
omz.zsh
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue