Created
November 2, 2022 14:06
-
-
Save manucabral/b3e86dfb1ef5c61411d0ac5027a32669 to your computer and use it in GitHub Desktop.
A simple module for managing the console output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
BeautifulOutput is a module for managing the terminal output of the program including colors and formatting. | |
The colors are defined in the COLORS dictionary, you can change it to whatever you want. | |
You can also add more colors, just add them to the dictionary colors. | |
To use it, just import it and call BeautifulOutput.init() before using it. | |
Then you can call BeautifulOutput(str) to print a string with the formatting. | |
Script by: Manuel Cabral (github: manucabral) | |
All rights reserved. | |
''' | |
import colorama | |
class BeautifulOutput: | |
'''BeautifulOutput class core.''' | |
COLORS = { | |
':r:': colorama.Fore.RED, | |
':g:': colorama.Fore.GREEN, | |
':y:': colorama.Fore.YELLOW, | |
':b:': colorama.Fore.BLUE, | |
} | |
def __init__(self, output: str, parse: str = True): | |
'''Initialize the BeautifulOutput instance.''' | |
if parse: | |
output = self.__parse(output) | |
self.printc(output) | |
@staticmethod | |
def init() -> None: | |
'''Initialize the output module by initializing colorama.''' | |
colorama.init() | |
def __parse(self, text: str) -> str: | |
'''Parse a string and replace the color codes with the colorama codes.''' | |
for key, value in self.COLORS.items(): | |
text = text.replace(key, value) | |
return text | |
def printc(self, text: str) -> None: | |
'''Print a string to the output.''' | |
print(colorama.Style.RESET_ALL + text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from beautiful_output import BeautifulOutput | |
# Initialize the class for only use colors | |
BeautifulOutput.init() | |
# Print a message with color | |
BeautifulOutput(':r:Hello :g:World!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment