Skip to content

Commit

Permalink
Fixes issue MasterOdin#9
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhan Kathawala committed Oct 18, 2017
1 parent a0aa467 commit 284e608
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions crayons.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,19 @@ def func_help(*args, **kwargs):
@property
def color_str(self):
style = 'BRIGHT' if self.bold else 'NORMAL'
c = '%s%s%s%s%s' % (getattr(colorama.Fore, self.color), getattr(colorama.Style, style), self.s, colorama.Fore.RESET, getattr(colorama.Style, 'NORMAL'))
c = '%s%s' % (getattr(colorama.Fore, self.color), getattr(colorama.Style, style))

# Find ANSI terminal escape sequences and add color info after them in the string
s = self.s
escape_regex = re.compile(r"""(?:\x1b\[[0-9]+m){2} # the first two ANSI esc seqs
(?:(?!\x1b)+.*)+ # anything not starting with esc seq
(?:\x1b\[[0-9]+m){2} # the last two ANSI esc seqs""", re.X)
escape_seqs = set(re.findall(escape_regex, s))
for seq in escape_seqs:
spl = s.split(seq)
s = (seq+c).join(spl)

c = '%s%s%s%s' % (c, s, getattr(colorama.Style, 'NORMAL'), colorama.Fore.RESET)

if self.always_color:
return c
Expand All @@ -76,7 +88,6 @@ def color_str(self):
else:
return self.s


def __len__(self):
return len(self.s)

Expand Down

0 comments on commit 284e608

Please sign in to comment.