From ee751aaddc938ee617c2a721bf6bd36e8fea1697 Mon Sep 17 00:00:00 2001 From: Andrea Alberti Date: Thu, 22 Jan 2026 23:55:02 +0100 Subject: [PATCH] Feat: add `--fzf-command` option to override FZF_DEFAULT_COMMAND --- yazi-plugin/preset/plugins/fzf.lua | 55 +++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/yazi-plugin/preset/plugins/fzf.lua b/yazi-plugin/preset/plugins/fzf.lua index 6842d26a0..cd349fc49 100644 --- a/yazi-plugin/preset/plugins/fzf.lua +++ b/yazi-plugin/preset/plugins/fzf.lua @@ -8,7 +8,7 @@ local state = ya.sync(function() return cx.active.current.cwd, selected end) -function M:entry() +function M:entry(job) ya.emit("escape", { visual = true }) local cwd, selected = state() @@ -16,8 +16,12 @@ function M:entry() return ya.notify { title = "Fzf", content = "Not supported under virtual filesystems", timeout = 5, level = "warn" } end + local default_cmd = M.parse_args(job and job.args or {}) + if default_cmd == "" then + return ya.notify { title = "Fzf", content = "Missing string after --fzf-command", timeout = 5, level = "error" } + end local permit = ui.hide() - local output, err = M.run_with(cwd, selected) + local output, err = M.run_with(cwd, selected, default_cmd) permit:drop() if not output then @@ -37,22 +41,30 @@ end ---@param cwd Url ---@param selected Url[] ---@return string?, Error? -function M.run_with(cwd, selected) - local child, err = Command("fzf") - :arg("-m") - :cwd(tostring(cwd)) - :stdin(#selected > 0 and Command.PIPED or Command.INHERIT) - :stdout(Command.PIPED) - :spawn() +function M.run_with(cwd, selected, default_cmd) + local input = nil + local source = "stdin" + if #selected > 0 then + source = "selection" + input = "" + for _, u in ipairs(selected) do + input = input .. string.format("%s\n", u) + end + end + + local cmd = Command("fzf"):arg("-m") + if default_cmd and #selected == 0 then + cmd:env("FZF_DEFAULT_COMMAND", default_cmd) + end + local child, err = + cmd:cwd(tostring(cwd)):stdin(input and Command.PIPED or Command.INHERIT):stdout(Command.PIPED):spawn() if not child then return nil, Err("Failed to start `fzf`, error: %s", err) end - for _, u in ipairs(selected) do - child:write_all(string.format("%s\n", u)) - end - if #selected > 0 then + if input then + child:write_all(input) child:flush() end @@ -65,6 +77,23 @@ function M.run_with(cwd, selected) return output.stdout, nil end +function M.parse_args(args) + if not args then + return nil + end + + local v = args.fzf_command or args["fzf-command"] + if type(v) == "string" then + return v + end + + if v ~= nil then + return "" + end + + return nil +end + function M.split_urls(cwd, output) local t = {} for line in output:gmatch("[^\r\n]+") do