mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-26 04:39:26 +01:00
utils.str: Rely only on the documented methods of textwrap.TextWrapper.
'_split_chunks()' should be fine, but '_wrap_chunks()' is the only one explicitly documented as overridable, so it's probably safer to use.
This commit is contained in:
parent
91a4083ae1
commit
41c5589bea
@ -320,31 +320,28 @@ def splitBytes(word, size):
|
|||||||
return (word[0:size-i], word[size-i:])
|
return (word[0:size-i], word[size-i:])
|
||||||
assert False, (word, size)
|
assert False, (word, size)
|
||||||
|
|
||||||
def byteTextWrap(text, size, break_on_hyphens=False):
|
|
||||||
"""Similar to textwrap.wrap(), but considers the size of strings (in bytes)
|
class ByteTextWrapper(textwrap.TextWrapper):
|
||||||
instead of their length (in characters)."""
|
def _wrap_chunks(self, words):
|
||||||
try:
|
|
||||||
words = textwrap.TextWrapper()._split_chunks(text)
|
|
||||||
except AttributeError: # Python 2
|
|
||||||
words = textwrap.TextWrapper()._split(text)
|
|
||||||
words.reverse() # use it as a stack
|
words.reverse() # use it as a stack
|
||||||
if sys.version_info[0] >= 3:
|
|
||||||
words = [w.encode() for w in words]
|
words = [w.encode() for w in words]
|
||||||
lines = [b'']
|
lines = [b'']
|
||||||
while words:
|
while words:
|
||||||
word = words.pop(-1)
|
word = words.pop(-1)
|
||||||
if len(word) > size:
|
if len(word) > self.width:
|
||||||
(before, after) = splitBytes(word, size)
|
(before, after) = splitBytes(word, self.width)
|
||||||
words.append(after)
|
words.append(after)
|
||||||
word = before
|
word = before
|
||||||
if len(lines[-1]) + len(word) <= size:
|
if len(lines[-1]) + len(word) <= self.width:
|
||||||
lines[-1] += word
|
lines[-1] += word
|
||||||
else:
|
else:
|
||||||
lines.append(word)
|
lines.append(word)
|
||||||
if sys.version_info[0] >= 3:
|
|
||||||
return [l.decode() for l in lines]
|
return [l.decode() for l in lines]
|
||||||
else:
|
|
||||||
return lines
|
def byteTextWrap(text, size, break_on_hyphens=False):
|
||||||
|
"""Similar to textwrap.wrap(), but considers the size of strings (in bytes)
|
||||||
|
instead of their length (in characters)."""
|
||||||
|
return ByteTextWrapper(width=size).wrap(text)
|
||||||
|
|
||||||
def commaAndify(seq, comma=',', And=None):
|
def commaAndify(seq, comma=',', And=None):
|
||||||
"""Given a a sequence, returns an English clause for that sequence.
|
"""Given a a sequence, returns an English clause for that sequence.
|
||||||
|
Loading…
Reference in New Issue
Block a user