Created
June 30, 2015 11:18
-
-
Save tuvokki/14deb97bef6df9bc6553 to your computer and use it in GitHub Desktop.
Simple Python class to create colored messages for command line printing
This file contains 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
# Helper class to print colored output | |
# | |
# To use code like this, you can do something like | |
# | |
# print bcolors.WARNING | |
# + "Warning: No active frommets remain. Continue?" | |
# + bcolors.ENDC | |
# | |
# you can also use the convenience method bcolors.colored like this | |
# | |
# print(bcolors.colored("This frumble is underlined", bcolors.UNDERLINE)) | |
# | |
# or use one of the following convenience methods | |
# warning, fail, ok, okblue, header | |
# | |
# like this | |
# | |
# print(bcolors.warning("This is dangerous")) | |
# | |
# Method calls can be nested too, pritn an underlined header do this: | |
# | |
# print(bcolors.header(bcolors.colored("The line under this text is purple too ... ", bcolors.UNDERLINE))) | |
class bcolors: | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
ENDC = '\033[0m' | |
# Method that returns a message with the desired color | |
# usage: | |
# print(bcolor.colored("My colored message", bcolor.OKBLUE)) | |
@staticmethod | |
def colored(message, color): | |
return color + message + bcolors.ENDC | |
# Method that returns a yellow warning | |
# usage: | |
# print(bcolors.warning("What you are about to do is potentially dangerous. Continue?")) | |
@staticmethod | |
def warning(message): | |
return bcolors.WARNING + message + bcolors.ENDC | |
# Method that returns a red fail | |
# usage: | |
# print(bcolors.fail("What you did just failed massively. Bummer")) | |
# or: | |
# sys.exit(bcolors.fail("Not a valid date")) | |
@staticmethod | |
def fail(message): | |
return bcolors.FAIL + message + bcolors.ENDC | |
# Method that returns a green ok | |
# usage: | |
# print(bcolors.ok("What you did just ok-ed massively. Yay!")) | |
@staticmethod | |
def ok(message): | |
return bcolors.OKGREEN + message + bcolors.ENDC | |
# Method that returns a blue ok | |
# usage: | |
# print(bcolors.okblue("What you did just ok-ed into the blue. Wow!")) | |
@staticmethod | |
def okblue(message): | |
return bcolors.OKBLUE + message + bcolors.ENDC | |
# Method that returns a header in some purple-ish color | |
# usage: | |
# print(bcolors.header("This is great")) | |
@staticmethod | |
def header(message): | |
return bcolors.HEADER + message + bcolors.ENDC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should import the class
bcolors
from the module (in your case also called bcolors because the file is also called that way):