Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mff_pretty_cli-0.2.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mff_pretty_cli-0.2.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file mff_pretty_cli-0.2.2.tar.gz.

File metadata

  • Download URL: mff_pretty_cli-0.2.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for mff_pretty_cli-0.2.2.tar.gz
Algorithm Hash digest
SHA256 ae7d0639e279a76588fd9450fdf2c1ac9225524401157c7c086f688a71cca9cb
MD5 5e8c6dde393a8116c6988abdaaa68ca6
BLAKE2b-256 eebab5b87c46c253d8ef0602f5f3942f5532f6063e29e9905b8c38a2446ec50c

See more details on using hashes here.

File details

Details for the file mff_pretty_cli-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mff_pretty_cli-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4ec5f98e2b9bd87f2b0f667b6aec22b7d3d9cec4352455cec6b64b7c017eaf3b
MD5 4718b99c7343d402f4802495a32e3cbd
BLAKE2b-256 f3d0f30125235e86b3b8ba8acdcfc35e80b9b882daa3fcc65aa1e21d983372a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page