Skip to content
Myr

Experimental support for Kitty image protocol in preview window

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Close #3228 * Works inside and outside of tmux * There is a problem where fzf unnecessarily displays the scroll offset indicator at the topbright of the screen when the image just fits the preview window. This is because `kitty icat` generates an extra line after the image area. # A 5-row images; an extra row at the end confuses fzf ["\e_Ga ... \e[9C􎻮̅̅ࠪ􎻮̅̍ࠪ􎻮̅̎ࠪ􎻮̅̐ࠪ􎻮̅̒ࠪ􎻮̅̽ࠪ􎻮̅̾ࠪ􎻮̅̿ࠪ􎻮̅͆ࠪ􎻮̅͊ࠪ􎻮̅͋ࠪ\n", "\r\e[9C􎻮̍̅ࠪ􎻮̍̍ࠪ􎻮̍̎ࠪ􎻮̍̐ࠪ􎻮̍̒ࠪ􎻮̍̽ࠪ􎻮̍̾ࠪ􎻮̍̿ࠪ􎻮̍͆ࠪ􎻮̍͊ࠪ􎻮̍͋ࠪ\n", "\r\e[9C􎻮̎̅ࠪ􎻮̎̍ࠪ􎻮̎̎ࠪ􎻮̎̐ࠪ􎻮̎̒ࠪ􎻮̎̽ࠪ􎻮̎̾ࠪ􎻮̎̿ࠪ􎻮̎͆ࠪ􎻮̎͊ࠪ􎻮̎͋ࠪ\n", "\r\e[9C􎻮̐̅ࠪ􎻮̐̍ࠪ􎻮̐̎ࠪ􎻮̐̐ࠪ􎻮̐̒ࠪ􎻮̐̽ࠪ􎻮̐̾ࠪ􎻮̐̿ࠪ􎻮̐͆ࠪ􎻮̐͊ࠪ􎻮̐͋ࠪ\n", "\r\e[9C􎻮̒̅ࠪ􎻮̒̍ࠪ􎻮̒̎ࠪ􎻮̒̐ࠪ􎻮̒̒ࠪ􎻮̒̽ࠪ􎻮̒̾ࠪ􎻮̒̿ࠪ􎻮̒͆ࠪ􎻮̒͊ࠪ􎻮̒͋ࠪ\n", "\r\e[39m\e8"] * Example: fzf --preview=' if file --mime-type {} | grep -qF 'image/'; then # --transfer-mode=memory is the fastest option but if you want fzf to be able # to redraw the image on terminal resize or on 'change-preview-window', # you need to use --transfer-mode=stream. kitty icat --clear --transfer-mode=memory --stdin=no --place=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}@0x0 {} else bat --color=always {} fi '

authored by Junegunn Choi · committed by Junegunn Choi · parent 0f15f1a · Browse files · 6 files+41 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+41 added−1 removed

  • other:+41 added−1 removed

6 files+41 added−1 removed

modified:src/tui/dummy.go+1 added
31 unmodified lines
32func (r *FullscreenRenderer) Init() {}33func (r *FullscreenRenderer) Resize(maxHeightFunc func(int) int) {}34func (r *FullscreenRenderer) Pause(bool) {}35func (r *FullscreenRenderer) Resume(bool, bool) {}36func (r *FullscreenRenderer) PassThrough(string) {}37func (r *FullscreenRenderer) Clear() {}38func (r *FullscreenRenderer) NeedScrollbarRedraw() bool { return false }39func (r *FullscreenRenderer) Refresh() {}40func (r *FullscreenRenderer) Close() {}
10 unmodified lines
modified:src/tui/light.go+5 added
29 unmodified lines
3031var offsetRegexp *regexp.Regexp = regexp.MustCompile("(.*)\x1b\\[([0-9]+);([0-9]+)R")32var offsetRegexpBegin *regexp.Regexp = regexp.MustCompile("^\x1b\\[[0-9]+;[0-9]+R")3334func (r *LightRenderer) PassThrough(str string) {35 r.queued.WriteString(str)36 r.flush()37}3839func (r *LightRenderer) stderr(str string) {40 r.stderrInternal(str, true, "")41}42
1059 unmodified lines
modified:src/tui/tcell.go+5 added
96 unmodified lines
97 AttrRegular = Attr(1 << 7)98 AttrClear = Attr(1 << 8)99)100101func (r *FullscreenRenderer) PassThrough(str string) {102 // No-op103 // https://github.com/gdamore/tcell/issues/363#issuecomment-680665073104}105106func (r *FullscreenRenderer) Resize(maxHeightFunc func(int) int) {}107108func (r *FullscreenRenderer) defaultTheme() *ColorTheme {109 if _screen.Colors() >= 256 {
660 unmodified lines
modified:src/tui/tui.go+1 added
472 unmodified lines
473 Clear()474 RefreshWindows(windows []Window)475 Refresh()476 Close()477 PassThrough(string)478 NeedScrollbarRedraw() bool479480 GetChar() Event481
312 unmodified lines
modified:src/terminal.go+16 added-1 removed
49 unmodified lines
50var whiteSuffix *regexp.Regexp51var offsetComponentRegex *regexp.Regexp52var offsetTrimCharsRegex *regexp.Regexp53var activeTempFiles []string54var passThroughRegex *regexp.Regexp5556const clearCode string = "\x1b[2J"5758func init() {
1 unmodified line
60 whiteSuffix = regexp.MustCompile(`\s*$`)61 offsetComponentRegex = regexp.MustCompile(`([+-][0-9]+)|(-?/[1-9][0-9]*)`)62 offsetTrimCharsRegex = regexp.MustCompile(`[^0-9/+-]`)63 activeTempFiles = []string{}6465 // Parts of the preview output that should be passed through to the terminal66 // * https://github.com/tmux/tmux/wiki/FAQ#what-is-the-passthrough-escape-sequence-and-how-do-i-use-it67 // * https://sw.kovidgoyal.net/kitty/graphics-protocol68 passThroughRegex = regexp.MustCompile(`\x1bPtmux;\x1b\x1b.*?[^\x1b]\x1b\\|\x1b_G.*?\x1b\\`)69}7071type jumpMode int72
1890 unmodified lines
1963 var lbg tui.Color = -11964 if ansi != nil {1965 ansi.lbg = -11966 }1961 line = strings.TrimRight(line, "\r\n")19671968 passThroughs := passThroughRegex.FindAllString(line, -1)1969 if passThroughs != nil {1970 line = passThroughRegex.ReplaceAllString(line, "")1971 }1972 line = strings.TrimLeft(strings.TrimRight(line, "\r\n"), "\r")19731974 if lineNo >= height || t.pwindow.Y() == height-1 && t.pwindow.X() > 0 {1975 t.previewed.filled = true1976 t.previewer.scrollable = true1977 break
4 unmodified lines
1982 x := t.pwindow.X()1983 t.renderPreviewSpinner()1984 t.pwindow.Move(y, x)1985 }1986 for _, passThrough := range passThroughs {1987 t.tui.PassThrough(passThrough)1988 }1989 var fillRet tui.FillReturn1990 prefixWidth := 01991 _, _, ansi = extractColor(line, ansi, func(str string, ansi *ansiState) bool {1992 trimmed := []rune(str)
1862 unmodified lines
modified:CHANGELOG.md+13 added
1 unmodified line
2=========340.43.05------6- Experimental, partial support for Kitty image protocol in the preview window7 ```sh8 fzf --preview='9 if file --mime-type {} | grep -qF 'image/'; then10 # --transfer-mode=memory is the fastest option but if you want fzf to be able11 # to redraw the image on terminal resize or on 'change-preview-window',12 # you need to use --transfer-mode=stream.13 kitty icat --clear --transfer-mode=memory --stdin=no --place=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}@0x0 {}14 else15 bat --color=always {}16 fi17 '18 ```19- `--listen` server can report program state in JSON format (`GET /`)20 ```sh21 # fzf server started in "headless" mode22 fzf --listen 6266 2> /dev/null
1691 unmodified lines