From 766cb749743faa1cd5952f084196cf586c336d64 Mon Sep 17 00:00:00 2001 From: jpk Date: Mon, 16 Jan 2023 16:30:17 +0100 Subject: [PATCH] Output formatting for fancy printing --- doxy/cli.py | 30 +++++++++++++++--------------- doxy/output.py | 14 +++++++++++++- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/doxy/cli.py b/doxy/cli.py index ac91c03..79ab926 100644 --- a/doxy/cli.py +++ b/doxy/cli.py @@ -52,7 +52,7 @@ def list(ctx): @click.pass_context @services.only_if_service_exists def edit(ctx, service): - print(Rule(f"Editing {service}")) + output.print_header(f"Editing {service}") compose_file = services.get_compose_file( Path(ctx.obj["CONFIG"].root_directory) / service ) @@ -70,7 +70,7 @@ def edit(ctx, service): @click.pass_context @services.only_if_service_exists def control(ctx, service, command): - print(Rule(f"Controlling {service}")) + output.print_header(f"Controlling {service}") compose_file = services.get_compose_file( Path(ctx.obj["CONFIG"].root_directory) / service ) @@ -88,13 +88,13 @@ def update(ctx, service, remove): compose_file = services.get_compose_file( Path(ctx.obj["CONFIG"].root_directory) / service ) - print(Rule(f"Updating {service}")) - command_chain = [ - ["down" if remove else "stop"], - ["pull"], - ["up", "-d"], - ] - for command in command_chain: + command_chain = { + f"Stopping {service}": ["down" if remove else "stop"], + f"Pulling {service} images": ["pull"], + f"Starting {service}": ["up", "-d"], + } + for title, command in command_chain.items(): + output.print_header(ctx, title) services.docker_compose_command(command, compose_file) @@ -106,12 +106,12 @@ def status(ctx, service): compose_file = services.get_compose_file( Path(ctx.obj["CONFIG"].root_directory) / service ) - print(Rule(f"Status of {service}")) - command_chain = [ - ["ps"], - ["top"], - ] - for command in command_chain: + command_chain = { + "Containers": ["ps"], + "Running processes": ["top"], + } + for title, command in command_chain.items(): + output.print_header(ctx, title) services.docker_compose_command(command, compose_file) diff --git a/doxy/output.py b/doxy/output.py index c073ac5..8f2f499 100644 --- a/doxy/output.py +++ b/doxy/output.py @@ -7,7 +7,7 @@ from rich.tree import Tree def _print_services_fancy(services: List[str]): - print(Rule(f"Listing services")) + print(Rule("Listing services")) tree = Tree("[bold]Available Services") for service in services: tree.add(service) @@ -27,3 +27,15 @@ def print_services(ctx: Context, services: List[str]): _print_services_simple(services) case _: echo("Unknown format choice") + + +def _print_header_fancy(text: str): + print(Rule(text)) + + +def print_header(ctx: Context, text: str): + match ctx.obj["FORMAT"]: + case "fancy": + _print_header_fancy(text) + case _: + pass