Skip to content

Commit

Permalink
✨ Add open_browser function
Browse files Browse the repository at this point in the history
This replaces `webbrowser.open` in order to prevent
spammy messages being shown on the terminal.
  • Loading branch information
patrick91 committed Oct 30, 2024
1 parent 9fd28ae commit 00b28bf
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import subprocess
from unittest.mock import patch

import pytest
import typer

url = "http://example.com"


@pytest.mark.parametrize(
"system, command",
[
("Darwin", "open"),
("Linux", "xdg-open"),
("FreeBSD", "xdg-open"),
],
)
def test_open_browser_unix(system: str, command: str):
with patch("platform.system", return_value=system), patch(
"shutil.which", return_value=True
), patch("subprocess.Popen") as mock_popen:
typer.open_browser(url)

mock_popen.assert_called_once_with(
[command, url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
)


def test_open_browser_windows():
with patch("platform.system", return_value="Windows"), patch(
"webbrowser.open"
) as mock_webbrowser_open:
typer.open_browser(url)

mock_webbrowser_open.assert_called_once_with(url)


def test_open_browser_no_xdg_open():
with patch("platform.system", return_value="Linux"), patch(
"shutil.which", return_value=None
), patch("webbrowser.open") as mock_webbrowser_open:
typer.open_browser(url)

mock_webbrowser_open.assert_called_once_with(url)
1 change: 1 addition & 0 deletions typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
from .models import FileTextWrite as FileTextWrite
from .params import Argument as Argument
from .params import Option as Option
from .browser import open_browser as open_browser
46 changes: 46 additions & 0 deletions typer/browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import platform
import shutil
import subprocess


def _is_macos() -> bool:
return platform.system() == "Darwin"


def _is_linux_or_bsd() -> bool:
if platform.system() == "Linux":
return True

return "BSD" in platform.system()


def open_browser(url: str) -> None:
"""
Open a web browser to the specified URL.
This function handles different operating systems separately:
- On macOS (Darwin), it uses the 'open' command.
- On Linux and BSD, it uses 'xdg-open' if available.
- On Windows (and other OSes), it uses the standard webbrowser module.
The function avoids, when possible, using the webbrowser module on Linux and macOS
to prevent spammy terminal messages from some browsers (e.g., Chrome).
"""

if _is_macos():
subprocess.Popen(
["open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
)
return

has_xdg_open = _is_linux_or_bsd() and shutil.which("xdg-open") is not None

if has_xdg_open:
subprocess.Popen(
["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
)
return

import webbrowser

webbrowser.open(url)

0 comments on commit 00b28bf

Please sign in to comment.