From 230fc49ae216169f9812adcf8942bba3993e61e0 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 7 Nov 2023 11:42:32 +0900 Subject: (Experimental) Add support for iTerm2 inline image protocol Close #1102 fzf --preview 'imgcat -W $FZF_PREVIEW_COLUMNS -H $FZF_PREVIEW_LINES {}' Notes: * There is no good way to determine the height of the rendered image, so we assume that the image takes the full height of the preview window. So the image cannot be displayed with the other text. * fzf-preview.sh script was updated to use `imgcat` if it's available but `chafa` is not. * iTerm2 also supports Sixel, so adding support for this protocol is not quite necessary but it renders animated GIFs much better (e.g. looping). --- bin/fzf-preview.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/fzf-preview.sh b/bin/fzf-preview.sh index d72cd2d4..8991e3de 100755 --- a/bin/fzf-preview.sh +++ b/bin/fzf-preview.sh @@ -4,8 +4,9 @@ # image in the preview window of fzf. # # Dependencies: -# - https://github.com/hpjansson/chafa # - https://github.com/sharkdp/bat +# - https://github.com/hpjansson/chafa +# - https://iterm2.com/utilities/imgcat if [[ $# -ne 1 ]]; then >&2 echo "usage: $0 FILENAME" @@ -44,6 +45,7 @@ elif ! [[ $KITTY_WINDOW_ID ]] && (( FZF_PREVIEW_TOP + FZF_PREVIEW_LINES == $(stt dim=${FZF_PREVIEW_COLUMNS}x$((FZF_PREVIEW_LINES - 1)) fi +# 1. Use kitty icat on kitty terminal if [[ $KITTY_WINDOW_ID ]]; then # 1. 'memory' is the fastest option but if you want the image to be scrollable, # you have to use 'stream'. @@ -52,10 +54,21 @@ if [[ $KITTY_WINDOW_ID ]]; then # 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="$dim@0x0" "$file" | sed '$d' | sed $'$s/$/\e[m/' + +# 2. Use chafa with Sixel output elif command -v chafa > /dev/null; then chafa -f sixel -s "$dim" "$file" # Add a new line character so that fzf can display multiple images in the preview window echo + +# 3. If chafa is not found but imgcat is available, use it on iTerm2 +elif command -v imgcat > /dev/null; then + # NOTE: We should use https://iterm2.com/utilities/it2check to check if the + # user is running iTerm2. But for the sake of simplicty, we just assume + # that's the case here. + imgcat -W "${dim%%x*}" -H "${dim##*x}" "$file" + +# 4. Cannot find any suitable method to preview the image else file "$file" fi -- cgit v1.2.3