Files
browser-cli/browser_cli/commands/navigate.py
T
2026-04-08 21:17:59 +02:00

76 lines
2.5 KiB
Python

import click
from browser_cli.client import send_command, BrowserNotConnected
from rich.console import Console
console = Console()
def _handle(command, args):
try:
return send_command(command, args)
except BrowserNotConnected as e:
console.print(f"[red]Error:[/red] {e}")
raise SystemExit(1)
except RuntimeError as e:
console.print(f"[red]Browser error:[/red] {e}")
raise SystemExit(1)
@click.command("open")
@click.argument("url")
@click.option("--bg", is_flag=True, help="Open in background (no focus)")
@click.option("--window", "window_name", default=None, help="Open in named window")
@click.option("--group", "group_name", default=None, help="Open directly into a tab group (name or ID)")
def cmd_open(url, bg, window_name, group_name):
"""Open URL in a new tab."""
result = _handle("navigate.open", {"url": url, "background": bg, "window": window_name, "group": group_name})
suffix = ""
if group_name:
suffix = f" in group '{group_name}'"
elif window_name:
suffix = f" in window '{window_name}'"
console.print(f"[green]Opened:[/green] {url}{suffix}")
@click.command("reload")
@click.argument("tab_id", type=int, required=False)
def cmd_reload(tab_id):
"""Reload the active (or specified) tab."""
_handle("navigate.reload", {"tabId": tab_id})
console.print("[green]Reloaded[/green]")
@click.command("hard-reload")
@click.argument("tab_id", type=int, required=False)
def cmd_hard_reload(tab_id):
"""Hard reload (bypass cache) the active (or specified) tab."""
_handle("navigate.hard_reload", {"tabId": tab_id})
console.print("[green]Hard reloaded[/green]")
@click.command("back")
@click.argument("tab_id", type=int, required=False)
def cmd_back(tab_id):
"""Navigate back in the active (or specified) tab."""
_handle("navigate.back", {"tabId": tab_id})
console.print("[green]Navigated back[/green]")
@click.command("forward")
@click.argument("tab_id", type=int, required=False)
def cmd_forward(tab_id):
"""Navigate forward in the active (or specified) tab."""
_handle("navigate.forward", {"tabId": tab_id})
console.print("[green]Navigated forward[/green]")
@click.command("focus")
@click.argument("pattern")
def cmd_focus(pattern):
"""Jump to the first tab whose URL matches PATTERN."""
result = _handle("navigate.focus", {"pattern": pattern})
if result:
console.print(f"[green]Focused:[/green] {result.get('url', result)}")
else:
console.print(f"[yellow]No tab found matching:[/yellow] {pattern}")