From d8188fce7b7bea982e7f9050c35e488e49fb8fd0 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 7 Oct 2023 18:20:27 +0900 Subject: Experimental support for Kitty image protocol in preview window MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ' --- src/tui/dummy.go | 1 + src/tui/light.go | 5 +++++ src/tui/tcell.go | 5 +++++ src/tui/tui.go | 1 + 4 files changed, 12 insertions(+) (limited to 'src/tui') diff --git a/src/tui/dummy.go b/src/tui/dummy.go index 7a02a8af..352e2b09 100644 --- a/src/tui/dummy.go +++ b/src/tui/dummy.go @@ -33,6 +33,7 @@ func (r *FullscreenRenderer) Init() {} func (r *FullscreenRenderer) Resize(maxHeightFunc func(int) int) {} func (r *FullscreenRenderer) Pause(bool) {} func (r *FullscreenRenderer) Resume(bool, bool) {} +func (r *FullscreenRenderer) PassThrough(string) {} func (r *FullscreenRenderer) Clear() {} func (r *FullscreenRenderer) NeedScrollbarRedraw() bool { return false } func (r *FullscreenRenderer) Refresh() {} diff --git a/src/tui/light.go b/src/tui/light.go index cff59c90..cc828fa9 100644 --- a/src/tui/light.go +++ b/src/tui/light.go @@ -31,6 +31,11 @@ const consoleDevice string = "/dev/tty" var offsetRegexp *regexp.Regexp = regexp.MustCompile("(.*)\x1b\\[([0-9]+);([0-9]+)R") var offsetRegexpBegin *regexp.Regexp = regexp.MustCompile("^\x1b\\[[0-9]+;[0-9]+R") +func (r *LightRenderer) PassThrough(str string) { + r.queued.WriteString(str) + r.flush() +} + func (r *LightRenderer) stderr(str string) { r.stderrInternal(str, true, "") } diff --git a/src/tui/tcell.go b/src/tui/tcell.go index 8f6806d4..0c3d4694 100644 --- a/src/tui/tcell.go +++ b/src/tui/tcell.go @@ -98,6 +98,11 @@ const ( AttrClear = Attr(1 << 8) ) +func (r *FullscreenRenderer) PassThrough(str string) { + // No-op + // https://github.com/gdamore/tcell/issues/363#issuecomment-680665073 +} + func (r *FullscreenRenderer) Resize(maxHeightFunc func(int) int) {} func (r *FullscreenRenderer) defaultTheme() *ColorTheme { diff --git a/src/tui/tui.go b/src/tui/tui.go index 9a88c455..4039565a 100644 --- a/src/tui/tui.go +++ b/src/tui/tui.go @@ -474,6 +474,7 @@ type Renderer interface { RefreshWindows(windows []Window) Refresh() Close() + PassThrough(string) NeedScrollbarRedraw() bool GetChar() Event -- cgit v1.2.3