Skip to main content

A package for formatted console output using ANSI escape codes - colors AND formatting.

Project description

This script allows you to set the foreground, and background colors of your Console output as well as setting a few styles. The script uses ANSI Escape Codes for colors black, red, green, yellow, blue, magenta, cyan, & white. It also uses the ANSI Escape Codes for styles bold, underline, and both combined. Coders can either put together a mult-formatted message or simply set the format for a message.

This helps when you're using very verbose libraries where you cannot turn off their output. Meanwhile you're trying to catch Exception messages like 'was that an Exception? It was kinda shaped like an exception." Now we can make your Exception red!!!!

OS COMPATIBILITY

Though Linux and Mac are pretty much alsways compatible... This library seamlessly takes into account Windows machines that are not configured to support ANSI escape codes. When your script runs, the library attempts to enable this feature for this session. If you're compatible, I go formatted. If I can make you compatible for this seesion, I go formatted. If all fails, then I go back to printing with default behavior.

HOW TO REFERENCE

  1. install the library
    pip install formatted-console-output
  1. At the top of your main script add the following import statement:
    from formatted_console_output import ForegroundColor, BackgroundColor, TextFormat, output_formatted_message as print, output_many_format_message as printf

CODE COMPATIBILITY/OVERLOADING

You do not have to alias the method imports as "print" and "printf" but that makes it more natural for you to code against and allows you to leverage everything else about the print() method. The script passes on all extra keyword arguments that are normally used in a print() call, so go wild. Anyone referencing this library that tries to use the print() method as normal would still get default console output with no format and normal behavior otherwise.

HOW TO USE IN CODE

The enumerators that define these colors/formats are:

  • ForegroundColor [BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, NONE]
  • BackgroundColor [BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, NONE]
  • TextFormat [BOLD, UNDERLINE, BOLD_AND_UNDERLINE, NONE]

The added keyword arguments are:

  • fg_color (default is ForegroundColor.NONE)
  • bg_color (default is BackgroundColor.NONE)
  • format (default is TextFormat.NONE)

In the following example we're printing a message to console with one format for the entire line: blue text on a yellow background in bold style.

I also threw in some standard keyword arguments to show that:

print(
    f"Archiving files and reporting against them in '{dir_path}'",
    fg_color=ForegroundColor.BLUE,
    bg_color=BackgroundColor.YELLOW,
    format=TextFormat.BOLD,
    sep="  -  ",
    end="\n\n"
)

In this example we're formatting each phrase differently and putting it together using an array of FormattedPhrase objects (any left out parameters in any Phrase object is considered to be set to NONE):

message = [
    FormattedPhrase(
        "The ",
        bg_color=BackgroundColor.BLACK,
        fg_color=ForegroundColor.YELLOW,
        format=TextFormat.UNDERLINE,
    ),
    FormattedPhrase(
        "quick brown foo ",
        bg_color=BackgroundColor.RED,
        fg_color=ForegroundColor.YELLOW,
        format=TextFormat.BOLD,
    ),
    FormattedPhrase(
        "jumped over the ",
        bg_color=BackgroundColor.BLACK,
        fg_color=ForegroundColor.YELLOW,
    ),
    FormattedPhrase(
        "lazy ",
        bg_color=BackgroundColor.BLUE,
        fg_color=ForegroundColor.YELLOW,
        format=TextFormat.BOLD,
    ),
    FormattedPhrase(
        "bar",
        bg_color=BackgroundColor.BLUE,
        fg_color=ForegroundColor.YELLOW,
        format=TextFormat.BOLD_AND_UNDERLINE,
    ),
]
printf(message)

In this final example, we're not printing, we're gathering formatted output to print later by pulling a FormattedPhrase object's get_output() method. In essence, a coder could put together an entire formatted paragragh in memory before printing by doing something like this:

message1 = FormattedPhrase(
    "The ",
    bg_color=BackgroundColor.BLACK,
    fg_color=ForegroundColor.YELLOW,
    format=TextFormat.UNDERLINE,
)
message2 = FormattedPhrase(
    "quick brown foo ",
    bg_color=BackgroundColor.RED,
    fg_color=ForegroundColor.YELLOW,
    format=TextFormat.BOLD,
)
message3 = FormattedPhrase(
    "jumped over the ",
    bg_color=BackgroundColor.BLACK,
    fg_color=ForegroundColor.YELLOW,
)
message4 = FormattedPhrase(
    "lazy ",
    bg_color=BackgroundColor.BLUE,
    fg_color=ForegroundColor.YELLOW,
    format=TextFormat.BOLD,
)
message5 = FormattedPhrase(
    "bar",
    bg_color=BackgroundColor.BLUE,
    fg_color=ForegroundColor.YELLOW,
    format=TextFormat.BOLD_AND_UNDERLINE,
)
my_message = (
    message1.get_output()
    + message2.get_output()
    + message3.get_output()
    + message4.get_output()
    + message5.get_output()
)
print(my_message)

###How do I make Exceptions Red?

You can intercept an Exception in a catch blcok (Except statement) by doing something like this at the top level:

def kill_empty_directory(dir_path):
    print(
        f"Removing {dir_path}\n",
        fg_color=ForegroundColor.BLUE,
        bg_color=BackgroundColor.YELLOW,
    )
    try:
        os.rmdir(dir_path)
    except Exception as e:
        original_error = str(e)
        print(f"Error deleting directory '{dir_path}': {original_error}",fg_color=ForegroundColor.RED)

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

formatted_console_output-0.2.4.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

formatted_console_output-0.2.4-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file formatted_console_output-0.2.4.tar.gz.

File metadata

File hashes

Hashes for formatted_console_output-0.2.4.tar.gz
Algorithm Hash digest
SHA256 2f570bced77c614567f0a8950d5d904c3519931ef9f1a2d0fa016b735259f9a1
MD5 a218230321b0d7b4b3c44b9299ab124a
BLAKE2b-256 af0ba0623f8a070aa333a3f8a5b0d30b2589713fe675358263aa7888feca47e0

See more details on using hashes here.

File details

Details for the file formatted_console_output-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for formatted_console_output-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3ea9b231b54b4b78f9ef7699b917e027e363cba98c2f2380e9118258022b856f
MD5 aac440f9ad805a70ed4be4ca57d0d9ba
BLAKE2b-256 0b4eed58841beb1fbd6a8c20717e7d54c1d6fbb95bff664ebd2e8dba502c6c5a

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