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

@@ -353,12 +353,6 @@ local function save_all_mod_buffers()
end end
end end
-- 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
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
-- image stuff -- image stuff
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
@@ -1666,12 +1660,7 @@ end
local function RenameNote() local function RenameNote()
local oldfile = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg }) local oldfile = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg })
tkutils.prompt_title(function(newname) tkutils.prompt_title(M.Cfg.extension, function(newname)
if not newname then
newname = ""
end
newname = strip_extension(newname, M.Cfg.extension)
local newpath = newname:match("(.*/)") or "" local newpath = newname:match("(.*/)") or ""
newpath = M.Cfg.home .. "/" .. newpath newpath = M.Cfg.home .. "/" .. newpath
@@ -2049,11 +2038,7 @@ local function CreateNoteSelectTemplate(opts)
return return
end end
tkutils.prompt_title(function(title) tkutils.prompt_title(M.Cfg.extension, function(title)
if not title then
title = ""
end
title = strip_extension(title, M.Cfg.extension)
on_create_with_template(opts, title) on_create_with_template(opts, title)
end) end)
end end
@@ -2126,11 +2111,7 @@ local function CreateNote(opts)
return CreateNoteSelectTemplate(opts) return CreateNoteSelectTemplate(opts)
end end
tkutils.prompt_title(function(title) tkutils.prompt_title(M.Cfg.extension, function(title)
if not title then
title = ""
end
title = strip_extension(title, M.Cfg.extension)
on_create(opts, title) on_create(opts, title)
end) end)
end end

View File

@@ -1,18 +1,27 @@
local M = {} 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__" local canceledStr = "__INPUT_CANCELLED__"
vim.ui.input({ vim.ui.input({
prompt = "Title: ", prompt = "Title: ",
default = "",
cancelreturn = canceledStr, cancelreturn = canceledStr,
}, function(title) }, function(title)
if not title then
title = ""
end
if title == canceledStr then if title == canceledStr then
vim.cmd("echohl WarningMsg") vim.cmd("echohl WarningMsg")
vim.cmd("echomsg 'Note creation cancelled!'") vim.cmd("echomsg 'Note creation cancelled!'")
vim.cmd("echohl None") vim.cmd("echohl None")
else else
title = strip_extension(title, ext)
callback(title) callback(title)
end end
end) end)