Created
August 20, 2022 09:26
-
-
Save mudssrali/a5fc28b4394bb62cd498641ba411eaca to your computer and use it in GitHub Desktop.
Open a browser with URL in Elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def browser_open(url) do | |
win_cmd_args = ["/c", "start", String.replace(url, "&", "^&")] | |
cmd_args = | |
case :os.type() do | |
{:win32, _} -> | |
{"cmd", win_cmd_args} | |
{:unix, :darwin} -> | |
{"open", [url]} | |
{:unix, _} -> | |
cond do | |
System.find_executable("xdg-open") -> {"xdg-open", [url]} | |
# When inside WSL | |
System.find_executable("cmd.exe") -> {"cmd.exe", win_cmd_args} | |
true -> nil | |
end | |
end | |
case cmd_args do | |
{cmd, args} -> System.cmd(cmd, args) | |
nil -> Logger.warn("could not open the browser, no open command found in the system") | |
end | |
:ok | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment