feat: add display formatting helpers

This commit is contained in:
2026-05-21 12:30:21 +08:00
parent d1c61ee930
commit 06452f48fe

View File

@@ -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}")