2022-07-10 20:07:33 +00:00
|
|
|
-- The prefix that will be before the search text for the user
|
|
|
|
|
local prompt_prefix = ">> "
|
|
|
|
|
|
2023-01-06 20:09:44 +00:00
|
|
|
-- Gets the suffix to delete from some text biased on what happens in a bash
|
|
|
|
|
-- prompt. If the text dose not end in a letter then the last word and all of
|
|
|
|
|
-- the tailing special characters will be returned. If the text dose end in a
|
|
|
|
|
-- letter then only the last word will be returned leaving the special
|
|
|
|
|
-- characters that are before the last word. For example
|
|
|
|
|
--
|
|
|
|
|
-- `some word` -> `some `
|
|
|
|
|
-- `some word` -> `some `
|
|
|
|
|
-- `some word ` -> `some `
|
|
|
|
|
local function get_delete_suffix(text)
|
|
|
|
|
if text:match "([A-Za-z]+)$" == nil then
|
|
|
|
|
return text:match "([A-Za-z]+[^A-Za-z]+)$"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return text:match "([A-Za-z]+)$"
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-10 20:07:33 +00:00
|
|
|
local prompt = {}
|
|
|
|
|
|
2022-07-24 11:50:30 +00:00
|
|
|
prompt.suffix = ""
|
2022-07-10 20:07:33 +00:00
|
|
|
prompt.value = ""
|
|
|
|
|
|
|
|
|
|
prompt.text = function()
|
2022-07-24 11:50:30 +00:00
|
|
|
return prompt.value .. prompt.suffix
|
2022-07-10 20:07:33 +00:00
|
|
|
end
|
2023-01-06 20:09:44 +00:00
|
|
|
|
2022-07-10 20:07:33 +00:00
|
|
|
prompt.update = function()
|
2022-07-24 11:50:30 +00:00
|
|
|
vim.api.nvim_echo({
|
|
|
|
|
{ prompt_prefix, "None" },
|
|
|
|
|
{ prompt.value:sub(1, -2), "None" },
|
|
|
|
|
{ prompt.value:sub(-1, -1), "Underlined" },
|
|
|
|
|
{ prompt.suffix, "None" },
|
|
|
|
|
}, false, {})
|
2022-07-10 20:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
prompt.input = function(char)
|
|
|
|
|
if char == "BACKSPACE" then
|
|
|
|
|
prompt.value = string.sub(prompt.value, 0, -2)
|
2022-07-24 11:50:30 +00:00
|
|
|
elseif char == "LEFT" then
|
|
|
|
|
if #prompt.value > 0 then
|
|
|
|
|
prompt.suffix = prompt.value:sub(-1, -1) .. prompt.suffix
|
|
|
|
|
prompt.value = prompt.value:sub(1, -2)
|
|
|
|
|
end
|
|
|
|
|
elseif char == "RIGHT" then
|
|
|
|
|
if #prompt.suffix > 0 then
|
|
|
|
|
prompt.value = prompt.value .. prompt.suffix:sub(1, 1)
|
|
|
|
|
prompt.suffix = prompt.suffix:sub(2, -1)
|
|
|
|
|
end
|
|
|
|
|
elseif char == "DELETE_WORD" then
|
2023-01-06 20:09:44 +00:00
|
|
|
local suffix = get_delete_suffix(prompt.value)
|
|
|
|
|
|
|
|
|
|
if suffix == nil then
|
2022-07-24 11:50:30 +00:00
|
|
|
prompt.value = ""
|
2023-01-06 20:09:44 +00:00
|
|
|
else
|
|
|
|
|
prompt.value = prompt.value:sub(1, #prompt.value - #suffix)
|
2022-07-24 11:50:30 +00:00
|
|
|
end
|
2022-07-10 20:07:33 +00:00
|
|
|
elseif char == "\\\\" then
|
|
|
|
|
prompt.value = prompt.value .. "\\"
|
|
|
|
|
else
|
|
|
|
|
prompt.value = prompt.value .. char
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
prompt.update()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
prompt.set = function(value)
|
|
|
|
|
prompt.value = value
|
2022-07-24 11:50:30 +00:00
|
|
|
prompt.suffix = ""
|
2022-07-10 20:07:33 +00:00
|
|
|
prompt.update()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
prompt.destroy = function()
|
|
|
|
|
prompt.value = ""
|
2022-07-24 11:50:30 +00:00
|
|
|
prompt.suffix = ""
|
2022-07-10 20:07:33 +00:00
|
|
|
vim.notify ""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return prompt
|