MunifTanjim / nui.nvim

UI Component Library for Neovim.
MIT License
1.62k stars 57 forks source link

prevent layout from closing when input submitted #377

Closed mboros1 closed 3 months ago

mboros1 commented 3 months ago

I'm trying to use the input box to send commands, which will send the inputs to an external program updating other popups

image

but when I submit, all of the boxes in the layout just close.

I can call a function in on submit to re-open the layout, but it causes the screen to flicker as it closes and re-opens which is a little annoying. Do you have advice on the best way to just have the input box clear the input text without closing when the on_submit function runs?

MunifTanjim commented 3 months ago

You can just do something like this:

  local prompt = "> "

  local input = Input({
    relative = "cursor",
    position = { row = 1, col = 0 },
    size = { width = 20 },
    border = "single",
  }, {
    prompt = prompt,
  })

  input:mount()

  vim.keymap.set({ "n", "i" }, "<Enter>", function()
    local value = string.sub(vim.api.nvim_get_current_line(), vim.fn.strwidth(prompt) + 1)
    print(value)
  end, { buffer = input.bufnr })

This will override the default behavior of <Enter> key (which is to submit and close the input).