Skip to content

Commit d8188fc

Browse files
committed
Experimental support for Kitty image protocol in preview window
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 '
1 parent 0f15f1a commit d8188fc

6 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ CHANGELOG
33

44
0.43.0
55
------
6+
- Experimental, partial support for Kitty image protocol in the preview window
7+
```sh
8+
fzf --preview='
9+
if file --mime-type {} | grep -qF 'image/'; then
10+
# --transfer-mode=memory is the fastest option but if you want fzf to be able
11+
# 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+
else
15+
bat --color=always {}
16+
fi
17+
'
18+
```
619
- `--listen` server can report program state in JSON format (`GET /`)
720
```sh
821
# fzf server started in "headless" mode

‎src/terminal.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var whiteSuffix *regexp.Regexp
5151
var offsetComponentRegex *regexp.Regexp
5252
var offsetTrimCharsRegex *regexp.Regexp
5353
var activeTempFiles []string
54+
var passThroughRegex *regexp.Regexp
5455

5556
const clearCode string = "\x1b[2J"
5657

@@ -60,6 +61,11 @@ func init() {
6061
offsetComponentRegex = regexp.MustCompile(`([+-][0-9]+)|(-?/[1-9][0-9]*)`)
6162
offsetTrimCharsRegex = regexp.MustCompile(`[^0-9/+-]`)
6263
activeTempFiles = []string{}
64+
65+
// Parts of the preview output that should be passed through to the terminal
66+
// * https://github.com/tmux/tmux/wiki/FAQ#what-is-the-passthrough-escape-sequence-and-how-do-i-use-it
67+
// * https://sw.kovidgoyal.net/kitty/graphics-protocol
68+
passThroughRegex = regexp.MustCompile(`\x1bPtmux;\x1b\x1b.*?[^\x1b]\x1b\\|\x1b_G.*?\x1b\\`)
6369
}
6470

6571
type jumpMode int
@@ -1958,7 +1964,13 @@ func (t *Terminal) renderPreviewText(height int, lines []string, lineNo int, unc
19581964
if ansi != nil {
19591965
ansi.lbg = -1
19601966
}
1961-
line = strings.TrimRight(line, "\r\n")
1967+
1968+
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")
1973+
19621974
if lineNo >= height || t.pwindow.Y() == height-1 && t.pwindow.X() > 0 {
19631975
t.previewed.filled = true
19641976
t.previewer.scrollable = true
@@ -1971,6 +1983,9 @@ func (t *Terminal) renderPreviewText(height int, lines []string, lineNo int, unc
19711983
t.renderPreviewSpinner()
19721984
t.pwindow.Move(y, x)
19731985
}
1986+
for _, passThrough := range passThroughs {
1987+
t.tui.PassThrough(passThrough)
1988+
}
19741989
var fillRet tui.FillReturn
19751990
prefixWidth := 0
19761991
_, _, ansi = extractColor(line, ansi, func(str string, ansi *ansiState) bool {

‎src/tui/dummy.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (r *FullscreenRenderer) Init() {}
3333
func (r *FullscreenRenderer) Resize(maxHeightFunc func(int) int) {}
3434
func (r *FullscreenRenderer) Pause(bool) {}
3535
func (r *FullscreenRenderer) Resume(bool, bool) {}
36+
func (r *FullscreenRenderer) PassThrough(string) {}
3637
func (r *FullscreenRenderer) Clear() {}
3738
func (r *FullscreenRenderer) NeedScrollbarRedraw() bool { return false }
3839
func (r *FullscreenRenderer) Refresh() {}

‎src/tui/light.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ const consoleDevice string = "/dev/tty"
3131
var offsetRegexp *regexp.Regexp = regexp.MustCompile("(.*)\x1b\\[([0-9]+);([0-9]+)R")
3232
var offsetRegexpBegin *regexp.Regexp = regexp.MustCompile("^\x1b\\[[0-9]+;[0-9]+R")
3333

34+
func (r *LightRenderer) PassThrough(str string) {
35+
r.queued.WriteString(str)
36+
r.flush()
37+
}
38+
3439
func (r *LightRenderer) stderr(str string) {
3540
r.stderrInternal(str, true, "")
3641
}

‎src/tui/tcell.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ const (
9898
AttrClear = Attr(1 << 8)
9999
)
100100

101+
func (r *FullscreenRenderer) PassThrough(str string) {
102+
// No-op
103+
// https://github.com/gdamore/tcell/issues/363#issuecomment-680665073
104+
}
105+
101106
func (r *FullscreenRenderer) Resize(maxHeightFunc func(int) int) {}
102107

103108
func (r *FullscreenRenderer) defaultTheme() *ColorTheme {

‎src/tui/tui.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ type Renderer interface {
474474
RefreshWindows(windows []Window)
475475
Refresh()
476476
Close()
477+
PassThrough(string)
477478
NeedScrollbarRedraw() bool
478479

479480
GetChar() Event

0 commit comments

Comments
 (0)