From eb25c12b03b5174fcb4890016a8504a3d3367345 Mon Sep 17 00:00:00 2001 From: Tran Vo Son Tung Date: Sun, 20 Aug 2023 11:00:12 +0700 Subject: [PATCH] perl: improve open url handler --- README.md | 1 + lua/url-open/modules/handlers.lua | 53 ++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 033e23c..033e5b4 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ default browser of your system. - http://example.com:8080 - https://www.example.com:8443 - ftp://ftp.example.com:2121 +- Allow you to open url from anywhere in the line if it only contains 1 url ## Installation diff --git a/lua/url-open/modules/handlers.lua b/lua/url-open/modules/handlers.lua index b3a0537..b7b9885 100644 --- a/lua/url-open/modules/handlers.lua +++ b/lua/url-open/modules/handlers.lua @@ -97,13 +97,58 @@ M.open_url = function(user_opts) local shell_safe_url = fn.shellescape(url_to_open) local command = "" if vim.loop.os_uname().sysname == "Linux" then - command = "silent! !xdg-open " .. shell_safe_url + if fn.executable("xdg-open") == 1 then + command = "silent! !xdg-open " .. shell_safe_url + elseif fn.executable("gnome-open") then + command = "silent! !gnome-open " .. shell_safe_url + else + vim.schedule( + function() + vim.notify( + "No known command to open url on Linux", + vim.log.levels.ERROR, + { title = "URL Handler" } + ) + end + ) + return + end elseif vim.loop.os_uname().sysname == "Darwin" then - command = "silent! !open " .. shell_safe_url + if fn.executable("open") == 1 then + command = "silent! !open " .. shell_safe_url + else + vim.schedule( + function() + vim.notify( + "No known command to open url on MacOS", + vim.log.levels.ERROR, + { title = "URL Handler" } + ) + end + ) + return + end elseif vim.loop.os_uname().sysname == "Windows" then - command = "silent! !start " .. shell_safe_url + if fn.executable("start") == 1 then + command = "silent! !start " .. shell_safe_url + else + vim.schedule( + function() + vim.notify( + "No known command to open url on Windows", + vim.log.levels.ERROR, + { title = "URL Handler" } + ) + end + ) + return + end else - print("Unknown operating system.") + vim.schedule( + function() + vim.notify("Unknown operating system.", vim.log.levels.ERROR, { title = "URL Handler" }) + end + ) return end M.call_cmd(command, {