Add initial implementation of color printing utility with functions for various colors and error handling
parent
d313c78b5b
commit
0af1d7680a
11 changed files with 185 additions and 0 deletions
@ -0,0 +1,7 @@ |
|||||||
|
try: |
||||||
|
from colorprinter.colorprinter.print_color import * |
||||||
|
except ImportError: |
||||||
|
try: |
||||||
|
from colorprinter.print_color import * |
||||||
|
except ImportError: |
||||||
|
print("Could not import colorprinter module") |
||||||
Binary file not shown.
@ -0,0 +1,102 @@ |
|||||||
|
def choose_color(last_color_index): |
||||||
|
""" |
||||||
|
Selects the next color in a predefined sequence of colors. |
||||||
|
|
||||||
|
Args: |
||||||
|
last_color_index (int): The index of the last color used. |
||||||
|
|
||||||
|
Returns: |
||||||
|
tuple: A tuple containing the ANSI escape code for the selected color (str) |
||||||
|
and the index of the selected color (int). |
||||||
|
""" |
||||||
|
colors = { |
||||||
|
"blue": "\033[94m", |
||||||
|
"green": "\033[92m", |
||||||
|
"yellow": "\033[93m", |
||||||
|
"red": "\033[91m", |
||||||
|
"purple": "\033[95m", |
||||||
|
} |
||||||
|
color_keys = list(colors.keys()) |
||||||
|
color_index = (last_color_index + 1) % len(color_keys) |
||||||
|
color = color_keys[color_index] |
||||||
|
return colors[color], color_index |
||||||
|
|
||||||
|
|
||||||
|
def print_green(*args): |
||||||
|
""" |
||||||
|
Prints the provided arguments in green. |
||||||
|
""" |
||||||
|
text = "" |
||||||
|
for arg in args: |
||||||
|
text += str(arg) + " " |
||||||
|
print(f"\033[92m{text}\033[0m") |
||||||
|
|
||||||
|
|
||||||
|
def print_red(*args): |
||||||
|
""" |
||||||
|
Prints the provided arguments in red. |
||||||
|
""" |
||||||
|
text = "" |
||||||
|
for arg in args: |
||||||
|
text += str(arg) + " " |
||||||
|
print(f"\033[91m{text}\033[0m") |
||||||
|
|
||||||
|
|
||||||
|
def print_yellow(*args): |
||||||
|
""" |
||||||
|
Prints the provided arguments in yellow. |
||||||
|
""" |
||||||
|
text = "" |
||||||
|
for arg in args: |
||||||
|
text += str(arg) + " " |
||||||
|
print(f"\033[93m{text}\033[0m") |
||||||
|
|
||||||
|
|
||||||
|
def print_blue(*args): |
||||||
|
""" |
||||||
|
Prints the provided arguments in blue. |
||||||
|
""" |
||||||
|
text = "" |
||||||
|
for arg in args: |
||||||
|
text += str(arg) + " " |
||||||
|
print(f"\033[94m{text}\033[0m") |
||||||
|
|
||||||
|
|
||||||
|
def print_purple(*args): |
||||||
|
""" |
||||||
|
Prints the provided arguments in purple. |
||||||
|
""" |
||||||
|
text = "" |
||||||
|
for arg in args: |
||||||
|
text += str(arg) + " " |
||||||
|
print(f"\033[95m{text}\033[0m") |
||||||
|
|
||||||
|
|
||||||
|
def print_rainbow(*args): |
||||||
|
""" |
||||||
|
Prints the provided arguments in a rainbow of colors. |
||||||
|
|
||||||
|
Args: |
||||||
|
*args: Variable length argument list. Each argument can be a string, list, or dictionary. |
||||||
|
- If the argument is a list, each element in the list will be printed in a different color. |
||||||
|
- If the argument is a dictionary, each key-value pair will be printed in a different color. |
||||||
|
- If the argument is a string or any other type, it will be printed in a different color. |
||||||
|
|
||||||
|
Returns: |
||||||
|
None |
||||||
|
""" |
||||||
|
color_index = -1 |
||||||
|
text = "" |
||||||
|
for arg in args: |
||||||
|
if isinstance(arg, list): |
||||||
|
for i in arg: |
||||||
|
color_code, color_index = choose_color(color_index) |
||||||
|
text += f"{color_code}{i}\033[0m " |
||||||
|
elif isinstance(arg, dict): |
||||||
|
for k, v in arg.items(): |
||||||
|
color_code, color_index = choose_color(color_index) |
||||||
|
text += f"{color_code}{k}: {v}\033[0m " |
||||||
|
else: |
||||||
|
color_code, color_index = choose_color(color_index) |
||||||
|
text += f"{color_code}{arg}\033[0m " |
||||||
|
print(text) |
||||||
@ -0,0 +1,63 @@ |
|||||||
|
Metadata-Version: 2.4 |
||||||
|
Name: colorprinter |
||||||
|
Version: 0.1.0 |
||||||
|
Summary: A simple utility for printing text in various colors to the terminal |
||||||
|
Home-page: https://github.com/lasseedfast/colorprinter |
||||||
|
Author: Lasse Edfast |
||||||
|
Author-email: lasse@edfast.se |
||||||
|
Classifier: Programming Language :: Python :: 3 |
||||||
|
Classifier: License :: OSI Approved :: MIT License |
||||||
|
Classifier: Operating System :: OS Independent |
||||||
|
Requires-Python: >=3.6 |
||||||
|
Description-Content-Type: text/markdown |
||||||
|
License-File: LICENSE |
||||||
|
Dynamic: author |
||||||
|
Dynamic: author-email |
||||||
|
Dynamic: classifier |
||||||
|
Dynamic: description |
||||||
|
Dynamic: description-content-type |
||||||
|
Dynamic: home-page |
||||||
|
Dynamic: license-file |
||||||
|
Dynamic: requires-python |
||||||
|
Dynamic: summary |
||||||
|
|
||||||
|
# Colorful Print Utility |
||||||
|
|
||||||
|
This Python module provides a simple utility for printing text in various colors to the terminal. It supports printing in green, red, yellow, blue, and purple, as well as a special "rainbow" mode that cycles through colors for each argument. |
||||||
|
Use it as as you normaly use ```print()```. |
||||||
|
|
||||||
|
## Functions |
||||||
|
|
||||||
|
- `print_green(*args)`: Prints the given arguments in green color. |
||||||
|
- `print_red(*args)`: Prints the given arguments in red color. |
||||||
|
- `print_yellow(*args)`: Prints the given arguments in yellow color. |
||||||
|
- `print_blue(*args)`: Prints the given arguments in blue color. |
||||||
|
- `print_purple(*args)`: Prints the given arguments in purple color. |
||||||
|
- `print_rainbow(*args)`: Prints each argument in a different color, cycling through the available colors. Supports nested lists and dictionaries. |
||||||
|
|
||||||
|
## Usage |
||||||
|
|
||||||
|
To use this module, simply import it and call the desired function with the text you want to print as arguments. Here's an example: |
||||||
|
|
||||||
|
`git clone https://github.com/lasseedfast/colorprinter` |
||||||
|
|
||||||
|
This will clone this repo as a folder to your computer. When you want to use the colorprinter, make sure that folder is in the same folder as you're working. Then you can use it like: |
||||||
|
|
||||||
|
```python |
||||||
|
from colorprinter.print_color import print_green, print_rainbow |
||||||
|
|
||||||
|
print_green("This is in green!") |
||||||
|
print_rainbow("This", "will", "be", "in", "multiple", "colors!") |
||||||
|
``` |
||||||
|
|
||||||
|
Alternatively, you can import all functions at once using from print_color import \*. However, be cautious when using this approach as it imports all public names defined in the module, which can lead to conflicts with other modules or variables in your namespace.* |
||||||
|
|
||||||
|
## Customization |
||||||
|
The colors are defined using ANSI escape codes. You can modify the choose_color function or the color codes within each print function if you wish to customize the colors. |
||||||
|
|
||||||
|
## Requirements |
||||||
|
This module uses standard Python libraries only and should work on any system that supports Python and ANSI escape codes for terminal colors. |
||||||
|
|
||||||
|
## License |
||||||
|
This project is open-sourced under the MIT License. Feel free to use, modify, and distribute as you see fit. |
||||||
|
|
||||||
@ -0,0 +1,9 @@ |
|||||||
|
LICENSE |
||||||
|
README.md |
||||||
|
setup.py |
||||||
|
colorprinter/__init__.py |
||||||
|
colorprinter/print_color.py |
||||||
|
colorprinter.egg-info/PKG-INFO |
||||||
|
colorprinter.egg-info/SOURCES.txt |
||||||
|
colorprinter.egg-info/dependency_links.txt |
||||||
|
colorprinter.egg-info/top_level.txt |
||||||
@ -0,0 +1 @@ |
|||||||
|
|
||||||
@ -0,0 +1 @@ |
|||||||
|
colorprinter |
||||||
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue