Skip to content

Commit

Permalink
Modified: escape path string.
Browse files Browse the repository at this point in the history
  • Loading branch information
obaland committed Mar 7, 2022
1 parent 3c72fba commit 5321481
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
12 changes: 0 additions & 12 deletions lua/vfiler/libs/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,6 @@ end
---@param s string
M.string.replace_keycode = vim.fn['vfiler#core#replace_keycode']

if M.is_windows then
function M.string.shellescape(str)
-- convert path separator
str = str:gsub('/', '\\')
return ('"%s"'):format(vim.fn.trim(vim.fn.escape(str, '/'), '/\\'))
end
else
function M.string.shellescape(str)
return vim.fn.shellescape(str)
end
end

function M.string.split(str, pattern)
return vim.list.from(vim.fn.split(str, pattern))
end
Expand Down
22 changes: 14 additions & 8 deletions lua/vfiler/libs/filesystem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,25 @@ else
copy_directory_format = 'cp -fR %s %s'
end

local function escape(path)
if core.is_windows then
-- trim end
path = path:gsub('[/\\]+$', '')
-- convert path separator
path = path:gsub('/', '\\')
return ('"%s"'):format(vim.fn.escape(path, '/'))
else
return vim.fn.shellescape(path)
end
end

function M.copy_directory(src, dest)
local command = copy_directory_format:format(
core.string.shellescape(src),
core.string.shellescape(dest)
)
local command = copy_directory_format:format(escape(src), escape(dest))
vim.fn.system(command)
end

function M.copy_file(src, dest)
local command = copy_file_format:format(
core.string.shellescape(src),
core.string.shellescape(dest)
)
local command = copy_file_format:format(escape(src), escape(dest))
vim.fn.system(command)
end

Expand Down
16 changes: 0 additions & 16 deletions tests/vfiler/libs/core_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,6 @@ describe('core.string', function()
eq('あいうえおかきくけ..しすせそたちつてと', actual)
end)
end)

describe('shellescape', function()
if core.is_windows then
local paths = {
{ input = 'C:/Users\\home/foo\\', excepted = '"C:\\Users\\home\\foo"', },
{ input = 'C:/Users\\home/foo\\ba r', excepted = '"C:\\Users\\home\\foo\\ba r"', },
}
for _, path in ipairs(paths) do
it(path.input, function()
eq(path.excepted, core.string.shellescape(path.input))
end)
end
else
-- TODO:
end
end)
end)

describe('core.path', function()
Expand Down

0 comments on commit 5321481

Please sign in to comment.