mirror of
https://github.com/folke/snacks.nvim
synced 2025-12-23 08:47:57 +00:00
feat(picker.util): cmdline parser used to properly parse diff args
This commit is contained in:
parent
c970bdd030
commit
502598953f
1 changed files with 49 additions and 0 deletions
|
|
@ -646,4 +646,53 @@ function M.globber(globs)
|
|||
end
|
||||
end
|
||||
|
||||
---@param cmdline string
|
||||
---@return string[]
|
||||
function M.parse_cmdline(cmdline)
|
||||
local args = {} ---@type string[]
|
||||
local current = ""
|
||||
local in_quote = false
|
||||
local quote_char = nil
|
||||
local i = 1
|
||||
|
||||
while i <= #cmdline do
|
||||
local char = cmdline:sub(i, i)
|
||||
local next_char = cmdline:sub(i + 1, i + 1)
|
||||
|
||||
if char == "\\" and next_char ~= "" then
|
||||
-- Escaped character - take next char literally
|
||||
current = current .. next_char
|
||||
i = i + 2
|
||||
elseif not in_quote and (char == '"' or char == "'") then
|
||||
-- Start quote
|
||||
in_quote = true
|
||||
quote_char = char
|
||||
i = i + 1
|
||||
elseif in_quote and char == quote_char then
|
||||
-- End quote
|
||||
in_quote = false
|
||||
quote_char = nil
|
||||
i = i + 1
|
||||
elseif not in_quote and char:match("%s") then
|
||||
-- Whitespace outside quotes - split arg
|
||||
if current ~= "" then
|
||||
args[#args + 1] = current
|
||||
current = ""
|
||||
end
|
||||
i = i + 1
|
||||
else
|
||||
-- Regular character or whitespace inside quotes
|
||||
current = current .. char
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
|
||||
-- Add last arg if any
|
||||
if current ~= "" then
|
||||
args[#args + 1] = current
|
||||
end
|
||||
|
||||
return args
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue