Skip to content

Commit a2cb29e

Browse files
committed
Fix failing wcwidth "wide character" tests by reverting bad ANSI code
1 parent 74885be commit a2cb29e

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

tabulate/__init__.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,29 +2738,45 @@ def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
27382738

27392739
# If we're allowed to break long words, then do so: put as much
27402740
# of the next chunk onto the current line as will fit.
2741-
if self.break_long_words:
2741+
2742+
# Reverted the broken ANSI code handling stuff to fix wcwidth handling
2743+
# - Doesn't use self._lend, infinite loops
2744+
# - doesn't locate chunks correctly b/c could be split by ANSI codes
2745+
#
2746+
# if self.break_long_words and space_left > 0:
2747+
# # Tabulate Custom: Build the string up piece-by-piece in order to
2748+
# # take each charcter's width into account
2749+
# chunk = reversed_chunks[-1]
2750+
# # Only count printable characters, so strip_ansi first, index later.
2751+
# for i in range( 1, space_left + 1 ):
2752+
# if self._len(_strip_ansi(chunk)[:i]) > space_left:
2753+
# break
2754+
#
2755+
# # Consider escape codes when breaking words up
2756+
# total_escape_len = 0
2757+
# last_group = 0
2758+
# if _ansi_codes.search(chunk) is not None:
2759+
# for group, _, _, _ in _ansi_codes.findall(chunk):
2760+
# escape_len = len(group)
2761+
# if (
2762+
# group
2763+
# in chunk[last_group : i + total_escape_len + escape_len - 1]
2764+
# ):
2765+
# total_escape_len += escape_len
2766+
# found = _ansi_codes.search(chunk[last_group:])
2767+
# last_group += found.end()
2768+
# cur_line.append(chunk[: i + total_escape_len - 1])
2769+
# reversed_chunks[-1] = chunk[i + total_escape_len - 1 :]
2770+
2771+
if self.break_long_words: # and space_left > 0:
27422772
# Tabulate Custom: Build the string up piece-by-piece in order to
27432773
# take each charcter's width into account
27442774
chunk = reversed_chunks[-1]
27452775
i = 1
2746-
# Only count printable characters, so strip_ansi first, index later.
2747-
while len(_strip_ansi(chunk)[:i]) <= space_left:
2776+
while self._len(chunk[:i]) <= space_left:
27482777
i = i + 1
2749-
# Consider escape codes when breaking words up
2750-
total_escape_len = 0
2751-
last_group = 0
2752-
if _ansi_codes.search(chunk) is not None:
2753-
for group, _, _, _ in _ansi_codes.findall(chunk):
2754-
escape_len = len(group)
2755-
if (
2756-
group
2757-
in chunk[last_group : i + total_escape_len + escape_len - 1]
2758-
):
2759-
total_escape_len += escape_len
2760-
found = _ansi_codes.search(chunk[last_group:])
2761-
last_group += found.end()
2762-
cur_line.append(chunk[: i + total_escape_len - 1])
2763-
reversed_chunks[-1] = chunk[i + total_escape_len - 1 :]
2778+
cur_line.append(chunk[: i - 1])
2779+
reversed_chunks[-1] = chunk[i - 1 :]
27642780

27652781
# Otherwise, we have to preserve the long word intact. Only add
27662782
# it to the current line if there's nothing already there --

0 commit comments

Comments
 (0)