refact(prompt_title): move strip_extension to utils

This commit is contained in:
Thomas Lambert
2022-10-03 11:54:18 +02:00
parent e0c7d29679
commit 0f53c256da
2 changed files with 14 additions and 24 deletions

View File

@@ -1,18 +1,27 @@
local M = {}
function M.prompt_title(callback)
-- strip an extension from a file name, escaping "." properly, eg:
-- strip_extension("path/Filename.md", ".md") -> "path/Filename"
local function strip_extension(str, ext)
return str:gsub("(" .. ext:gsub("%.", "%%.") .. ")$", "")
end
function M.prompt_title(ext, callback)
local canceledStr = "__INPUT_CANCELLED__"
vim.ui.input({
prompt = "Title: ",
default = "",
cancelreturn = canceledStr,
}, function(title)
if not title then
title = ""
end
if title == canceledStr then
vim.cmd("echohl WarningMsg")
vim.cmd("echomsg 'Note creation cancelled!'")
vim.cmd("echohl None")
else
title = strip_extension(title, ext)
callback(title)
end
end)