Debian Codegraph @

bin/fzf-preview.sh

raw
1 #!/usr/bin/env bash
2 #
3 # The purpose of this script is to demonstrate how to preview a file or an
4 # image in the preview window of fzf.
5 #
6 # Dependencies:
7 # - https://github.com/sharkdp/bat
8 # - https://github.com/hpjansson/chafa
9 # - https://iterm2.com/utilities/imgcat
10
11 if [[ $# -ne 1 ]]; then
12 >&2 echo "usage: $0 FILENAME[:LINENO][:IGNORED]"
13 exit 1
14 fi
15
16 file=${1/#\~\//$HOME/}
17
18 center=0
19 if [[ ! -r $file ]]; then
20 if [[ $file =~ ^(.+):([0-9]+)\ *$ ]] && [[ -r ${BASH_REMATCH[1]} ]]; then
21 file=${BASH_REMATCH[1]}
22 center=${BASH_REMATCH[2]}
23 elif [[ $file =~ ^(.+):([0-9]+):[0-9]+\ *$ ]] && [[ -r ${BASH_REMATCH[1]} ]]; then
24 file=${BASH_REMATCH[1]}
25 center=${BASH_REMATCH[2]}
26 fi
27 fi
28
29 type=$(file --brief --dereference --mime -- "$file")
30
31 if [[ ! $type =~ image/ ]]; then
32 if [[ $type =~ =binary ]]; then
33 file "$1"
34 exit
35 fi
36
37 # Sometimes bat is installed as batcat.
38 if command -v batcat > /dev/null; then
39 batname="batcat"
40 elif command -v bat > /dev/null; then
41 batname="bat"
42 else
43 cat "$1"
44 exit
45 fi
46
47 ${batname} --style="${BAT_STYLE:-numbers}" --color=always --pager=never --highlight-line="${center:-0}" -- "$file"
48 exit
49 fi
50
51 dim=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}
52 if [[ $dim == x ]]; then
53 dim=$(stty size < /dev/tty | awk '{print $2 "x" $1}')
54 elif ! [[ $KITTY_WINDOW_ID ]] && ((FZF_PREVIEW_TOP + FZF_PREVIEW_LINES == $(stty size < /dev/tty | awk '{print $1}'))); then
55 # Avoid scrolling issue when the Sixel image touches the bottom of the screen
56 # * https://github.com/junegunn/fzf/issues/2544
57 dim=${FZF_PREVIEW_COLUMNS}x$((FZF_PREVIEW_LINES - 1))
58 fi
59
60 # 1. Use icat (from Kitty) if kitten is installed
61 if [[ $KITTY_WINDOW_ID ]] || [[ $GHOSTTY_RESOURCES_DIR ]] && command -v kitten > /dev/null; then
62 # 1. 'memory' is the fastest option but if you want the image to be scrollable,
63 # you have to use 'stream'.
64 #
65 # 2. The last line of the output is the ANSI reset code without newline.
66 # This confuses fzf and makes it render scroll offset indicator.
67 # So we remove the last line and append the reset code to its previous line.
68 kitten icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place="$dim@0x0" "$file" | sed '$d' | sed $'$s/$/\e[m/'
69
70 # 2. Use chafa with Sixel output
71 elif command -v chafa > /dev/null; then
72 chafa -s "$dim" "$file"
73 # Add a new line character so that fzf can display multiple images in the preview window
74 echo
75
76 # 3. If chafa is not found but imgcat is available, use it on iTerm2
77 elif command -v imgcat > /dev/null; then
78 # NOTE: We should use https://iterm2.com/utilities/it2check to check if the
79 # user is running iTerm2. But for the sake of simplicity, we just assume
80 # that's the case here.
81 imgcat -W "${dim%%x*}" -H "${dim##*x}" "$file"
82
83 # 4. Cannot find any suitable method to preview the image
84 else
85 file "$file"
86 fi