feat: add `trim_prompt` setting for `vim.ui.input` (#134)

This commit is contained in:
pseudometa 2024-01-14 18:15:12 +01:00 committed by GitHub
parent 94b0d24483
commit 48b713413d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 4 deletions

View File

@ -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",

View File

@ -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",

View File

@ -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