"""Shell completion command.""" from __future__ import annotations import sys from pathlib import Path import click from rich.console import Console console = Console() @click.command("completion") @click.argument("shell", type=click.Choice(["zsh", "bash", "fish"])) @click.option("--script", is_flag=True, help="Output the raw completion script instead of instructions") @click.pass_context def cmd_completion(ctx, shell, script): """Print shell completion setup instructions (or output the script with --script).""" if script: from click.shell_completion import BashComplete, FishComplete, ZshComplete cls = {"zsh": ZshComplete, "bash": BashComplete, "fish": FishComplete}[shell] comp = cls(ctx.find_root().command, {}, "browser-cli", "_BROWSER_CLI_COMPLETE") click.echo(comp.source()) return exe = sys.executable.replace("/python", "/browser-cli").replace("/python3", "/browser-cli") if not Path(exe).exists(): exe = "browser-cli" env_var = "_BROWSER_CLI_COMPLETE" if shell == "zsh": console.print("[bold]Quickest setup — generate the file once:[/bold]") console.print() console.print(" [cyan]uv run browser-cli completion zsh --script > ~/.zfunc/_browser-cli[/cyan]") console.print() console.print(" Then add these lines to [bold]~/.zshrc[/bold] (before any compinit call):") console.print(" [cyan]fpath=(~/.zfunc $fpath)[/cyan]") console.print(" [cyan]autoload -Uz compinit && compinit[/cyan]") console.print() console.print(" Reload: [cyan]exec zsh[/cyan]") console.print() console.print("[bold]Alternative — eval on every shell start (simpler but slower):[/bold]") console.print(f' [cyan]eval "$({env_var}=zsh_source {exe})"[/cyan]') elif shell == "bash": console.print("[bold]Quickest setup — generate the file once:[/bold]") console.print() console.print(" [cyan]uv run browser-cli completion bash --script > ~/.bash_completion.d/browser-cli[/cyan]") console.print() console.print(" Reload: [cyan]source ~/.bashrc[/cyan]") console.print() console.print("[bold]Alternative — eval on every shell start:[/bold]") console.print(f' [cyan]eval "$({env_var}=bash_source {exe})"[/cyan]') elif shell == "fish": console.print("[bold]Setup:[/bold]") console.print() console.print(" [cyan]uv run browser-cli completion fish --script > ~/.config/fish/completions/browser-cli.fish[/cyan]")