Skip to content

Commit 5003ba6

Browse files
author
Montvydas Klumbys
committed
Remove ESC char printing when not needed
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.
1 parent f753317 commit 5003ba6

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Diff for: bashplotlib/utils/helpers.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,24 @@
2626
colour_help = ', '.join([colour for colour in bcolours if colour != "ENDC"])
2727

2828

29-
def get_colour(colour):
29+
def get_colour(colour, default="default"):
3030
"""
3131
Get the escape code sequence for a colour
3232
"""
33-
return bcolours.get(colour, bcolours['ENDC'])
33+
return bcolours.get(colour, bcolours[default])
3434

3535

36-
def printcolour(text, sameline=False, colour=get_colour("ENDC")):
36+
def printcolour(text, sameline=False, colour="default"):
3737
"""
3838
Print color text using escape codes
3939
"""
40-
if sameline:
41-
sep = ''
40+
sep = '' if sameline else '\n'
41+
42+
# If no colour set, do not print color ESC characters
43+
if get_colour(colour) == get_colour("ENDC"):
44+
sys.stdout.write(text + sep)
4245
else:
43-
sep = '\n'
44-
sys.stdout.write(get_colour(colour) + text + bcolours["ENDC"] + sep)
46+
sys.stdout.write(get_colour(colour) + text + get_colour("ENDC") + sep)
4547

4648

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

0 commit comments

Comments
 (0)