refactor: use ueberzugpp and improve img preview methods
This commit is contained in:
parent
013333420f
commit
0453494b0a
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,28 +1,8 @@
|
|||
#!/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))
|
||||
|
|
@ -30,15 +10,17 @@ function draw_preview {
|
|||
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]="${@}")
|
||||
function draw_preview {
|
||||
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
|
||||
|
|
|
|||
1
omz.zsh
1
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue