Skip to content

Instantly share code, notes, and snippets.

@mudssrali
Created August 20, 2022 09:26
Show Gist options
  • Save mudssrali/a5fc28b4394bb62cd498641ba411eaca to your computer and use it in GitHub Desktop.
Save mudssrali/a5fc28b4394bb62cd498641ba411eaca to your computer and use it in GitHub Desktop.
Open a browser with URL in Elixir
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