In the following code, I have created an input that closes the current buffer when the user types 'y'. However, I am encountering an issue where the 'on_submit' callback is being executed twice. This causes the code to attempt to delete the same buffer twice, resulting in an error as shown in the image below.
local Input = require("nui.input")
local event = require("nui.utils.autocmd").event
local prompt_msg = "Close current file?"
local bufnr = vim.api.nvim_get_current_buf()
local input = Input({
position = "50%",
size = {
width = #prompt_msg + 4,
},
border = {
style = "single",
text = {
top = prompt_msg,
top_align = "center",
},
},
win_options = {
winhighlight = "Normal:Normal,FloatBorder:Normal",
},
}, {
prompt = "> ",
default_value = "N",
on_close = function()
print("Input Closed!")
end,
on_submit = function(value)
print("START - on_submit")
if value:lower() == "y" then
vim.api.nvim_buf_delete(bufnr, { force = true })
end
print("END - on_submit")
end,
})
input:mount()
input:on(event.BufLeave, function()
input:unmount()
end)
In the following code, I have created an input that closes the current buffer when the user types 'y'. However, I am encountering an issue where the 'on_submit' callback is being executed twice. This causes the code to attempt to delete the same buffer twice, resulting in an error as shown in the image below.