Skip to content

Instantly share code, notes, and snippets.

@manucabral
Created November 2, 2022 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manucabral/b3e86dfb1ef5c61411d0ac5027a32669 to your computer and use it in GitHub Desktop.
Save manucabral/b3e86dfb1ef5c61411d0ac5027a32669 to your computer and use it in GitHub Desktop.
A simple module for managing the console output
'''
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)
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