- # the current character is a newline, so reset the relative
- # position (start a new line)
- elif each_character == "\n":
- rel_pos = 0
- last_whitespace = abs_pos
-
- # the current character meets the requested maximum line width,
- # so we need to backtrack and find a space at which to wrap;
- # special care is taken to avoid an off-by-one in case the
- # current character is a double-width glyph
- elif each_character != "\r" and (
- rel_pos >= width or (
- rel_pos >= width - 1 and glyph_columns(
- each_character
- ) == 2
- )
- ):
-
- # it's always possible we landed on whitespace
- if unicodedata.category(each_character) in ("Cc", "Zs"):
- last_whitespace = abs_pos
-
- # insert an eol in place of the space
- text = text[:last_whitespace] + "\r\n" + text[last_whitespace + 1:]
+ # track the most recent whitespace we've seen
+ # TODO(fungi) exclude non-breaking spaces (\x0a)
+ elif unicodedata.category(each_character) in ("Cc", "Zs"):
+ if each_character == "\n":
+ # the current character is a newline, so reset the relative
+ # position too (start a new line)
+ rel_pos = 0
+ if each_character != "\r":
+ # the current character is not a carriage return, so mark it as
+ # whitespace (we don't want to break and wrap between CR+LF)
+ last_abs_whitespace = abs_pos
+ last_rel_whitespace = rel_pos
+
+ # the current character meets the requested maximum line width, so we
+ # need to wrap unless the current word is wider than the terminal (in
+ # which case we let it do the wrapping instead)
+ if last_rel_whitespace != 0 and (rel_pos > width or (
+ rel_pos > width - 1 and glyph_columns(each_character) == 2)):
+
+ # insert an eol in place of the last space
+ text = (text[:last_abs_whitespace] + "\r\n" +
+ text[last_abs_whitespace + 1:])