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 allows you to create custom colors that are also passed as ANSI Escape Codes. The script also uses the ANSI Escape Codes for styles bold, underline, and both combined. Coders can either put together a multi-formatted message or simply set the format for a single print() 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 always compatible... This library seamlessly takes into account Windows machines that are not necessarily configured to support ANSI escape codes. When your script runs, the library attempts to enable this feature for this session. If you're already compatible, we go formatted. If we can make you compatible for this seesion, we go formatted. If all fails, then we go back to printing with default behavior.
HOW TO REFERENCE
- install the library
pip install formatted-console-output
- At the top of your script add one of the following import statements:
from formatted_console_output import ForegroundColor, BackgroundColor, TextFormat, FormattedPhrase, CustomColor, output_formatted_message as print, output_many_format_message as printf
...or...
from formatted_console_output import *
from formatted_console_output import 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
Keyword Arguments (kwargs)
The added keyword arguments are:
- fg_color (default is ForegroundColor.NONE)
- bg_color (default is BackgroundColor.NONE)
- format (default is TextFormat.NONE)
Enumerators & Classes
The enumerators/classes 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]
- CustomColor - any RGB
NOTE: bg_color and fg_color can take either an Enumerator value or a CustomColor object.
Code Examples
Example 1:
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 (sep and end) to show that can be included:
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"
)
Example 2
In this example, we're passing a CustomColor which is a brown color for bg_color. So this will print yellow on brown:
print(
"The quick brown foo jumped over the lazy bar",
fg_color=ForegroundColor.YELLOW,
bg_color=CustomColor(r=156, g=101, b=0, is_fg=False),
)
Example 3
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)
Example 4
In this final example, we're not printing until later. First we're gathering formatted output by pulling a FormattedPhrase object's get_output() method into a variable. 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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file formatted_console_output-0.3.0.tar.gz.
File metadata
- Download URL: formatted_console_output-0.3.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0788e2db318ba57edc668b66493fc42e552f48affc36ed723ccda5a551692215
|
|
| MD5 |
c9874baa5bf284de612af5d0248afa59
|
|
| BLAKE2b-256 |
e4e32267208a5c012c273a689af6cd942d8ad381bb279f98927258c4edf1859b
|
File details
Details for the file formatted_console_output-0.3.0-py3-none-any.whl.
File metadata
- Download URL: formatted_console_output-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7a10bf45b0da04a8f95a296583eba27ae87868f664bb3886103f5702c73e67b
|
|
| MD5 |
be265a2b06ce16cbc45f8016d0d812ca
|
|
| BLAKE2b-256 |
b6e3ed3aa6758ee215dfc7efdc4abe3edf0d80b767c4f8a1eb913a1c767af26f
|