Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix column wrapping breaking ANSI escape codes (fixes #307) #308

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

devdanzin
Copy link

In tabulate._CustomTextWrap._handle_long_word, ANSI escape codes may be broken in the middle, resulting wrongly formatted tables. That happens because the length of the string is checked in while self._len(chunk[:i]) <= space_left:, which may strip ANSI codes after they were broken by indexing into their middle.

To solve that, we instead calculate the length of the string without ANSI codes, then index into it: while len(_strip_ansi(chunk)[:i]) <= space_left:. That gives the amount of printable characters that should be included. Then we iterate through the escape codes present until we reach that many printable characters, and add the escape code lengths to figure out how much of the original string should be included.

Tests included.

Test code:

import tabulate
print(tabulate.tabulate(tabular_data=(('012345 (\x1b[32mabcdefghij\x1b[0m)', 'XX'),), maxcolwidths=11, tablefmt='grid'))"

Result before patch:

+-------------+----+
| 012345 ( XX |
| 2mabcdefghi |    |
| j)          |    |
+-------------+----+

broken_tabulate-color_wrap

Result after patch:

+-------------+----+
| 012345 (abc | XX |
| defghij)    |    |
+-------------+----+

fixed_tabulate_color_wrap

Fixes #307.

@devdanzin devdanzin deleted the branch astanin:master March 4, 2024 10:46
@devdanzin devdanzin closed this Mar 4, 2024
@devdanzin devdanzin deleted the master branch March 4, 2024 10:46
@devdanzin devdanzin restored the master branch March 6, 2024 10:46
@devdanzin devdanzin reopened this Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Column wrapping may break ANSI escape codes
1 participant