From fa90b74e31190e2da55db71d18e9f7c4b3022cdb Mon Sep 17 00:00:00 2001 From: bhagwan Date: Sat, 28 May 2022 12:26:10 -0700 Subject: [PATCH] wrapper around `fn.input` to prevent exception with `` --- lua/fzf-lua/actions.lua | 6 +++--- lua/fzf-lua/providers/grep.lua | 2 +- lua/fzf-lua/providers/tags.lua | 2 +- lua/fzf-lua/utils.lua | 7 +++++++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lua/fzf-lua/actions.lua b/lua/fzf-lua/actions.lua index 87a210a..2881ec0 100644 --- a/lua/fzf-lua/actions.lua +++ b/lua/fzf-lua/actions.lua @@ -460,7 +460,7 @@ M.git_checkout = function(selected, opts) local cmd_checkout = path.git_cwd({"git", "checkout"}, opts) local cmd_cur_commit = path.git_cwd({"git", "rev-parse", "--short HEAD"}, opts) local commit_hash = selected[1]:match("[^ ]+") - if vim.fn.input("Checkout commit " .. commit_hash .. "? [y/n] ") == "y" then + if utils.input("Checkout commit " .. commit_hash .. "? [y/n] ") == "y" then local current_commit = utils.io_systemlist(cmd_cur_commit) if(commit_hash == current_commit) then return end table.insert(cmd_checkout, commit_hash) @@ -509,7 +509,7 @@ M.git_stash_drop = function(selected, opts) end M.git_stash_pop = function(selected, opts) - if vim.fn.input("Pop " .. #selected .. " stash(es)? [y/n] ") == "y" then + if utils.input("Pop " .. #selected .. " stash(es)? [y/n] ") == "y" then local cmd = path.git_cwd({"git", "stash", "pop"}, opts) git_exec(selected, opts, cmd) vim.cmd("e!") @@ -517,7 +517,7 @@ M.git_stash_pop = function(selected, opts) end M.git_stash_apply = function(selected, opts) - if vim.fn.input("Apply " .. #selected .. " stash(es)? [y/n] ") == "y" then + if utils.input("Apply " .. #selected .. " stash(es)? [y/n] ") == "y" then local cmd = path.git_cwd({"git", "stash", "apply"}, opts) git_exec(selected, opts, cmd) vim.cmd("e!") diff --git a/lua/fzf-lua/providers/grep.lua b/lua/fzf-lua/providers/grep.lua index 83e19d9..d4f8927 100644 --- a/lua/fzf-lua/providers/grep.lua +++ b/lua/fzf-lua/providers/grep.lua @@ -124,7 +124,7 @@ M.grep = function(opts) -- if user did not provide a search term -- provide an input prompt if not opts.search and not opts.raw_cmd then - opts.search = vim.fn.input(opts.input_prompt) or '' + opts.search = utils.input(opts.input_prompt) or '' end -- search query in header line diff --git a/lua/fzf-lua/providers/tags.lua b/lua/fzf-lua/providers/tags.lua index 0beb396..800fd54 100644 --- a/lua/fzf-lua/providers/tags.lua +++ b/lua/fzf-lua/providers/tags.lua @@ -150,7 +150,7 @@ M.grep = function(opts) end if not opts.search then - opts.search = vim.fn.input(opts.input_prompt or 'Grep For> ') + opts.search = utils.input(opts.input_prompt or 'Grep For> ') end return M.tags(opts) diff --git a/lua/fzf-lua/utils.lua b/lua/fzf-lua/utils.lua index 87024ac..6f9c91c 100644 --- a/lua/fzf-lua/utils.lua +++ b/lua/fzf-lua/utils.lua @@ -611,6 +611,13 @@ function M.io_system(cmd, use_lua_io) end end +-- wrapper around |input()| to allow cancellation with `` +-- without "E5108: Error executing lua Keyboard interrupt" +function M.input(prompt, text) + local ok, res = pcall(vim.fn.input, prompt, text or '') + return ok and res or nil +end + function M.fzf_bind_to_neovim(key) local conv_map = { ['alt'] = 'A',