From 48b713413d0b78c21e10df500844ae6178418edd Mon Sep 17 00:00:00 2001 From: pseudometa <73286100+chrisgrieser@users.noreply.github.com> Date: Sun, 14 Jan 2024 18:15:12 +0100 Subject: [PATCH] feat: add `trim_prompt` setting for `vim.ui.input` (#134) --- README.md | 5 ++++- lua/dressing/config.lua | 5 ++++- lua/dressing/input.lua | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8e40eab..4475f60 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,10 @@ require("dressing").setup({ enabled = true, -- Default prompt string - default_prompt = "Input:", + default_prompt = "Input", + + -- Trim trailing `:` from prompt + trim_prompt = true, -- Can be 'left', 'right', or 'center' title_pos = "left", diff --git a/lua/dressing/config.lua b/lua/dressing/config.lua index 655c0fb..d4afe69 100644 --- a/lua/dressing/config.lua +++ b/lua/dressing/config.lua @@ -4,7 +4,10 @@ local default_config = { enabled = true, -- Default prompt string - default_prompt = "Input:", + default_prompt = "Input", + + -- Trim trailing `:` from prompt + trim_prompt = true, -- Can be 'left', 'right', or 'center' title_pos = "left", diff --git a/lua/dressing/input.lua b/lua/dressing/input.lua index bbc3cd1..2e8fc81 100644 --- a/lua/dressing/input.lua +++ b/lua/dressing/input.lua @@ -251,6 +251,17 @@ local function get_max_strwidth(lines) return max end +---@param title string +---@param trim_colon boolean input.trim_prompt config +---@return string +local function trim_and_pad_title(title, trim_colon) + title = vim.trim(title) + if trim_colon then + title = title:gsub(":$", "") + end + return (" %s "):format(title) +end + ---@param config table ---@param prompt_lines string[] ---@param default? string @@ -308,7 +319,7 @@ local function create_or_update_win(config, prompt_lines, default) end end if vim.fn.has("nvim-0.9") == 1 and #prompt_lines == 1 then - winopt.title = prompt_lines[1]:gsub("^%s*(.-)%s*$", " %1 ") + winopt.title = trim_and_pad_title(prompt_lines[1], config.trim_prompt) -- We used to use "prompt_align" here winopt.title_pos = config.prompt_align or config.title_pos end @@ -440,7 +451,7 @@ local function show_input(opts, on_confirm) if vim.fn.has("nvim-0.9") == 0 and #prompt_lines == 1 then util.add_title_to_win( winid, - string.gsub(prompt_lines[1], "^%s*(.-)%s*$", "%1"), + trim_and_pad_title(prompt_lines[1], config.trim_prompt), { align = config.prompt_align } ) end