Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions yazi-plugin/preset/plugins/fzf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ 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()
if cwd.scheme.is_virtual then
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
Expand All @@ -37,22 +41,33 @@ end
---@param cwd Url
---@param selected Url[]
---@return string?, Error?
function M.run_with(cwd, selected)
local child, err = Command("fzf")
:arg("-m")
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(#selected > 0 and Command.PIPED or Command.INHERIT)
: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

Expand All @@ -65,6 +80,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
Expand Down
Loading