This commit is contained in:
Henry Hsiao 2026-02-20 18:04:38 -08:00 committed by GitHub
commit 62a76dfba9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -197,6 +197,62 @@ class ANSITextWrapper(TextWrapper):
chunks.append(" ")
return chunks
def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
"""_handle_long_word(chunks : [string],
cur_line : [string],
cur_len : int, width : int)
Handle a chunk of text (most likely a word, not whitespace) that
is too long to fit in any line.
"""
# Figure out when indent is larger than the specified width, and make
# sure at least one character is stripped off on every pass
if width < 1:
space_left = 1
else:
space_left = width - cur_len
# If we're allowed to break long words, then do so: put as much
# of the next chunk onto the current line as will fit.
if self.break_long_words:
chunk = reversed_chunks[-1]
# Calculate end using a loop against d_len
end = 1
chunk_length = len(chunk)
while end <= chunk_length:
new_chunk = chunk[:end]
d_len_new_chunk = d_len(new_chunk)
# Length is perfect
if d_len_new_chunk == space_left:
break
elif d_len_new_chunk > space_left:
end -= 1
break
end += 1
if self.break_on_hyphens and len(chunk) > space_left:
# break after last hyphen, but only if there are
# non-hyphens before it
hyphen = chunk.rfind('-', 0, space_left)
if hyphen > 0 and any(c != '-' for c in chunk[:hyphen]):
end = hyphen + 1
cur_line.append(chunk[:end])
reversed_chunks[-1] = chunk[end:]
# Otherwise, we have to preserve the long word intact. Only add
# it to the current line if there's nothing already there --
# that minimizes how much we violate the width constraint.
elif not cur_line:
cur_line.append(reversed_chunks.pop())
# If we're not allowed to break long words, and there's already
# text on the current line, do nothing. Next time through the
# main loop of _wrap_chunks(), we'll wind up here again, but
# cur_len will be zero, so the next line will be entirely
# devoted to the long word that we can't handle right now.
def _wrap_chunks(self, chunks):
"""_wrap_chunks(chunks : [string]) -> [string]