Skip to content

Commit

Permalink
Clean up input lib
Browse files Browse the repository at this point in the history
  • Loading branch information
CKolkey committed Nov 15, 2023
1 parent a69162c commit 843afb5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 67 deletions.
81 changes: 19 additions & 62 deletions lua/neogit/lib/input.lua
Original file line number Diff line number Diff line change
@@ -1,56 +1,5 @@
local M = {}

-- selene: allow(global_usage)
if not _G.__NEOGIT then
_G.__NEOGIT = {}
end

-- selene: allow(global_usage)
if not _G.__NEOGIT.completers then
_G.__NEOGIT.completers = {}
end

local function user_input_prompt(prompt, default_value, completion_function)
vim.fn.inputsave()

local status, result = pcall(vim.fn.input, {
prompt = prompt,
default = default_value,
completion = completion_function and ("customlist,v:lua.__NEOGIT.completers." .. completion_function)
or nil,
})

vim.fn.inputrestore()
if not status then
return nil
end
return result
end

local COMPLETER_SEQ = 1
local function make_completion_function(options)
local id = "completer" .. tostring(COMPLETER_SEQ)
COMPLETER_SEQ = COMPLETER_SEQ + 1

-- selene: allow(global_usage)
_G.__NEOGIT.completers[id] = function(arg_lead)
local result = {}
for _, v in ipairs(options) do
if v:lower():find(arg_lead:lower(), nil, true) then
table.insert(result, v)
end
end
return result
end

return id
end

-- selene: allow(global_usage)
local function remove_completion_function(id)
_G.__NEOGIT.completers[id] = nil
end

--- Provides the user with a confirmation
---@param msg string Prompt to use for confirmation
---@param options table|nil
Expand All @@ -76,28 +25,36 @@ function M.get_choice(msg, options)
return options.values[choice]:match("&(.)")
end

function M.get_user_input(prompt, default)
return user_input_prompt(prompt, default)
end

function M.get_secret_user_input(prompt)
---@param prompt string Prompt to use for user input
---@param default any Default value to use
---@param completion string? Completion type to use. See vim docs for :command-complete
---@return string|nil
function M.get_user_input(prompt, default, completion)
vim.fn.inputsave()

local status, result = pcall(vim.fn.inputsecret, prompt)
local status, result = pcall(vim.fn.input, {
prompt = prompt,
default = default,
completion = completion
})

vim.fn.inputrestore()

if not status then
return nil
end

return result
end

function M.get_user_input_with_completion(prompt, options)
local completer_id = make_completion_function(options)
local result = user_input_prompt(prompt, nil, completer_id)
remove_completion_function(completer_id)
function M.get_secret_user_input(prompt)
vim.fn.inputsave()
local status, result = pcall(vim.fn.inputsecret, prompt)
vim.fn.inputrestore()

if not status then
return nil
end

return result
end

Expand Down
5 changes: 0 additions & 5 deletions tests/mocks/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ input.get_user_input = function(_, default)
end
end

input.get_user_input_with_completion = function(_, _)
local value = table.remove(M.values, 1)
return value
end

input.get_confirmation = function(_, _)
return M.confirmed
end
Expand Down

0 comments on commit 843afb5

Please sign in to comment.