sixel: Export $FZF_PREVIEW_TOP to the preview command (#2544)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
So that it can determine if it should subtract 1 from $FZF_PREVIEW_LINES to avoid scrolling issue of Sixel image that touches the bottom of the screen.
authored by Junegunn Choi · committed by Junegunn Choi · parent a0145ce · Browse files · 7 files·+35 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+35 added−10 removed
- other:+35 added−10 removed
Provenance
- Commit
21ab64e- Tree
3d1ebb4- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
a0145ce
7 files changed+35 added−10 removed
modified:bin/fzf-preview.sh+20 added-7 removed
11 unmodified lines121314151616171819202122181920212223242526273 unmodified lines313233343536373839404142434445463547484950515253545556575859606111 unmodified lines exit 1fi
file=${1/#\~\//$HOME/}type=$(file --mime-type "$file")type=$(file --dereference --mime -- "$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 if [[ $type =~ =binary ]]; then file "$1" exit fi
# Sometimes bat is installed as batcat. if command -v batcat > /dev/null; then batname="batcat" elif command -v bat > /dev/null; then3 unmodified lines exit fi
${batname} --style="${BAT_STYLE:-numbers}" --color=always --pager=never -- "$file" exitfi
dim=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}if [[ $dim = x ]]; then dim=$(stty size < /dev/tty | awk '{print $2 "x" $1}')elif ! [[ $KITTY_WINDOW_ID ]] && (( FZF_PREVIEW_TOP + FZF_PREVIEW_LINES == $(stty size < /dev/tty | awk '{print $1}') )); then # Avoid scrolling issue when the Sixel image touches the bottom of the screen # * https://github.com/junegunn/fzf/issues/2544 dim=${FZF_PREVIEW_COLUMNS}x$((FZF_PREVIEW_LINES - 1))fi
elif [[ $KITTY_WINDOW_ID ]]; thenif [[ $KITTY_WINDOW_ID ]]; then # 1. 'memory' is the fastest option but if you want the image to be scrollable, # you have to use 'stream'. # # 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="$dim@0x0" "$file" | sed '$d' | sed $'$s/$/\e[m/'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 echoelse file "$file"fimodified:man/man1/fzf.1+3 added
546 unmodified lines547 \fB$LINES\fR and \fB$COLUMNS\fR with the same values but they can be reset548 by the default shell, so prefer to refer to the ones with \fBFZF_PREVIEW_\fR549 prefix.)550 551+fzf also exports \fB$FZF_PREVIEW_TOP\fR and \fB$FZF_PREVIEW_LEFT\fR so that552+the preview command can determine the position of the preview window.553+554 A placeholder expression starting with \fB+\fR flag will be replaced to the555 space-separated list of the selected lines (or the current line if no selection556 was made) individually quoted.557 802 unmodified linesmodified:src/tui/dummy.go+1 added
39 unmodified lines40 func (r *FullscreenRenderer) Close() {}41 func (r *FullscreenRenderer) Size() TermSize { return TermSize{} }42 43 func (r *FullscreenRenderer) GetChar() Event { return Event{} }44+func (r *FullscreenRenderer) Top() int { return 0 }45 func (r *FullscreenRenderer) MaxX() int { return 0 }46 func (r *FullscreenRenderer) MaxY() int { return 0 }47 48 func (r *FullscreenRenderer) RefreshWindows(windows []Window) {}4 unmodified linesmodified:src/tui/light.go+4 added
718 unmodified lines719 r.closePlatform()720 r.restoreTerminal()721 }722 723+func (r *LightRenderer) Top() int {724+ return r.yoffset725+}726+727 func (r *LightRenderer) MaxX() int {728 return r.width729 }730 381 unmodified linesmodified:src/tui/tcell.go+4 added
170 unmodified lines171 r.initScreen()172 initTheme(r.theme, r.defaultTheme(), r.forceBlack)173 }174 175+func (r *FullscreenRenderer) Top() int {176+ return 0177+}178+179 func (r *FullscreenRenderer) MaxX() int {180 ncols, _ := _screen.Size()181 return int(ncols)182 }602 unmodified linesmodified:src/tui/tui.go+1 added
492 unmodified lines493 NeedScrollbarRedraw() bool494 495 GetChar() Event496 497+ Top() int498 MaxX() int499 MaxY() int500 501 Size() TermSize311 unmodified linesmodified:src/terminal.go+2 added-3 removed
2065 unmodified lines2066 t.tui.PassThrough(passThrough)2067 2068 if requiredLines > 0 {2069 if y+requiredLines == height {2070- if t.tui.MaxY() == t.pwindow.Top()+height {2071- t.tui.PassThrough("\x1b[1T")2072- }2070 t.pwindow.Move(height-1, maxWidth-1)2071 t.previewed.filled = true2072 break Loop2073 } else {712 unmodified lines2786 env = append(env, lines)2787 env = append(env, "FZF_PREVIEW_"+lines)2788 env = append(env, columns)2789 env = append(env, "FZF_PREVIEW_"+columns)2790+ env = append(env, fmt.Sprintf("FZF_PREVIEW_TOP=%d", t.tui.Top()+t.pwindow.Top()))2791+ env = append(env, fmt.Sprintf("FZF_PREVIEW_LEFT=%d", t.pwindow.Left()))2792 }2793 cmd.Env = env2794 2795 out, _ := cmd.StdoutPipe()1210 unmodified lines