Skip to content

Commit 70d9c51

Browse files
committed
Feat: add --fzf-command option to override FZF_DEFAULT_COMMAND
1 parent 4e0acf8 commit 70d9c51

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

yazi-plugin/preset/plugins/fzf.lua

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ local state = ya.sync(function()
88
return cx.active.current.cwd, selected
99
end)
1010

11-
function M:entry()
11+
function M:entry(job)
1212
ya.emit("escape", { visual = true })
1313

1414
local cwd, selected = state()
1515
if cwd.scheme.is_virtual then
1616
return ya.notify { title = "Fzf", content = "Not supported under virtual filesystems", timeout = 5, level = "warn" }
1717
end
1818

19+
local default_cmd = M.parse_args(job and job.args or {})
20+
if default_cmd == "" then
21+
return ya.notify { title = "Fzf", content = "Missing string after --fzf-command", timeout = 5, level = "error" }
22+
end
1923
local permit = ui.hide()
20-
local output, err = M.run_with(cwd, selected)
24+
local output, err = M.run_with(cwd, selected, default_cmd)
2125

2226
permit:drop()
2327
if not output then
@@ -37,22 +41,33 @@ end
3741
---@param cwd Url
3842
---@param selected Url[]
3943
---@return string?, Error?
40-
function M.run_with(cwd, selected)
41-
local child, err = Command("fzf")
42-
:arg("-m")
44+
function M.run_with(cwd, selected, default_cmd)
45+
local input = nil
46+
local source = "stdin"
47+
if #selected > 0 then
48+
source = "selection"
49+
input = ""
50+
for _, u in ipairs(selected) do
51+
input = input .. string.format("%s\n", u)
52+
end
53+
end
54+
55+
local cmd = Command("fzf"):arg("-m")
56+
if default_cmd and #selected == 0 then
57+
cmd:env("FZF_DEFAULT_COMMAND", default_cmd)
58+
end
59+
local child, err = cmd
4360
:cwd(tostring(cwd))
44-
:stdin(#selected > 0 and Command.PIPED or Command.INHERIT)
61+
:stdin(input and Command.PIPED or Command.INHERIT)
4562
:stdout(Command.PIPED)
4663
:spawn()
4764

4865
if not child then
4966
return nil, Err("Failed to start `fzf`, error: %s", err)
5067
end
5168

52-
for _, u in ipairs(selected) do
53-
child:write_all(string.format("%s\n", u))
54-
end
55-
if #selected > 0 then
69+
if input then
70+
child:write_all(input)
5671
child:flush()
5772
end
5873

@@ -65,6 +80,23 @@ function M.run_with(cwd, selected)
6580
return output.stdout, nil
6681
end
6782

83+
function M.parse_args(args)
84+
if not args then
85+
return nil
86+
end
87+
88+
local v = args.fzf_command or args["fzf-command"]
89+
if type(v) == "string" then
90+
return v
91+
end
92+
93+
if v ~= nil then
94+
return ""
95+
end
96+
97+
return nil
98+
end
99+
68100
function M.split_urls(cwd, output)
69101
local t = {}
70102
for line in output:gmatch("[^\r\n]+") do

0 commit comments

Comments
 (0)