Skip to content

Instantly share code, notes, and snippets.

@tuvokki
Created June 30, 2015 11:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tuvokki/14deb97bef6df9bc6553 to your computer and use it in GitHub Desktop.
Save tuvokki/14deb97bef6df9bc6553 to your computer and use it in GitHub Desktop.
Simple Python class to create colored messages for command line printing
# 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
@snowman11784
Copy link

Does this class support Python 2.7?
I am trying to see if using this class would work for me, but it fails for each function I tried with the same error.
I have provided an example below...

$ python
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bcolors
>>> print(bcolors.warning("This is dangerous"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'warning'

@serajan
Copy link

serajan commented Nov 5, 2020

Does this class support Python 2.7?
I am trying to see if using this class would work for me, but it fails for each function I tried with the same error.
I have provided an example below...

$ python
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bcolors
>>> print(bcolors.warning("This is dangerous"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'warning'

For example if you have created a python file as bcolors.py, try as such snowman11784

from bcolors import bcolors
print(bcolors.warning("This is dangerous"))

@tuvokki
Copy link
Author

tuvokki commented Nov 10, 2020

You should import the class bcolors from the module (in your case also called bcolors because the file is also called that way):

Python 2.7.16 (default, Jun  5 2020, 22:59:21)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from bcolors import bcolors
>>> print(bcolors.warning("This is dangerous"))
This is dangerous

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment