A package for formatted console output using ANSI escape codes - colors and formatting.
Project description
This library allows you to set the foreground and background colors and style of your Console output. The script uses ANSI Escape Codes for colors and allows you to create custom colors as well that are also passed as 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 without having to understand the Escape Codes or how they are supported.
This library also exposes some functionality that allows you to print WARNING, ERROR, and INFO messages or to print an Exception's stacktrace as an ERROR. This helps when you're using other 3rd-party libraries that are very verbose, and you cannot turn off their output. Meanwhile you're crossing your eyes trying to find Exception messages like 'was that an Exception? It was kinda shaped like an exception message...' 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 session, 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 the following import statements as needed:
import formatted_console_output as fmt
from formatted_console_output import output_formatted_message as print
from formatted_console_output import output_many_format_message as printf
from formatted_console_output import output_error_message as msg
from formatted_console_output import log_exception_stacktrace as err
CODE COMPATIBILITY & OVERLOADING/ALIASING
I have found it easiest mentally to alias each method as shown in the imports above. You do not have to alias the method imports this way but that makes it more natural for you to code with - like a method overload that allows you to leverage everything else about the print() method. In fact in all four exposed methods in this library, all extra keyword arguments that are normally used in a print() call will get passed on properly, so go wild.
Using the alias 'print' does NOT interfere with any other scripts. Any code referencing this library's output method as 'print' that then wants to use the normal print() method would absolutely get the expected behavior even if it passed through this library first.
HOW TO USE IN CODE
Methods Exposed in this Library
- output_formatted_message() - Replaces print. Takes a message and a print() method's kwargs along with your added format and colors
- output_many_format_message() - Prints an array of FormattedPhrase objects. Also takes a print() method's kwargs
- output_error_message() - Prints a message as ERROR, WARNING, or INFO. Also takes a print() method's kwargs
- log_exception_stacktrace() - Prints an Exception including the stacktrace as ERROR. Also takes a print() method's kwargs
Keyword Arguments for This Library (kwargs)
The added keyword arguments that you will use most 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]
- ErrorType [INFO, WARNING, ERROR]
- CustomColor - any RGB as a fg_color or bg_color
- Parameters in constructor: int:r, int:g, int:b, bool:is_fg
- Can be passed as an argument in place of BackgroundColor or ForegroundColor
- FormattedPhrase() - an individually formatted piece of text that can be joined to others
- Parameters in constructor: str:string_phrase, enum:bg_color, enum:fg_color, enum:format
- Methods/Functions:
- get_output() - returns the string_phrase with all escape codes embedded
NOTE: parameters bg_color and fg_color in any method can take either an Enumerator value (BackgroundColor, ForeGroundColor) or a CustomColor object.
CODE EXAMPLES
Example 1: Print a single line of formatted text
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:
import formatted_console_output as fmt
from formatted_console_output import output_formatted_message as print
print(
f"The quick brown foo jumped over the lazy bar",
fg_color=fmt.ForegroundColor.BLUE,
bg_color=fmt.BackgroundColor.YELLOW,
format=fmt.TextFormat.BOLD,
sep=" - ",
end="\n\n",
)
Example 2: Use a CustomColor
In this example, we're passing a CustomColor which is a brown color in place of the BackgroundColor enum for bg_color. So this will print yellow on brown:
import formatted_console_output as fmt
from formatted_console_output import output_formatted_message as print
print(
"The quick brown foo jumped over the lazy bar",
fg_color=fmt.ForegroundColor.YELLOW,
bg_color=fmt.CustomColor(r=156, g=101, b=0, is_fg=False),
)
Example 3: Multiple formats in a single message using FormattedPhrase
In this example we're formatting each phrase differently and putting it together using an array of FormattedPhrase objects:
Note: any left out parameter in any FormattedPhrase object is considered to be set to NONE
import formatted_console_output as fmt
from formatted_console_output import output_many_format_message as printf
#create an array of FormattedPhrase objects to pass to printf()
message = [
fmt.FormattedPhrase(
"The ",
bg_color=fmt.BackgroundColor.BLACK,
fg_color=fmt.ForegroundColor.YELLOW,
format=fmt.TextFormat.UNDERLINE,
),
fmt.FormattedPhrase(
"quick brown foo ",
bg_color=fmt.BackgroundColor.RED,
fg_color=fmt.ForegroundColor.YELLOW,
format=fmt.TextFormat.BOLD,
),
fmt.FormattedPhrase(
"jumped over the ",
fg_color=fmt.ForegroundColor.YELLOW,
),
fmt.FormattedPhrase(
"lazy ",
bg_color=fmt.BackgroundColor.BLUE,
fg_color=fmt.ForegroundColor.YELLOW,
),
fmt.FormattedPhrase(
"bar",
bg_color=fmt.BackgroundColor.BLUE,
fg_color=fmt.ForegroundColor.YELLOW,
format=fmt.TextFormat.BOLD_AND_UNDERLINE,
),
]
printf(message)
Example 4: Combining multiple messages over time
In this 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 over the course of the program's execution before printing at the end (and still get all formatting) by doing something like this:
import formatted_console_output as fmt
from formatted_console_output import output_formatted_message as print
message = ""
# some code
message += fmt.FormattedPhrase(
"The ",
bg_color=fmt.BackgroundColor.BLACK,
fg_color=fmt.ForegroundColor.YELLOW,
format=fmt.TextFormat.UNDERLINE,
).get_output()
# some more code
message += fmt.FormattedPhrase(
"quick brown foo ",
bg_color=fmt.BackgroundColor.RED,
fg_color=fmt.ForegroundColor.YELLOW,
format=fmt.TextFormat.BOLD,
).get_output()
# some more code
message += fmt.FormattedPhrase(
"jumped over the ",
bg_color=fmt.BackgroundColor.BLACK,
fg_color=fmt.ForegroundColor.YELLOW,
).get_output()
# some more code
message += fmt.FormattedPhrase(
"lazy ",
bg_color=fmt.BackgroundColor.BLUE,
fg_color=fmt.ForegroundColor.YELLOW,
format=fmt.TextFormat.BOLD,
).get_output()
# some more code
message += fmt.FormattedPhrase(
"bar",
bg_color=fmt.BackgroundColor.BLUE,
fg_color=fmt.ForegroundColor.YELLOW,
format=fmt.TextFormat.BOLD_AND_UNDERLINE,
).get_output()
print(message)
Example 5: Printing an Exception with its Stacktrace
With log_exception_stacktrace() exposed in this library, you can intercept an Exception in a catch block (Except statement) and even get the full stacktrace back by doing something like this:
from formatted_console_output import log_exception_stacktrace as err
def func(a, b):
raise Exception("This is a generic exception for testing purposes.")
def main():
try:
a = b = None
func(a, b)
except Exception as e:
err(e)
main()
Example 6: Printing INFO, WARNING, and ERROR messages
With output_error_message() exposed, you can print messages as red (ERROR), yellow (WARNING), or cyan (INFO) by doing this:
import formatted_console_output as fmt
from formatted_console_output import output_error_message as msg
msg("This is an Informational Message.", error_type=fmt.ErrorType.INFO)
msg("This is a Warning Message.", error_type=fmt.ErrorType.WARNING)
msg("This is an Error Message.", error_type=fmt.ErrorType.ERROR)
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.4.3.tar.gz.
File metadata
- Download URL: formatted_console_output-0.4.3.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d810052fe4b2b9161b3b27f52838e4fed8e402455a92f59da646ed2904ad7b58
|
|
| MD5 |
fd28f05fce5d6f893b76648a1c671428
|
|
| BLAKE2b-256 |
7821f4b27c676b6fd10506886fa533b82e35bb9230c534b06f499476ee8d9264
|
File details
Details for the file formatted_console_output-0.4.3-py3-none-any.whl.
File metadata
- Download URL: formatted_console_output-0.4.3-py3-none-any.whl
- Upload date:
- Size: 8.2 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 |
57ba7db0f350a180306af8a73e048ec421f8ae4689517f433f4151d67452629b
|
|
| MD5 |
9298442179389adc1614280e24418fd0
|
|
| BLAKE2b-256 |
716e6a9858efe3f7d56efeb1fb9b36989151da4e8c5387810d715d138f655980
|