Skip to content
Myr

fzf-preview.sh

This file

Path

bin/fzf-preview.sh

86 lines · 2.8 KiB · Shell

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 76efddd point at it.

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 1–86why, one step back
1#!/usr/bin/env bash
2#3# The purpose of this script is to demonstrate how to preview a file or an4# image in the preview window of fzf.5#6# Dependencies:7# - https://github.com/sharkdp/bat8# - https://github.com/hpjansson/chafa9# - https://iterm2.com/utilities/imgcat1011if [[ $# -ne 1 ]]; then12 >&2 echo "usage: $0 FILENAME[:LINENO][:IGNORED]"13 exit 114fi1516file=${1/#\~\//$HOME/}1718center=019if [[ ! -r $file ]]; then20 if [[ $file =~ ^(.+):([0-9]+)\ *$ ]] && [[ -r ${BASH_REMATCH[1]} ]]; then21 file=${BASH_REMATCH[1]}22 center=${BASH_REMATCH[2]}23 elif [[ $file =~ ^(.+):([0-9]+):[0-9]+\ *$ ]] && [[ -r ${BASH_REMATCH[1]} ]]; then24 file=${BASH_REMATCH[1]}25 center=${BASH_REMATCH[2]}26 fi27fi2829type=$(file --brief --dereference --mime -- "$file")3031if [[ ! $type =~ image/ ]]; then32 if [[ $type =~ =binary ]]; then33 file "$1"34 exit35 fi3637 # Sometimes bat is installed as batcat.38 if command -v batcat > /dev/null; then39 batname="batcat"40 elif command -v bat > /dev/null; then41 batname="bat"42 else43 cat "$1"44 exit45 fi4647 ${batname} --style="${BAT_STYLE:-numbers}" --color=always --pager=never --highlight-line="${center:-0}" -- "$file"48 exit49fi5051dim=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}52if [[ $dim == x ]]; then53 dim=$(stty size < /dev/tty | awk '{print $2 "x" $1}')54elif ! [[ $KITTY_WINDOW_ID ]] && ((FZF_PREVIEW_TOP + FZF_PREVIEW_LINES == $(stty size < /dev/tty | awk '{print $1}'))); then55 # Avoid scrolling issue when the Sixel image touches the bottom of the screen56 # * https://github.com/junegunn/fzf/issues/254457 dim=${FZF_PREVIEW_COLUMNS}x$((FZF_PREVIEW_LINES - 1))58fi5960# 1. Use icat (from Kitty) if kitten is installed61if [[ $KITTY_WINDOW_ID ]] || [[ $GHOSTTY_RESOURCES_DIR ]] && command -v kitten > /dev/null; then62 # 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/'6970# 2. Use chafa with Sixel output71elif command -v chafa > /dev/null; then72 chafa -s "$dim" "$file"73 # Add a new line character so that fzf can display multiple images in the preview window74 echo7576# 3. If chafa is not found but imgcat is available, use it on iTerm277elif command -v imgcat > /dev/null; then78 # NOTE: We should use https://iterm2.com/utilities/it2check to check if the79 # user is running iTerm2. But for the sake of simplicity, we just assume80 # that's the case here.81 imgcat -W "${dim%%x*}" -H "${dim##*x}" "$file"8283# 4. Cannot find any suitable method to preview the image84else85 file "$file"86fi