Skip to content

Commit

Permalink
feat(fzf-tmux): default to using "--tmux"
Browse files Browse the repository at this point in the history
Replaces `fzf_bin = "fzf-tmux"` as "--tmux" is the preferred method
for both fzf and skim.

Fixed getting the correct width from tmux window/pane for accurate
"flex" layout calculation based on % in setting (default to 50%).

This commit also perpares us better for the upcoming "--popup" option
in fzf in order to support zellij popups.

Ref: junegunn/fzf#4145
  • Loading branch information
ibhagwan committed Jan 4, 2025
1 parent b27cb58 commit 55977c1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 32 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,10 @@ fzf_opts = {
-- popup 80% width, 80% height (note `-p` requires tmux > 3.2)
-- and removes the sides margin added by `fzf-tmux` (fzf#3162)
-- for more options run `fzf-tmux --help`
fzf_tmux_opts = { ["-p"] = "80%,80%", ["--margin"] = "0,0" },
-- NOTE: since fzf v0.53 / sk v0.15 it is recommended to use
-- native tmux integration by adding the below to `fzf_opts`
-- fzf_opts = { ["--tmux"] = "center,80%,60%" }
fzf_tmux_opts = { ["-p"] = "80%,80%", ["--margin"] = "0,0" },
```

</details>
Expand Down
48 changes: 21 additions & 27 deletions lua/fzf-lua/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,6 @@ function M.normalize_opts(opts, globals, __resume_key)
end
end

-- prioritize fzf-tmux split pane flags over the
-- popup flag `-p` from fzf-lua defaults (#865)
opts._is_fzf_tmux_popup = true
if type(opts.fzf_tmux_opts) == "table" then
for _, flag in ipairs({ "-u", "-d", "-l", "-r" }) do
if opts.fzf_tmux_opts[flag] then
opts._is_fzf_tmux_popup = false
opts.fzf_tmux_opts["-p"] = nil
end
end
end

-- Merge `winopts` with outputs from `winopts_fn`
local winopts_fn = opts.winopts_fn or M.globals.winopts_fn
if type(winopts_fn) == "function" then
Expand Down Expand Up @@ -648,6 +636,7 @@ function M.normalize_opts(opts, globals, __resume_key)
["--highlight-line"] = true,
}
},
["0.53"] = { fzf_opts = { ["--tmux"] = true } },
["0.52"] = { fzf_opts = { ["--highlight-line"] = true } },
["0.42"] = {
fzf_opts = {
Expand Down Expand Up @@ -720,22 +709,27 @@ function M.normalize_opts(opts, globals, __resume_key)
end

-- Are we using fzf-tmux? if so get available columns
opts._is_fzf_tmux = vim.env.TMUX
-- pre fzf v0.53 uses the fzf-tmux script
and opts.fzf_bin:match("fzf%-tmux$") and 1
-- fzf v0.53 added native tmux integration
or utils.has(opts, "fzf", { 0, 53 }) and opts.fzf_opts["--tmux"] and 2
-- skim v0.15.5 added native tmux integration
or utils.has(opts, "sk", { 0, 15, 5 }) and opts.fzf_opts["--tmux"] and 2
if opts._is_fzf_tmux then
local out = utils.io_system({ "tmux", "display-message", "-p", "#{window_width}" })
opts._tmux_columns = tonumber(out:match("%d+"))
opts.winopts.split = nil
if opts._is_fzf_tmux == 2 then
-- native tmux integration is implemented using tmux popups
opts._is_fzf_tmux_popup = true
opts._is_fzf_tmux = (function()
if not vim.env.TMUX then return end
local is_tmux =
(opts.fzf_bin:match("fzf%-tmux$") or opts.fzf_bin:match("sk%-tmux$")) and 1
-- fzf v0.53 added native tmux integration
or utils.has(opts, "fzf", { 0, 53 }) and opts.fzf_opts["--tmux"] and 2
-- skim v0.15.5 added native tmux integration
or utils.has(opts, "sk", { 0, 15, 5 }) and opts.fzf_opts["--tmux"] and 2
if is_tmux == 1 then
-- backward compat when using the `fzf-tmux` script: prioritize fzf-tmux
-- split pane flags over the popup flag `-p` from fzf-lua defaults (#865)
if type(opts.fzf_tmux_opts) == "table" then
for _, flag in ipairs({ "-u", "-d", "-l", "-r" }) do
if opts.fzf_tmux_opts[flag] then
opts.fzf_tmux_opts["-p"] = nil
end
end
end
end
end
return is_tmux
end)()

-- refresh highlights if background/colorscheme changed (#1092)
if not M.__HLS_STATE
Expand Down
4 changes: 1 addition & 3 deletions lua/fzf-lua/profiles/fzf-tmux.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
return {
desc = "fzf-native run inside a tmux popup",
fzf_bin = "fzf-tmux",
fzf_opts = { ["--border"] = "rounded" },
fzf_tmux_opts = { ["-p"] = "80%,60%" },
fzf_opts = { ["--border"] = "rounded", ["--tmux"] = "center,80%,60%" },
winopts = { preview = { default = "bat" } },
manpages = { previewer = "man_native" },
helptags = { previewer = "help_native" },
Expand Down
38 changes: 37 additions & 1 deletion lua/fzf-lua/win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,49 @@ local strip_borderchars_hl = function(border)
return borderchars
end

function FzfWin:tmux_columns()
local is_popup, is_hsplit, opt_val = (function()
-- Backward compat using "fzf-tmux" script
if self._o._is_fzf_tmux == 1 then
for _, flag in ipairs({ "-l", "-r" }) do
if self._o.fzf_tmux_opts[flag] then
-- left/right split, not a popup, is an hsplit
return false, true, self._o.fzf_tmux_opts[flag]
end
end
for _, flag in ipairs({ "-u", "-d" }) do
if self._o.fzf_tmux_opts[flag] then
-- up/down split, not a popup, not an hsplit
return false, false, self._o.fzf_tmux_opts[flag]
end
end
-- Default is a popup with "-p" or without
return true, false, self._o.fzf_tmux_opts["-p"]
else
return true, false, self._o.fzf_opts["--tmux"]
end
end)()
local out = utils.io_system({
"tmux", "display-message", "-p",
is_popup and "#{window_width}" or "#{pane_width}"
})
local cols = tonumber(out:match("%d+"))
-- Calc the correct width when using tmux popup or left|right splits
-- fzf's defaults to "--tmux" is "center,50%" or "50%" for splits
if is_popup or is_hsplit then
local percent = type(opt_val) == "string" and tonumber(opt_val:match("(%d+)%%")) or 50
cols = math.floor(cols * percent / 100)
end
return cols
end

function FzfWin:columns(no_fullscreen)
assert(self.winopts)
-- When called from `core.preview_window` we need to get the no-fullscreen columns
-- in order to get an accurate alternate layout trigger that will also be consistent
-- when starting with `winopts.fullscreen == true`
local winopts = no_fullscreen and self:normalize_winopts(false) or self.winopts
return self._o._is_fzf_tmux and self._o._is_fzf_tmux_popup and self._o._tmux_columns
return self._o._is_fzf_tmux and self:tmux_columns()
or winopts.split and vim.api.nvim_win_get_width(self.fzf_winid or 0)
or winopts.width
end
Expand Down

0 comments on commit 55977c1

Please sign in to comment.