Simple helper to get pretty printing in the CLI.
Project description
Pretty-CLI: Pretty Printing for the CLI
This package provides PrettyCli
, a utility class for structured printing in the CLI. Simply use its print()
and helper methods instead of the default print()
and you're good to go!
The package is available in PyPi as mff-pretty-cli
. You can install it with:
pip install mff-pretty-cli
Here is a full example of the available function calls:
from pretty_cli import PrettyCli
cli = PrettyCli()
cli.main_title("my example file:\nAmazing")
cli.print("Hello, world!")
cli.print("你好!")
cli.big_divisor() # Divisors, titles, etc. add blank space above/under as needed.
cli.print("Let's print a dict:")
cli.blank() # Add a blank if the previous line is not blank already.
cli.blank()
cli.blank()
cli.print({ # Enforces nice alignment of dict contents.
"foo": "bar",
"nested": { "hi": "there" },
"another one": { "how": "are you?", "fine": "thanks" },
})
cli.small_divisor()
cli.print("Some header styles:")
cli.chapter("a chapter")
cli.subchapter("a sub-chapter")
cli.section("a section")
cli.print("That's all, folks!")
And the produced output:
==================================================================
======================== MY EXAMPLE FILE: ========================
============================ AMAZING =============================
==================================================================
Hello, world!
你好!
================================
Let's print a dict:
foo: bar
nested:
hi: there
another one:
how: are you?
fine: thanks
----------------
Some header styles:
================ A Chapter ================
-------- A Sub-Chapter --------
[A Section]
That's all, folks!
Dataclass Support
By default, dataclasses are converted to dicts. This code:
import math
from dataclasses import dataclass
from pretty_cli import PrettyCli
@dataclass
class MyData:
some_int: int
some_float: float
some_string: str
cli = PrettyCli()
my_data = MyData(
some_int=42,
some_float=math.pi,
some_string="Lorem ipsum dolor sit amet.",
)
cli.print(my_data)
Produces this output:
some_int: 42
some_float: 3.141592653589793
some_string: Lorem ipsum dolor sit amet.
Replicating to File
Sometimes, we want to save a copy of the output to a log file. In Unix systems, one can use tee
:
echo "Hi there" | tee hi.log
However, this is inconvenient in more complicated scenarios. For example, we might want to generate a timestamp within Python and use it for the file name, or we might want to strip color codes from the file output without using command-line tricks. PrettyCli
has built-in support for replicating output to a file, with optional stripping of ANSI codes (on by default):
cli = PrettyCli(log_file="path/to/file", strip_ansi=False) # strip_ansi defaults to True.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for mff_pretty_cli-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f8584a95b3a54fc0a6d0803abf77ce917f6ddcb2d1425668ba3cd6b847b00e7 |
|
MD5 | a167236a1bacebeccc3a16147787f4ac |
|
BLAKE2b-256 | e325b967fa5eba6b4565fcfbd454fd1f70c5bf0d72e815bb0a8d93a0a2f917ad |