Last active
August 7, 2020 09:01
-
-
Save lablnet/4eccde2c3025670fedd3504f6bb66967 to your computer and use it in GitHub Desktop.
Enable coloured print in python
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
| """c_print.py: Enable coloured print.""" | |
| __author__ = "Muhammad Umer Farooq" | |
| __license__ = "MIT" | |
| __version__ = "1.0.0" | |
| __maintainer__ = "Muhammad Umer Farooq" | |
| __email__ = "[email protected]" | |
| __status__ = "Production" | |
| def c_print(msg, color=None): | |
| """ | |
| Print color output | |
| :param msg: msg you want to print in colored | |
| :param color: color name | |
| :return: None | |
| :colors: 'gray' | |
| 'green' | |
| 'red' | |
| 'yellow' | |
| 'purple' | |
| 'magenta' | |
| 'cyan' | |
| 'black' | |
| :Example: | |
| c_print("Test", 'yellow') | |
| """ | |
| # If no msg provided throw an exception. | |
| if not msg: | |
| raise Exception("Msg is required") | |
| # print colors. | |
| colors = { | |
| 'gray': '\033[90m', | |
| 'green': '\033[92m', | |
| 'red': '\033[91m', | |
| 'yellow': '\033[93m', | |
| 'purple': '\033[94m', | |
| 'magenta': '\033[95m', | |
| 'cyan': '\033[96m', | |
| 'black': '\033[97m', | |
| 'end': '\033[0m' | |
| } | |
| # If color provided or exists in colors print it otherwise ignore it and just print msg. | |
| if color in colors.keys(): | |
| print(colors[color] + str(msg) + colors['end']) | |
| else: | |
| print(msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment