Skip to content

Commit 518ab7a

Browse files
phanenibhagwan
authored andcommitted
fix(#2522): ctrl-c should safely abort confirm()
1 parent abe5eca commit 518ab7a

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lua/fzf-lua/actions.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ M.git_branch_del = function(selected, opts)
931931
utils.warn("Cannot delete active branch '%s'", branch)
932932
return
933933
end
934-
if vim.fn.confirm("Delete branch " .. branch .. "?", "&Yes\n&No") == 1 then
934+
if utils.confirm("Delete branch " .. branch .. "?", "&Yes\n&No") == 1 then
935935
table.insert(cmd_del_branch, branch)
936936
local output, rc = utils.io_systemlist(cmd_del_branch)
937937
if rc ~= 0 then
@@ -998,7 +998,7 @@ M.git_worktree_del = function(selected, opts)
998998
utils.warn("Cannot delete current worktree '%s'", worktree_path)
999999
return
10001000
end
1001-
if vim.fn.confirm("Delete worktree " .. worktree_path .. "?", "&Yes\n&No") == 1 then
1001+
if utils.confirm("Delete worktree " .. worktree_path .. "?", "&Yes\n&No") == 1 then
10021002
local cmd_del = path.git_cwd({ "git", "worktree", "remove", worktree_path }, opts) ---@type string[]
10031003
local output, rc = utils.io_systemlist(cmd_del)
10041004
if rc ~= 0 then
@@ -1045,7 +1045,7 @@ M.git_checkout = function(selected, opts)
10451045
local commit_hash = match_commit_hash(selected[1], opts)
10461046
local current_commit = utils.io_systemlist(cmd_cur_commit)[1]
10471047
if commit_hash == current_commit then return end
1048-
if vim.fn.confirm("Checkout commit " .. commit_hash .. "?", "&Yes\n&No") == 1 then
1048+
if utils.confirm("Checkout commit " .. commit_hash .. "?", "&Yes\n&No") == 1 then
10491049
local cmd_checkout = path.git_cwd({ "git", "checkout" }, opts)
10501050
table.insert(cmd_checkout, commit_hash)
10511051
local output, rc = utils.io_systemlist(cmd_checkout)
@@ -1103,7 +1103,7 @@ M.git_stage_unstage = function(selected, opts)
11031103
end
11041104

11051105
M.git_reset = function(selected, opts)
1106-
if vim.fn.confirm("Reset " .. #selected .. " file(s)?", "&Yes\n&No") == 1 then
1106+
if utils.confirm("Reset " .. #selected .. " file(s)?", "&Yes\n&No") == 1 then
11071107
for _, s in ipairs(selected) do
11081108
s = utils.strip_ansi_coloring(s)
11091109
local is_untracked = s:sub(5, 5) == "?"
@@ -1118,14 +1118,14 @@ M.git_reset = function(selected, opts)
11181118
end
11191119

11201120
M.git_stash_drop = function(selected, opts)
1121-
if vim.fn.confirm("Drop " .. #selected .. " stash(es)?", "&Yes\n&No") == 1 then
1121+
if utils.confirm("Drop " .. #selected .. " stash(es)?", "&Yes\n&No") == 1 then
11221122
local cmd = path.git_cwd({ "git", "stash", "drop" }, opts)
11231123
git_exec(selected, opts, cmd)
11241124
end
11251125
end
11261126

11271127
M.git_stash_pop = function(selected, opts)
1128-
if vim.fn.confirm("Pop " .. #selected .. " stash(es)?", "&Yes\n&No") == 1 then
1128+
if utils.confirm("Pop " .. #selected .. " stash(es)?", "&Yes\n&No") == 1 then
11291129
local cmd = path.git_cwd({ "git", "stash", "pop" }, opts)
11301130
git_exec(selected, opts, cmd)
11311131
-- trigger autoread or warn the users buffer(s) was changed
@@ -1134,7 +1134,7 @@ M.git_stash_pop = function(selected, opts)
11341134
end
11351135

11361136
M.git_stash_apply = function(selected, opts)
1137-
if vim.fn.confirm("Apply " .. #selected .. " stash(es)?", "&Yes\n&No") == 1 then
1137+
if utils.confirm("Apply " .. #selected .. " stash(es)?", "&Yes\n&No") == 1 then
11381138
local cmd = path.git_cwd({ "git", "stash", "apply" }, opts)
11391139
git_exec(selected, opts, cmd)
11401140
-- trigger autoread or warn the users buffer(s) was changed

lua/fzf-lua/utils.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ function M.save_dialog(bufnr)
10551055
M.warn(string.format("buffer %d has unsaved changes", bufnr))
10561056
return false
10571057
end
1058-
local res = vim.fn.confirm(string.format([[Save changes to "%s"?]], info.name),
1058+
local res = M.confirm(string.format([[Save changes to "%s"?]], info.name),
10591059
"&Yes\n&No\n&Cancel")
10601060
if res == 0 or res == 3 then
10611061
-- user cancelled
@@ -1335,6 +1335,19 @@ function M.input(prompt, default)
13351335
return ok and res or nil
13361336
end
13371337

1338+
-- wrapper around |confirm()| to allow cancellation with `<C-c>`
1339+
---@param msg string
1340+
---@param choice? string
1341+
---@param default? integer
1342+
---@param ty? string
1343+
---@return integer
1344+
function M.confirm(msg, choice, default, ty)
1345+
local ok, res = pcall(vim.fn.confirm, msg, choice, default, ty)
1346+
if ok and type(res) == "number" then return res end
1347+
if type(res) == "string" and res:match("Keyboard interrupt") then return 0 end -- <C-c>
1348+
error(res)
1349+
end
1350+
13381351
function M.fzf_bind_to_neovim(key)
13391352
local conv_map = {
13401353
["alt"] = "A",

0 commit comments

Comments
 (0)