Skip to content

Commit

Permalink
fix(path.shorten): do not shorten drive spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Jan 15, 2024
1 parent b909f52 commit a22cfce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lua/fzf-lua/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ function M.shorten(path, max_len)
local parts = {}
local start_idx = 1
max_len = max_len and tonumber(max_len) > 0 and max_len or 1
if utils.__IS_WINDOWS and M.is_absolute(path) then
-- do not shorten "C:\" to "C", for glob to succeed
-- we need the paths to start with a valid drive spec
table.insert(parts, path:sub(1, 2))
start_idx = 4
end
repeat
local i = find_next_separator(path, start_idx)
local end_idx = i and start_idx + math.min(i - start_idx, max_len) - 1 or nil
Expand Down
12 changes: 7 additions & 5 deletions tests/path_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,13 @@ describe("Testing path module", function()
assert.are.equal(path.shorten([[/foo/bar]]), [[\f\bar]])
assert.are.equal(path.shorten([[/foo/bar\baz]]), [[\f\b\baz]])
assert.are.equal(path.shorten([[\]]), [[\]])
assert.are.equal(path.shorten([[c:\foo]]), [[c\foo]])
assert.are.equal(path.shorten([[c:\foo\bar]]), [[c\f\bar]])
assert.are.equal(path.shorten([[c:\foo\bar\baz]]), [[c\f\b\baz]])
assert.are.equal(path.shorten([[c:\.foo\.bar\baz]]), [[c\.f\.b\baz]])
assert.are.equal(path.shorten([[c:/foo\bar]]), [[c/f/bar]])
assert.are.equal(path.shorten([[c:/]]), [[c:/]])
assert.are.equal(path.shorten([[c:\]]), [[c:\]])
assert.are.equal(path.shorten([[c:\foo]]), [[c:\foo]])
assert.are.equal(path.shorten([[c:\foo\bar]]), [[c:\f\bar]])
assert.are.equal(path.shorten([[c:\foo\bar\baz]]), [[c:\f\b\baz]])
assert.are.equal(path.shorten([[c:\.foo\.bar\baz]]), [[c:\.f\.b\baz]])
assert.are.equal(path.shorten([[c:/foo\bar]]), [[c:/f/bar]])
assert.are.equal(path.shorten([[~/foo/bar]]), [[~/f/bar]])
assert.are.equal(path.shorten([[~\foo\bar]]), [[~\f\bar]])
assert.are.equal(path.shorten([[~\foo/bar]]), [[~\f\bar]])
Expand Down

0 comments on commit a22cfce

Please sign in to comment.