Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape dash in prompt to prevent using beginning of the prompt as an option #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lua/telescope-live-grep-args/prompt_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,29 @@ end
local non_autoquote_chars = {
["'"] = true,
['"'] = true,
["-"] = true,
-- ["-"] = true, -- idk why dash here listed with the same purpose as the quotes
}

-- Check for a dash at the beginning of the prompt and escape it.
-- We need to escape dash at the beginning of the prompt because such dash will be interpreted as an option.
local function escape_dash_in_prompt(prompt)
first_char = string.sub(prompt, 1, 1)
pos = string.find(prompt, "-")
if pos == 1 or (pos == 2 and non_autoquote_chars[first_char]) then
prompt = prompt:sub(1, pos - 1) .. "\\" .. prompt:sub(pos)
end

return prompt
end

--- Parses prompt shell like and returns a table containing the arguments
--- If autoquote is true (default) and promt does not start with ', " or - then { prompt } will be returned.
M.parse = function(prompt, autoquote)
if string.len(prompt) == 0 then
return {}
end

prompt = escape_dash_in_prompt(prompt)
autoquote = autoquote or autoquote == nil
local first_char = string.sub(prompt, 1, 1)
if autoquote and non_autoquote_chars[first_char] == nil then
Expand Down