Make TextWrapper ANSI-aware#3420
Open
kdeldycke wants to merge 1 commit into
Open
Conversation
This was referenced May 12, 2026
5aa9cf0 to
d946074
Compare
kdeldycke
added a commit
to kdeldycke/click-extra
that referenced
this pull request
May 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug description
click.formatting.wrap_textand the underlyingclick.formatting.TextWrapperuse the Python standard library implementation inherited fromtextwrap.TextWrapper. But it is not aware of ANSI codes. Which are invisible to the user, but artificially inflate the line limit accounting used to decide when a long line of terminal text is going to be cut and put on a new line.Solution
To fix this, I copied the
textwrap.TextWrapper._wrap_chunksimplementation from the standard library and replaced the used of rawlen()by our ownclick._compat.term_len(), which strip ANSI before counting.Patching it there allow to fix the shared behavior of
wrap_textitself, but alsoHelpFormatter.write_usage,write_text,write_dl,HelpFormatter.write_textandwrite_paragraph.Python's own
_wrap_chunksis monolithic and too rigid, and couldn't find a clean way of patching it. So my last resort was to brutally copy it and change its code locally.This is I think the right thing to do in principle, as Click as its own subclass for
textwrap.TextWrapper, and already ships with the perfectclick._compat.term_len()utility for this task.Diff with Python's sydlib
For reference, here are the changes I applied compared to the inherited class:
Note that this is a minimal diff, not the exact code I added in that PR, as Click's linter and type checking impose some alternative formatting.
Context
I uncovered this issue while implementing themes in Click Extra. I had to implement a workaround there: https://github.com/kdeldycke/click-extra/blob/430ccb277bbf5e6f000b9b82d22c03db2b673318/click_extra/colorize.py#L528-L556