"""Navigation namespace: ``b.nav.*``.""" from __future__ import annotations from browser_cli.models import Tab from browser_cli.sdk.base import Namespace class NavigationNS(Namespace): """Open URLs, navigate history, and focus tabs.""" def open(self, url: str, *, background: bool = False, window: str | None = None, group: str | None = None) -> None: """Open *url* in a new tab.""" self._c._cmd("navigate.open", {"url": url, "background": background, "window": window, "group": group}) def open_wait( self, url: str, *, timeout: float = 30.0, background: bool = False, window: str | None = None, group: str | None = None, ) -> Tab: """Open *url* in a new tab and block until fully loaded. Returns the Tab.""" return self._c._require_tab( self._c._cmd("navigate.open_wait", { "url": url, "timeout": int(timeout * 1000), "background": background, "window": window, "group": group, }), "navigate.open_wait returned unexpected data", ) def reload(self, tab_id: int | None = None) -> None: self._c._cmd("navigate.reload", {"tabId": tab_id}) def hard_reload(self, tab_id: int | None = None) -> None: self._c._cmd("navigate.hard_reload", {"tabId": tab_id}) def back(self, tab_id: int | None = None) -> None: self._c._cmd("navigate.back", {"tabId": tab_id}) def forward(self, tab_id: int | None = None) -> None: self._c._cmd("navigate.forward", {"tabId": tab_id}) def focus(self, pattern: str) -> dict | None: """Focus the first tab whose URL matches *pattern*. Returns the matched tab info, if any.""" return self._c._cmd("navigate.focus", {"pattern": pattern}) def to(self, tab_id: int, url: str) -> None: """Navigate a specific tab to *url* in place.""" self._c._cmd("navigate.to", {"tabId": tab_id, "url": url}) def search( self, engine: str, query: str, *, background: bool = False, window: str | None = None, group: str | None = None, ) -> None: """Open a search query in the given engine (e.g. 'google', 'youtube', 'ddg').""" from urllib.parse import quote_plus from browser_cli.commands.search import ENGINES template = ENGINES.get(engine) if template is None: raise ValueError(f"Unknown search engine '{engine}'. Available: {', '.join(ENGINES)}") url = template.format(query=quote_plus(query)) self._c._cmd("navigate.open", {"url": url, "background": background, "window": window, "group": group})