fzf-preview.sh: Fall back to `stty size` (#2544)

This commit is contained in:
Junegunn Choi 2023-11-02 00:14:11 +09:00
parent 278dce9ba6
commit 69176fc5f4
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
1 changed files with 8 additions and 2 deletions

View File

@ -15,6 +15,11 @@ fi
file=${1/#\~\//$HOME/}
type=$(file --mime-type "$file")
dim=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}
if [[ $dim = x ]]; then
dim=$(stty size | awk '{print $2 "x" $1}')
fi
if [[ ! $type =~ image/ ]]; then
# Sometimes bat is installed as batcat.
if command -v batcat > /dev/null; then
@ -34,9 +39,10 @@ elif [[ $KITTY_WINDOW_ID ]]; then
# 2. The last line of the output is the ANSI reset code without newline.
# This confuses fzf and makes it render scroll offset indicator.
# So we remove the last line and append the reset code to its previous line.
kitty icat --clear --transfer-mode=memory --stdin=no --place="${FZF_PREVIEW_COLUMNS}x$((FZF_PREVIEW_LINES))@0x0" "$file" | sed '$d' | sed $'$s/$/\e[m/'
kitty icat --clear --transfer-mode=memory --stdin=no --place="$dim@0x0" "$file" | sed '$d' | sed $'$s/$/\e[m/'
elif command -v chafa > /dev/null; then
chafa -f sixel -s "${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}" "$file"
chafa -f sixel -s "$dim" "$file"
echo
else
file "$file"
fi