Fixed regression where reconverted ANSIStrings would lose all codes.

Made slices append trailing color codes.
This commit is contained in:
Kelketek Rritaa 2014-02-28 20:17:43 -06:00 committed by Griatch
parent 2b4bfb4433
commit 04eb057419

View file

@ -436,15 +436,24 @@ class ANSIString(unicode):
string to be handled as already decoded. It is important not to double string to be handled as already decoded. It is important not to double
decode strings, as escapes can only be respected once. decode strings, as escapes can only be respected once.
""" """
string = to_str(args[0], force_string=True) string = args[0]
if not isinstance(string, basestring): if not isinstance(string, basestring):
string = str(string) string = to_str(string, force_string=True)
parser = kwargs.get('parser', ANSI_PARSER) parser = kwargs.get('parser', ANSI_PARSER)
decoded = kwargs.get('decoded', False) or hasattr(string, '_raw_string') decoded = kwargs.get('decoded', False) or hasattr(string, '_raw_string')
if not decoded: if not decoded:
string = parser.parse_ansi(string) string = parser.parse_ansi(string)
if hasattr(string, '_clean_string'):
clean_string = string._clean_string
string = string._raw_string
else:
clean_string = unicode(parser.parse_ansi( clean_string = unicode(parser.parse_ansi(
string, strip_ansi=True), 'utf-8') string, strip_ansi=True))
if not isinstance(string, unicode):
string = string.decode('utf-8')
else:
# Do this to prevent recursive ANSIStrings.
string = unicode(string)
ansi_string = super(ANSIString, cls).__new__(ANSIString, clean_string) ansi_string = super(ANSIString, cls).__new__(ANSIString, clean_string)
ansi_string._raw_string = string ansi_string._raw_string = string
ansi_string._clean_string = clean_string ansi_string._clean_string = clean_string
@ -542,10 +551,6 @@ class ANSIString(unicode):
""" """
slice_indexes = self._char_indexes[slc] slice_indexes = self._char_indexes[slc]
# If it's the end of the string, we need to append final color codes. # If it's the end of the string, we need to append final color codes.
if self._char_indexes and self._char_indexes[-1] in slice_indexes:
append_tail = self._get_interleving(len(self))
else:
append_tail = ''
if not slice_indexes: if not slice_indexes:
return ANSIString('') return ANSIString('')
try: try:
@ -554,6 +559,7 @@ class ANSIString(unicode):
return ANSIString('') return ANSIString('')
last_mark = slice_indexes[0] last_mark = slice_indexes[0]
# Check between the slice intervals for escape sequences. # Check between the slice intervals for escape sequences.
i = None
for i in slice_indexes[1:]: for i in slice_indexes[1:]:
for index in range(last_mark, i): for index in range(last_mark, i):
if index in self._code_indexes: if index in self._code_indexes:
@ -563,6 +569,10 @@ class ANSIString(unicode):
string += self._raw_string[i] string += self._raw_string[i]
except IndexError: except IndexError:
pass pass
if slc.stop is not None:
append_tail = self._get_interleving(slc.stop)
else:
append_tail = ''
return ANSIString(string + append_tail, decoded=True) return ANSIString(string + append_tail, decoded=True)
def __getitem__(self, item): def __getitem__(self, item):