From c34ce665c255bb23f73ab140510540dd1c7123e4 Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Wed, 5 Jun 2024 11:56:20 +0200 Subject: [PATCH] feat: allow passing `footer` in input and select with builtin backend --- README.md | 17 +++++++++++++++++ lua/dressing/input.lua | 14 +++++++++----- lua/dressing/select/builtin.lua | 4 ++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4475f60..57f0c69 100644 --- a/README.md +++ b/README.md @@ -341,6 +341,23 @@ require('dressing').setup({ ## Notes for plugin authors +### Window Footers +With nvim 0.10, `input` and `select` (with the `builtin` backend) also support +adding a footer via the `opts` of `vim.ui.input` and `vim.ui.select`: + +```lua +vim.ui.input({ + prompt = "Hello World", + footer = "foobar", +}, function(text) print(text) end) +``` + +This is not part of the official API for `vim.ui.input` and `vim.ui.select`, but +added for increased customizability. The `footer` and `footer_pos` values are +simply passed to `vim.api.nvim_open_win()`. + +### Telescope customization + TL;DR: you can customize the telescope `vim.ui.select` implementation by passing `telescope` into `opts`. The `vim.ui` hooks are a great boon for us because we can now assume that users diff --git a/lua/dressing/input.lua b/lua/dressing/input.lua index 04e5d4a..03a27a8 100644 --- a/lua/dressing/input.lua +++ b/lua/dressing/input.lua @@ -264,10 +264,10 @@ end ---@param config table ---@param prompt_lines string[] ----@param default? string +---@param opts table ---@return integer ---@return boolean -local function create_or_update_win(config, prompt_lines, default) +local function create_or_update_win(config, prompt_lines, opts) local parent_win = 0 local winopt local win_conf @@ -295,8 +295,8 @@ local function create_or_update_win(config, prompt_lines, default) util.calculate_width(config.relative, config.prefer_width, config, parent_win) -- Then expand the width to fit the prompt and default value prefer_width = math.max(prefer_width, 4 + get_max_strwidth(prompt_lines)) - if default then - prefer_width = math.max(prefer_width, 2 + vim.api.nvim_strwidth(default)) + if opts.default then + prefer_width = math.max(prefer_width, 2 + vim.api.nvim_strwidth(opts.default)) end -- Then recalculate to clamp final value to min/max local width = util.calculate_width(config.relative, prefer_width, config, parent_win) @@ -323,6 +323,10 @@ local function create_or_update_win(config, prompt_lines, default) -- We used to use "prompt_align" here winopt.title_pos = config.prompt_align or config.title_pos end + if vim.fn.has("nvim-0.10") == 1 then + winopt.footer = opts.footer + winopt.footer_pos = opts.footer_pos + end winopt = config.override(winopt) or winopt @@ -416,7 +420,7 @@ local show_input = util.make_queued_async_fn(2, function(opts, on_confirm) local prompt_lines = vim.split(prompt, "\n", { plain = true, trimempty = true }) -- Create or update the window - local winid, start_in_insert = create_or_update_win(config, prompt_lines, opts.default) + local winid, start_in_insert = create_or_update_win(config, prompt_lines, opts) context = { winid = winid, on_confirm = on_confirm, diff --git a/lua/dressing/select/builtin.lua b/lua/dressing/select/builtin.lua index 460b2e8..9b17cbc 100644 --- a/lua/dressing/select/builtin.lua +++ b/lua/dressing/select/builtin.lua @@ -100,6 +100,10 @@ M.select = function(config, items, opts, on_choice) winopt.title = opts.prompt:gsub("^%s*(.-)%s*$", " %1 ") winopt.title_pos = config.title_pos or "center" end + if vim.fn.has("nvim-0.10") == 1 then + winopt.footer = opts.footer + winopt.footer_pos = opts.footer_pos + end winopt = config.override(winopt) or winopt local winid = vim.api.nvim_open_win(bufnr, true, winopt) for option, value in pairs(config.win_options) do