Skip to content

Commit

Permalink
feat: add trim_prompt setting for vim.ui.input (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Jan 14, 2024
1 parent 94b0d24 commit 48b7134
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion lua/dressing/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 13 additions & 2 deletions lua/dressing/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 48b7134

Please sign in to comment.