From 76919abaaf4951cc008ef6b6319d1a508aba4ea6 Mon Sep 17 00:00:00 2001 From: Thomas Lambert Date: Thu, 29 Sep 2022 11:11:20 +0200 Subject: [PATCH] fix(prompt): allow cancelling title input --- lua/telekasten.lua | 5 +++-- lua/telekasten/utils.lua | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 lua/telekasten/utils.lua diff --git a/lua/telekasten.lua b/lua/telekasten.lua index ce7822c..1296698 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -20,6 +20,7 @@ local linkutils = require("taglinks.linkutils") local dateutils = require("taglinks.dateutils") local Path = require("plenary.path") local vaultPicker = require("vaultpicker") +local tkutils = require("telekasten.utils") -- declare locals for the nvim api stuff to avoid more lsp warnings local vim = vim @@ -2038,7 +2039,7 @@ local function CreateNoteSelectTemplate(opts) return end - vim.ui.input({ prompt = "Title: " }, function(title) + tkutils.prompt_title(function(title) if not title then title = "" end @@ -2115,7 +2116,7 @@ local function CreateNote(opts) return CreateNoteSelectTemplate(opts) end - vim.ui.input({ prompt = "Title: ", default = "" }, function(title) + tkutils.prompt_title(function(title) if not title then title = "" end diff --git a/lua/telekasten/utils.lua b/lua/telekasten/utils.lua new file mode 100644 index 0000000..5d17d2a --- /dev/null +++ b/lua/telekasten/utils.lua @@ -0,0 +1,21 @@ +local M = {} + +function M.prompt_title(callback) + local canceledStr = "__INPUT_CANCELLED__" + + vim.ui.input({ + prompt = "Title: ", + default = "", + cancelreturn = canceledStr, + }, function(title) + if title == canceledStr then + vim.cmd("echohl WarningMsg") + vim.cmd("echomsg 'Note creation cancelled!'") + vim.cmd("echohl None") + else + callback(title) + end + end) +end + +return M