mirror of
https://github.com/Ascyii/telekasten.nvim.git
synced 2026-01-01 06:14:23 -05:00
refact: print_error and prompt_title
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
local M = {}
|
||||
|
||||
--- Escapes Lua pattern characters for use in gsub
|
||||
-- Prints a basic error message
|
||||
local function print_error(s)
|
||||
vim.cmd("echohl ErrorMsg")
|
||||
vim.cmd("echomsg " .. '"' .. s .. '"')
|
||||
vim.cmd("echohl None")
|
||||
end
|
||||
|
||||
-- Escapes Lua pattern characters for use in gsub
|
||||
function M.escape(s)
|
||||
-- return s:gsub("[^%w]", "%%%1") -- Escape everything ?
|
||||
return s:gsub("[%%%]%^%-$().[*+?]", "%%%1")
|
||||
end
|
||||
|
||||
--- Returns string with listed chars removed (= safer gsub)
|
||||
-- Returns string with listed chars removed (= safer gsub)
|
||||
function M.strip(s, chars_to_remove)
|
||||
return s:gsub("[" .. M.escape(chars_to_remove) .. "]", "")
|
||||
end
|
||||
@@ -17,27 +24,4 @@ local function strip_extension(str, ext)
|
||||
return str:gsub("(" .. ext:gsub("%.", "%%.") .. ")$", "")
|
||||
end
|
||||
|
||||
function M.prompt_title(ext, defaultFile, callback)
|
||||
local canceledStr = "__INPUT_CANCELLED__"
|
||||
|
||||
vim.ui.input({
|
||||
prompt = "Title: ",
|
||||
cancelreturn = canceledStr,
|
||||
completion = "file",
|
||||
default = defaultFile,
|
||||
}, 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)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user