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

Clean up input lib #942

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
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
17 changes: 6 additions & 11 deletions lua/neogit/lib/git/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ M.create = function(directory, sync)
end

M.init_repo = function()
local directory = vim.fn.input {
prompt = "Create repository in: ",
text = "",
cancelreturn = "",
completion = "dir",
}
local directory = input.get_user_input("Create repository in > ", "", "dir")
if directory == "" then
return
end
Expand All @@ -29,10 +24,14 @@ M.init_repo = function()
directory = vim.fn.fnamemodify(directory, ":p")

if vim.fn.isdirectory(directory) == 0 then
notification.error("You entered an invalid directory")
notification.error("Invalid Directory")
return
end

local status = require("neogit.status")
status.cwd_changed = true
vim.cmd.lcd(directory)

if cli.git_is_repository_sync() then
if
not input.get_confirmation(
Expand All @@ -44,10 +43,6 @@ M.init_repo = function()
end
end

local status = require("neogit.status")
status.cwd_changed = true
vim.cmd.lcd(directory)

M.create(directory)

status.refresh(true, "InitRepo")
Expand Down
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
Loading