diff --git a/README.md b/README.md index 43874c0..5b101b5 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ dotfiles repo. ```lua vim.pack.add({ - { src = "https://git.housh.dev/michael/shortenurl.nvm" }, + { src = "https://git.housh.dev/michael/shortenurl.nvim" }, }) ``` diff --git a/lua/shortenurl/init.lua b/lua/shortenurl/init.lua index 23e9049..5cb2e2d 100644 --- a/lua/shortenurl/init.lua +++ b/lua/shortenurl/init.lua @@ -49,16 +49,24 @@ shortenurl.prompt = function(s) return cmd end +-- Run the command and return the short url as a string. +local function runCmd(cmd) + local output = trimNewline(vim.fn.system(cmd)) + if not output or output == "" then + return + end + return trimQuotes(output) +end + -- Convert url on the current line. shortenurl.convertUrl = function() local url = get_url() local cmd = shortenurl.prompt(url) - local output = trimNewline(vim.fn.system(cmd)) - if output == "" then + local output = runCmd(cmd) + if not output or output == "" then print("Got empty output") return end - output = trimQuotes(output) -- Replace occurence of the url with the shortened url. vim.cmd(":%s/" .. vim.fn.escape(url, "/") .. "/" .. vim.fn.escape(output, "/")) end @@ -67,7 +75,7 @@ end shortenurl.convertFromClipboard = function() local url = trimNewline(vim.fn.system("wl-paste")) local cmd = shortenurl.prompt(url) - local output = trimNewline(vim.fn.system(cmd)) + local output = runCmd(cmd) if not output or output == "" then print("Got empty output") return