Skip to content

Commit

Permalink
feat: added support for listing files with dir /s/b/a:-d
Browse files Browse the repository at this point in the history
fix: path shorten/lengthen
  • Loading branch information
ibhagwan committed Jan 15, 2024
1 parent 76268b1 commit 4e01b21
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
15 changes: 12 additions & 3 deletions lua/fzf-lua/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,17 @@ M.file = function(x, opts)
opts = opts or {}
local ret = {}
local icon, hl
local colon_idx = utils.find_next_char(x, COLON_BYTE) or 0
if utils.__IS_WINDOWS then colon_idx = utils.find_next_char(x, COLON_BYTE, colon_idx) or 0 end
local colon_idx = 0
local is_absolute = path.is_absolute(x)
if utils.__IS_WINDOWS and string.byte(x, #x) == 13 then
-- strip ^M added by the "dir /s/b" command
x = x:sub(1, #x - 1)
end
if utils.__IS_WINDOWS and is_absolute then
colon_idx = utils.find_next_char(x, COLON_BYTE, 3) or 0
else
colon_idx = utils.find_next_char(x, COLON_BYTE) or 0
end
local file_part = colon_idx > 1 and x:sub(1, colon_idx - 1) or x
local rest_of_line = colon_idx > 1 and x:sub(colon_idx) or nil
-- strip ansi coloring from path so we can use filters
Expand All @@ -423,7 +432,7 @@ M.file = function(x, opts)
if opts.cwd and #opts.cwd > 0 then
filepath = path.relative_to(filepath, opts.cwd)
end
if path.is_absolute(filepath) then
if is_absolute then
-- filter for cwd only
if opts.cwd_only then
local cwd = opts.cwd or vim.loop.cwd()
Expand Down
22 changes: 17 additions & 5 deletions lua/fzf-lua/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,23 @@ end
function M.lengthen(path)
-- we use 'glob_escape' to escape \{} (#548)
local separator = M.separator(path)
path = utils.glob_escape(path)
return vim.fn.glob(path:gsub(separator, "%*" .. separator)
-- remove the starting '*/' if any
:gsub("^%*" .. separator, separator)):match("[^\n]+")
or string.format("<glob expand failed for '%s'>", path)
local glob_expr = utils.glob_escape(path)
local glob_expr_prefix = ""
if M.is_absolute(path) then
-- don't prefix with * the leading / on UNIX or C:\ on windows
if utils.__IS_WINDOWS then
glob_expr_prefix = glob_expr:sub(1, 3)
glob_expr = glob_expr:sub(4)
else
glob_expr_prefix = glob_expr:sub(1, 1)
glob_expr = glob_expr:sub(2)
end
end
-- replace separator with wildcard + separator
glob_expr = glob_expr_prefix .. glob_expr:gsub(separator, "%*" .. separator)
return vim.fn.glob(glob_expr):match("[^\n]+")
-- or string.format("<glob expand failed for '%s'>", path)
or string.format("<glob expand failed for '%s'>", glob_expr)
end

local function lastIndexOf(haystack, needle)
Expand Down
4 changes: 3 additions & 1 deletion lua/fzf-lua/previewer/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ end

function Previewer.base:cmdline(_)
local act = shell.raw_action(function(items, _, _)
self:display_entry(items[1])
-- TODO: is this the right place to remove the double blackslash
-- added by fzf's field index expression on windows?
self:display_entry(items[1]:gsub([[\\]], [[\]]))
return ""
end, "{}", self.opts.debug)
return act
Expand Down
6 changes: 6 additions & 0 deletions lua/fzf-lua/providers/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ local get_files_cmd = function(opts)
command = string.format("fd %s", opts.fd_opts)
elseif vim.fn.executable("rg") == 1 then
command = string.format("rg %s", opts.rg_opts)
elseif utils.__IS_WINDOWS then
-- `dir` command returns absolute paths with ^M for EOL
-- `make_entry.file` will strip the ^M
-- set `opts.cwd` for relative path display
command = "dir /s/b/a:-d"
opts.cwd = opts.cwd or vim.loop.cwd()
else
POSIX_find_compat(opts.find_opts)
command = string.format("find -L . %s", opts.find_opts)
Expand Down

0 comments on commit 4e01b21

Please sign in to comment.