zabbix-cli icon indicating copy to clipboard operation
zabbix-cli copied to clipboard

Add console functions to `zabbix_cli.app.app`

Open pederhan opened this issue 1 year ago • 0 comments

When writing the plugin guide, it occurred to me that the current API for printing messages could be improved. Instead of importing these functions:

  • zabbix_cli.output.console.success
  • zabbix_cli.output.console.info
  • zabbix_cli.output.console.warning
  • zabbix_cli.output.console.error

We could define methods on zabbix_cli.app.app.StatefulApp which provides these functions wherever we have access to the app object. Furthermore, this would let us give the user more control over the styling of these functions by letting us automatically pass in some sort of state wherever these are accessed.

So in essence we would go from:

from zabbix_cli.output.console import success
from zabbix_cli.output.console import info
from zabbix_cli.output.console import warning
from zabbix_cli.output.console import error

@app.command(name="my_command")
def my_command() -> None:
    success("Success message")
    info("Info message")
    warning("Warning message")
    error("Error message")

To:

from zabbix_cli.app import app

@app.command(name="my_command")
def my_command() -> None:
    app.success("Success message")
    app.info("Info message")
    app.warning("Warning message")
    app.error("Error message")

Or:

from zabbix_cli.app import app

@app.command(name="my_command")
def my_command() -> None:
    app.console.success("Success message")
    app.console.info("Info message")
    app.console.warning("Warning message")
    app.console.error("Error message")

The abstraction is not clear to me yet. We need to figure out if we want to create a rich.console.Console subclass that provides these methods or some sort of thin abstraction class that wraps our existing functions.

pederhan avatar Sep 30 '24 08:09 pederhan