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 in JSON or simply set the format for the whole line.
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
- install the library
pip install formatted-console-output
- 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 a JSON structure where the key is the phrase being formatted and the parameters are the text formatting and colors (any left out parameter is considered to be set to NONE):
message = {
"The ": {"bg_color": BackgroundColor.BLACK, "fg_color": ForegroundColor.YELLOW},
"quick brown foo ": {
"bg_color": BackgroundColor.RED,
"fg_color": ForegroundColor.YELLOW,
"format": TextFormat.BOLD,
},
"jumped over the ": {
"bg_color": BackgroundColor.BLACK,
"fg_color": ForegroundColor.YELLOW,
},
"lazy ": {
"bg_color": BackgroundColor.BLUE,
"fg_color": ForegroundColor.YELLOW,
"format": TextFormat.BOLD,
},
"bar": {
"bg_color": BackgroundColor.BLUE,
"fg_color": ForegroundColor.YELLOW,
"format": TextFormat.BOLD_AND_UNDERLINE,
},
}
printf(message)
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.2.0.tar.gz.
File metadata
- Download URL: formatted_console_output-0.2.0.tar.gz
- Upload date:
- Size: 4.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 |
560203bc60c095f68cd36f981402b3f7e90fd1426f2836fe4fa598ca1c113918
|
|
| MD5 |
03c7ccdf41b66e75918f178895971c89
|
|
| BLAKE2b-256 |
c34bd32a25d65ac78c065248bcf1ccefe22aa7806588474db9fc13fc6b3327ff
|
File details
Details for the file formatted_console_output-0.2.0-py3-none-any.whl.
File metadata
- Download URL: formatted_console_output-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.5 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 |
5c8e873d49f546463eea9edab124e0a1ae0293d0cf0e70d9a47e628a9fe44bb8
|
|
| MD5 |
cf000046ffebaa26ae3f005b0d014d93
|
|
| BLAKE2b-256 |
402e65f97d550908fa379c094016e9ce6c46cccc2667180b8db161e581d233db
|