From 06452f48fe831097357c0946af52fbe3f8fe29d9 Mon Sep 17 00:00:00 2001 From: tech Date: Thu, 21 May 2026 12:30:21 +0800 Subject: [PATCH] feat: add display formatting helpers --- src/autossh_mgr/display.py | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/autossh_mgr/display.py diff --git a/src/autossh_mgr/display.py b/src/autossh_mgr/display.py new file mode 100644 index 0000000..70aab4e --- /dev/null +++ b/src/autossh_mgr/display.py @@ -0,0 +1,57 @@ +import click +from autossh_mgr.config import TunnelConfig +from autossh_mgr.process import TunnelStatus + + +def _server_str(t: TunnelConfig) -> str: + return f"{t.user}@{t.host}:{t.port}" + + +def _ports_str(t: TunnelConfig) -> str: + return f"{t.local_port} -> {t.remote_port}" + + +def print_tunnel_table(tunnels: list[TunnelConfig], statuses: dict[str, TunnelStatus]) -> None: + if not tunnels: + click.echo("No tunnels configured.") + return + fmt = f"{'NAME':<20} {'SERVER':<32} {'PORTS':<15} STATUS" + click.echo(fmt) + click.echo("-" * 72) + for t in tunnels: + s = statuses.get(t.name) + state = s.state if s else "unknown" + click.echo(f"{t.name:<20} {_server_str(t):<32} {_ports_str(t):<15} {state}") + + +def print_status_table(tunnels: list[TunnelConfig], statuses: dict[str, TunnelStatus]) -> None: + if not tunnels: + click.echo("No tunnels configured.") + return + fmt = f"{'NAME':<20} {'STATE':<10} {'PID':<8} UPTIME" + click.echo(fmt) + click.echo("-" * 52) + for t in tunnels: + s = statuses.get(t.name) + if s: + pid_str = str(s.pid) if s.pid else "-" + uptime_str = s.uptime or "-" + click.echo(f"{t.name:<20} {s.state:<10} {pid_str:<8} {uptime_str}") + + +def print_tunnel_detail(tunnel: TunnelConfig, status: TunnelStatus | None) -> None: + click.echo(f"Name: {tunnel.name}") + click.echo(f"Host: {tunnel.host}") + click.echo(f"SSH port: {tunnel.port}") + click.echo(f"User: {tunnel.user}") + click.echo(f"Identity file: {tunnel.identity_file}") + click.echo(f"Local: {tunnel.local_host}:{tunnel.local_port}") + click.echo(f"Remote: {tunnel.remote_host}:{tunnel.remote_port}") + if tunnel.ssh_options: + click.echo(f"SSH options: {tunnel.ssh_options}") + if status: + click.echo(f"State: {status.state}") + if status.pid: + click.echo(f"PID: {status.pid}") + if status.uptime: + click.echo(f"Uptime: {status.uptime}")