feat(picker.util): utility function to get all bins on the PATH

This commit is contained in:
Folke Lemaitre 2025-02-11 12:13:44 +01:00
parent 37f6665c48
commit 5d42c7e5e4
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -531,4 +531,30 @@ function M.modeline(buf)
end
end
--- Gets the list of binaries in the PATH.
--- This won't check if the binary is executable.
--- On Windows, additional extensions are checked.
function M.get_bins()
local is_win = jit.os:find("Windows")
local path = vim.split(os.getenv("PATH") or "", is_win and ";" or ":", { plain = true })
local bins = {} ---@type table<string, string>
for _, p in ipairs(path) do
p = vim.fs.normalize(p)
for file, t in vim.fs.dir(p) do
if t ~= "directory" then
local fpath = p .. "/" .. file
local base, ext = file:match("^(.*)%.(%a+)$")
if is_win then
if base and ext and vim.tbl_contains({ "exe", "bat", "com", "cmd" }, ext) then
bins[base] = bins[base] or fpath
end
else
bins[file] = bins[file] or fpath
end
end
end
end
return bins
end
return M