Allow `common.path_suggest` to specify a root directory

This will make relative paths start from `root`.
This commit is contained in:
Guldoman 2022-06-01 03:51:29 +02:00
parent 9a428648a9
commit 295e6b7e5a
No known key found for this signature in database
GPG Key ID: EA928C8BDA1A8825
1 changed files with 25 additions and 2 deletions

View File

@ -140,9 +140,25 @@ function common.fuzzy_match_with_recents(haystack, recents, needle)
end end
function common.path_suggest(text) function common.path_suggest(text, root)
if root and root:sub(-1) ~= PATHSEP then
root = root .. PATHSEP
end
local path, name = text:match("^(.-)([^/\\]*)$") local path, name = text:match("^(.-)([^/\\]*)$")
local files = system.list_dir(path == "" and "." or path) or {} -- ignore root if path is absolute
local is_absolute = common.is_absolute_path(text)
if not is_absolute then
if path == "" then
path = root or "."
else
path = (root or "") .. path
end
end
local files = system.list_dir(path) or {}
if path:sub(-1) ~= PATHSEP then
path = path .. PATHSEP
end
local res = {} local res = {}
for _, file in ipairs(files) do for _, file in ipairs(files) do
file = path .. file file = path .. file
@ -151,6 +167,13 @@ function common.path_suggest(text)
if info.type == "dir" then if info.type == "dir" then
file = file .. PATHSEP file = file .. PATHSEP
end end
if root then
-- remove root part from file path
local s, e = file:find(root, nil, true)
if s == 1 then
file = file:sub(e + 1)
end
end
if file:lower():find(text:lower(), nil, true) == 1 then if file:lower():find(text:lower(), nil, true) == 1 then
table.insert(res, file) table.insert(res, file)
end end