stevearc / dressing.nvim

Neovim plugin to improve the default vim.ui interfaces
MIT License
1.69k stars 32 forks source link

FR: Dynamic input width #162

Open chrisgrieser opened 3 weeks ago

chrisgrieser commented 3 weeks ago

So, I saw something similar in a folke plugin, and tinkered a bit with resizing the window depending on the content:

https://github.com/stevearc/dressing.nvim/assets/73286100/23dc190b-0365-4c07-8188-ac49f4f3ad82

Here the quick-and-dirty solution I came up with. I feel like it would be a nice addition to Dressing itself. What do you think?

-- dynamically resize input field
vim.api.nvim_create_autocmd("FileType", {
    group = vim.api.nvim_create_augroup("DressingInputWidth", {}),
    pattern = "DressingInput",
    callback = function(ctx)
        local win = vim.api.nvim_get_current_win()
        local startWidth = vim.api.nvim_win_get_width(win)
        local winOpts = vim.api.nvim_win_get_config(win)
        local pad = 3
        vim.api.nvim_create_autocmd({ "TextChangedI", "TextChanged" }, {
            buffer = ctx.buf,
            callback = function()
                local lineLength = #vim.api.nvim_get_current_line() + pad
                local newWidth = math.max(startWidth, lineLength)
                vim.api.nvim_win_set_config(win, {
                    relative = winOpts.relative,
                    row = winOpts.row,
                    col = math.floor((vim.o.columns - newWidth) / 2),
                    width = newWidth,
                })
            end,
        })
    end,
})
stevearc commented 2 weeks ago

Looks nice! I'd be open to looking at a PR