[termstr] Fixed splitting lines seperated by "\r\n".

This commit is contained in:
Andrew Hamilton 2016-02-18 17:59:42 +00:00
parent a60216cae0
commit 6eb64745b9
2 changed files with 13 additions and 3 deletions

View file

@ -171,9 +171,16 @@ class TermStr(collections.UserString):
return self._split_style(self.data.split(sep, maxsplit), len(sep))
def splitlines(self, keepends=0):
# FIX. Fails when a line seperator isn't one character in length.. \r\n
sep_length = 0 if keepends else len("\n")
return self._split_style(self.data.splitlines(keepends), sep_length)
lines_with_ends = self.data.splitlines(keepends=True)
lines_without_ends = self.data.splitlines()
result_parts = lines_with_ends if keepends else lines_without_ends
result = []
cursor = 0
for line, line_with_end in zip(result_parts, lines_with_ends):
style_part = self.style[cursor:cursor+len(line)]
result.append(self.__class__(line, style_part))
cursor += len(line_with_end)
return result
def capitalize(self):
return self.__class__(self.data.capitalize(), self.style)

View file

@ -96,8 +96,11 @@ class TermStrTests(unittest.TestCase):
self.assertEqual(foo.join(["C"]), TermStr("C"))
bar = TermStr("bar", bold_style)
self.assertEqual((foo + "\n" + bar).splitlines(), [foo, bar])
self.assertEqual((foo + "\r\n" + bar).splitlines(), [foo, bar])
self.assertEqual((foo + "\n" + bar).splitlines(keepends=True),
[TermStr("foo\n"), bar])
self.assertEqual((foo + "\r\n" + bar).splitlines(keepends=True),
[TermStr("foo\r\n"), bar])
self.assertEqual(foo.ljust(5), foo + TermStr(" "))
self.assertEqual(foo.rjust(5), TermStr(" ") + foo)
self.assertEqual(TermStr("FOO").lower(), foo)