Skip to content

Commit

Permalink
Remove ESC char printing when not needed
Browse files Browse the repository at this point in the history
For "default" or unknown (e.g. None) colour do not print ESC characters. This is Useful when storing output into a text file or when running on windows cmd.
  • Loading branch information
Montvydas Klumbys committed Mar 26, 2019
1 parent f753317 commit 5003ba6
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions bashplotlib/utils/helpers.py
Expand Up @@ -26,22 +26,24 @@
colour_help = ', '.join([colour for colour in bcolours if colour != "ENDC"])


def get_colour(colour):
def get_colour(colour, default="default"):
"""
Get the escape code sequence for a colour
"""
return bcolours.get(colour, bcolours['ENDC'])
return bcolours.get(colour, bcolours[default])


def printcolour(text, sameline=False, colour=get_colour("ENDC")):
def printcolour(text, sameline=False, colour="default"):
"""
Print color text using escape codes
"""
if sameline:
sep = ''
sep = '' if sameline else '\n'

# If no colour set, do not print color ESC characters
if get_colour(colour) == get_colour("ENDC"):
sys.stdout.write(text + sep)
else:
sep = '\n'
sys.stdout.write(get_colour(colour) + text + bcolours["ENDC"] + sep)
sys.stdout.write(get_colour(colour) + text + get_colour("ENDC") + sep)


def drange(start, stop, step=1.0, include_stop=False):
Expand Down

0 comments on commit 5003ba6

Please sign in to comment.