Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
Don't parse results of previous operations.
Browse files Browse the repository at this point in the history
Extension of #16. Better to
do this for Color() % str() and other operations.
  • Loading branch information
Robpol86 committed May 3, 2016
1 parent 06ad1d7 commit 051a6b4
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions colorclass/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ def __new__(cls, *args, **kwargs):

def __add__(self, other):
"""Concatenate."""
return self.__class__(self.value_colors + other)
return self.__class__(self.value_colors + other, keep_tags=True)

def __getitem__(self, item):
"""Retrieve character."""
try:
color_pos = self.color_index[int(item)]
except TypeError: # slice
return super(ColorStr, self).__getitem__(item)
return self.__class__(find_char_color(self.value_colors, color_pos))
return self.__class__(find_char_color(self.value_colors, color_pos), keep_tags=True)

def __iter__(self):
"""Yield one color-coded character at a time."""
Expand All @@ -94,11 +94,11 @@ def __len__(self):

def __mod__(self, other):
"""String substitution (like printf)."""
return self.__class__(self.value_colors % other)
return self.__class__(self.value_colors % other, keep_tags=True)

def __mul__(self, other):
"""Multiply string."""
return self.__class__(self.value_colors * other)
return self.__class__(self.value_colors * other, keep_tags=True)

def __repr__(self):
"""Representation of a class instance (like datetime.datetime.now())."""
Expand All @@ -118,7 +118,7 @@ def center(self, width, fillchar=None):
result = self.value_no_colors.center(width, fillchar)
else:
result = self.value_no_colors.center(width)
return self.__class__(result.replace(self.value_no_colors, self.value_colors))
return self.__class__(result.replace(self.value_no_colors, self.value_colors), keep_tags=True)

def count(self, sub, start=0, end=-1):
"""Return the number of non-overlapping occurrences of substring sub in string[start:end].
Expand Down Expand Up @@ -166,7 +166,7 @@ def decode(self, encoding=None, errors='strict'):
:param str encoding: Codec.
:param str errors: Error handling scheme.
"""
return self.__class__(super(ColorStr, self).decode(encoding, errors))
return self.__class__(super(ColorStr, self).decode(encoding, errors), keep_tags=True)

def find(self, sub, start=None, end=None):
"""Return the lowest index where substring sub is found, such that sub is contained within string[start:end].
Expand Down Expand Up @@ -236,7 +236,7 @@ def join(self, iterable):
:param iterable: Join items in this iterable.
"""
return self.__class__(super(ColorStr, self).join(iterable))
return self.__class__(super(ColorStr, self).join(iterable), keep_tags=True)

def ljust(self, width, fillchar=None):
"""Return left-justified string of length width. Padding is done using the specified fill character or space.
Expand All @@ -248,7 +248,7 @@ def ljust(self, width, fillchar=None):
result = self.value_no_colors.ljust(width, fillchar)
else:
result = self.value_no_colors.ljust(width)
return self.__class__(result.replace(self.value_no_colors, self.value_colors))
return self.__class__(result.replace(self.value_no_colors, self.value_colors), keep_tags=True)

def rfind(self, sub, start=None, end=None):
"""Return the highest index where substring sub is found, such that sub is contained within string[start:end].
Expand Down Expand Up @@ -280,7 +280,7 @@ def rjust(self, width, fillchar=None):
result = self.value_no_colors.rjust(width, fillchar)
else:
result = self.value_no_colors.rjust(width)
return self.__class__(result.replace(self.value_no_colors, self.value_colors))
return self.__class__(result.replace(self.value_no_colors, self.value_colors), keep_tags=True)

def splitlines(self, keepends=False):
"""Return a list of the lines in the string, breaking at line boundaries.
Expand Down Expand Up @@ -336,5 +336,7 @@ def zfill(self, width):
:param int width: Length of output string.
"""
if not self.value_no_colors:
return self.__class__(self.value_no_colors.zfill(width))
return self.__class__(self.value_colors.replace(self.value_no_colors, self.value_no_colors.zfill(width)))
result = self.value_no_colors.zfill(width)
else:
result = self.value_colors.replace(self.value_no_colors, self.value_no_colors.zfill(width))
return self.__class__(result, keep_tags=True)

0 comments on commit 051a6b4

Please sign in to comment.