Skip to content

Commit

Permalink
perl: improve open url handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sontungexpt committed Aug 20, 2023
1 parent 9b92229 commit eb25c12
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
53 changes: 49 additions & 4 deletions lua/url-open/modules/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit eb25c12

Please sign in to comment.