[termstr] Fixed splitting lines seperated by "\r\n".
This commit is contained in:
parent
a60216cae0
commit
6eb64745b9
2 changed files with 13 additions and 3 deletions
13
termstr.py
13
termstr.py
|
|
@ -171,9 +171,16 @@ class TermStr(collections.UserString):
|
||||||
return self._split_style(self.data.split(sep, maxsplit), len(sep))
|
return self._split_style(self.data.split(sep, maxsplit), len(sep))
|
||||||
|
|
||||||
def splitlines(self, keepends=0):
|
def splitlines(self, keepends=0):
|
||||||
# FIX. Fails when a line seperator isn't one character in length.. \r\n
|
lines_with_ends = self.data.splitlines(keepends=True)
|
||||||
sep_length = 0 if keepends else len("\n")
|
lines_without_ends = self.data.splitlines()
|
||||||
return self._split_style(self.data.splitlines(keepends), sep_length)
|
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):
|
def capitalize(self):
|
||||||
return self.__class__(self.data.capitalize(), self.style)
|
return self.__class__(self.data.capitalize(), self.style)
|
||||||
|
|
|
||||||
|
|
@ -96,8 +96,11 @@ class TermStrTests(unittest.TestCase):
|
||||||
self.assertEqual(foo.join(["C"]), TermStr("C"))
|
self.assertEqual(foo.join(["C"]), TermStr("C"))
|
||||||
bar = TermStr("bar", bold_style)
|
bar = TermStr("bar", bold_style)
|
||||||
self.assertEqual((foo + "\n" + bar).splitlines(), [foo, bar])
|
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),
|
self.assertEqual((foo + "\n" + bar).splitlines(keepends=True),
|
||||||
[TermStr("foo\n"), bar])
|
[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.ljust(5), foo + TermStr(" "))
|
||||||
self.assertEqual(foo.rjust(5), TermStr(" ") + foo)
|
self.assertEqual(foo.rjust(5), TermStr(" ") + foo)
|
||||||
self.assertEqual(TermStr("FOO").lower(), foo)
|
self.assertEqual(TermStr("FOO").lower(), foo)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue