Utility for colored print and input statements
Project description
Custom Color Print and Input Utility
This project provides a customizable utility for printing colored, styled text directly in the terminal.
It builds upon the popular termcolor library and extends its capabilities to allow for simpler and dynamic styling options, including inline colorization and formatting.
Features
- Customizable Colored Prints: Easily print styled text with options for bold, underline, and different foreground/background color combinations.
- Colored Input Prompts: Accept user input with colored and styled prompts, enhancing terminal interaction.
- Inline Colorization: Style specific parts of your text using a special format within the print message itself.
- Error, Warning, and Info Messages: Predefined formats for error, warning, and informational messages to streamline logging and debugging.
- Exception Messages: Predefined formats for exceptions that prints an error message with basic exception details.
- Multiline Input: Customizable multiline input prompts for collecting multiple lines of text.
Installation
You can install colorfulPyPrint via pip. This will also install termcolor as a dependency:
pip install colorfulPyPrint
Usage
Basic Custom Print and Input
print_custom
and input_custom
, allows for dynamic, styled text output by specifying foreground colors, background colors, bold, and underline attributes.
# Example: Print a message in green text, with a yellow background, and bold.
print_custom("Hello, World!", text_color="green", on_color="on_yellow", bold=True)
Inline Customization
The print_custom
and input_custom
functions support inline text color and style customization within a single string using special tags.
# Example:
# 'Hello!' will be green
# 'John' will be bold, underlined & red on grey
# 'birthday' will be bold green
# 'Feb 29' will be bold, & magenta on yellow
msg = ("<cyan:Hello!> My name is <bold_ul_red_on_grey:John>."
"My <bold_green:birthday> is on <bold_magenta_on_yellow:Feb 29>!")
print_custom(msg)
IMPORTANT: bold_ and ul_ always need to be in front of colors
Predefined Print & Input Functions
You can suffix print or input with any of the colors. E.g. print_red
, print_magenta
, input_green
, etc.
Errors, Info, Warning, and Done
For convenience, I have also added print_error
, print_info
, print_warning
and print_done
that add symbols to differentiate the output.
print_error("This is an error message")
print_info("This is info")
print_warning("This is a warning")
print_done("Task completed successfully")
Output:
${\color{red}❌ This\ is\ an\ error\ message}$
${\color{#00BFFF}⚡ This\ is\ info}$
${\color{#FF7E00}⚠ This\ is\ a\ warning}$
${\color{00FF00}✅ Task\ completed\ successfully}$
Exceptions
The print_exception
function accepts the exception object and prints an error message with the ExceptionClass and Exception message, and not print the traceback to keep the output clean.
# Example: print_exception()
try:
x = 1/0
except Exception as e:
print_exception(e)
Output:
${\color{red}❌ ZeroDivisionError: division by zero}$
Multiline Input
multiline_input
lets you collect multiple lines of input (including any new lines) until an end signal is provided (default: q!).
# Example: Collect multiline input from the user
text = multiline_input("Please enter the description:", end_signal="q!")
print("You entered:\n", text)
Output:
${\color{#00BFFF}Please\ enter\ the\ description:}$
${\color{#FF7E00}Enter\ multi-line\ text\ (to\ finish,\ in\ an\ empty\ line\ type\ '\color{#FF1493}q!\color{#FF7E00}'\ and\ hit\ enter)}$:
This is the user's input
The above empty line will be retained
The next line will end input
q!
Supported Colors and Attributes
Text Colors
red
,green
,yellow
,blue
,magenta
,cyan
,white
, andgrey
Background Colors
on_green
,on_yellow
,on_blue
,on_magenta
,on_cyan
,on_white
, andon_grey
Attributes
bold
,underline
Inline Tags
Use inline tags to change the format of parts of your message dynamically. Example: <bold_red:Hello>
will print "Hello" in bold red.
msg = "<bold_green:Success!> The operation completed <ul_blue:without issues>."
print_custom(msg)
Contributing
Contributions are welcome! Please open an issue or submit a pull request with any improvements, bug fixes, or new features.
- Fork the repository.
- Create your feature branch:
git checkout -b my-new-feature
. - Commit your changes:
git commit -m 'Add some feature'
. - Push to the branch:
git push origin my-new-feature
. - Open a pull request.
Acknowledgments
Special thanks to the creators of the termcolor project for providing the foundation upon which this utility is built. Please check the project for more advanced styling and color schemes.
License
This project is licensed under the MIT License. See the LICENSE file for details.
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
File details
Details for the file colorfulpyprint-0.1.1.tar.gz
.
File metadata
- Download URL: colorfulpyprint-0.1.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e08a18adfbe46d8dc2f8e66d2a2c3eb9a733d11d04c67be70b937db76d7e2f7e |
|
MD5 | 0c9c5497ff920a6c934d33526e1df0b6 |
|
BLAKE2b-256 | cd176a24459fc3742e2e2ca8224e8fa819a2327373efe3f44f3ee47437e9a90a |
File details
Details for the file colorfulPyPrint-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: colorfulPyPrint-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff12d52de930386f10e8212e5d3081fa395816866747c9816b3f4599678df324 |
|
MD5 | 2dd651598e8846609442a76cde22ea86 |
|
BLAKE2b-256 | 2ed5e6a828308ccf85296c02776c0005f13019359bff891f3882fd843b853171 |