From 0f53c256dac634f8c58c8fb348df67aa8803e554 Mon Sep 17 00:00:00 2001 From: Thomas Lambert Date: Mon, 3 Oct 2022 11:54:18 +0200 Subject: [PATCH] refact(prompt_title): move strip_extension to utils --- lua/telekasten.lua | 25 +++---------------------- lua/telekasten/utils.lua | 13 +++++++++++-- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 55b0cb5..dea80b2 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -353,12 +353,6 @@ local function save_all_mod_buffers() 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 -- ---------------------------------------------------------------------------- @@ -1666,12 +1660,7 @@ end local function RenameNote() local oldfile = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg }) - tkutils.prompt_title(function(newname) - if not newname then - newname = "" - end - newname = strip_extension(newname, M.Cfg.extension) - + tkutils.prompt_title(M.Cfg.extension, function(newname) local newpath = newname:match("(.*/)") or "" newpath = M.Cfg.home .. "/" .. newpath @@ -2049,11 +2038,7 @@ local function CreateNoteSelectTemplate(opts) return end - tkutils.prompt_title(function(title) - if not title then - title = "" - end - title = strip_extension(title, M.Cfg.extension) + tkutils.prompt_title(M.Cfg.extension, function(title) on_create_with_template(opts, title) end) end @@ -2126,11 +2111,7 @@ local function CreateNote(opts) return CreateNoteSelectTemplate(opts) end - tkutils.prompt_title(function(title) - if not title then - title = "" - end - title = strip_extension(title, M.Cfg.extension) + tkutils.prompt_title(M.Cfg.extension, function(title) on_create(opts, title) end) end diff --git a/lua/telekasten/utils.lua b/lua/telekasten/utils.lua index 5d17d2a..87df324 100644 --- a/lua/telekasten/utils.lua +++ b/lua/telekasten/utils.lua @@ -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)