Skip to content

gh-154686: Add text_len to TextWrapper - #152702

Open
kdeldycke wants to merge 1 commit into
python:mainfrom
kdeldycke:ANSI-aware-TextWrapper
Open

kdeldycke wants to merge 1 commit into
python:mainfrom
kdeldycke:ANSI-aware-TextWrapper

Conversation

@kdeldycke

@kdeldycke kdeldycke commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

Add a text_len parameter to textwrap.wrap(), textwrap.fill(), textwrap.shorten() and textwrap.TextWrapper. It sets how the width of a string is measured, so text with double-width or zero-width characters wraps to the right number of columns:

>>> import textwrap, unicodedata
>>> def display_width(text):
...     return sum(2 if unicodedata.east_asian_width(c) in 'FW' else 1 for c in text)
...
>>> textwrap.wrap('東京 大阪 京都 札幌', width=10)
['東京 大阪 京都', '札幌']
>>> textwrap.wrap('東京 大阪 京都 札幌', width=10, text_len=display_width)
['東京 大阪', '京都 札幌']

text_len must be additive: the width of a string is the sum of the widths of its characters. ANSI escape sequences do not follow this rule, so this PR does not touch the argparse fix of #154634.

Context

I am a maintainer of Click and had to maintain my own local fork of TextWrapper to make it strip ANSI strings properly. See my initial code at: pallets/click#3420 .

This PR started as a direct port of that code to the standard library. A width function cannot handle escape sequences, so I reworked it to focus on variable-width characters only.

This PR:

@bedevere-app

bedevere-app Bot commented Jun 30, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot

python-cla-bot Bot commented Jun 30, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Jun 30, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Jun 30, 2026
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from 4497db3 to baba0c5 Compare June 30, 2026 15:40
kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Jun 30, 2026
# Conflicts:
#	Doc/whatsnew/3.16.rst
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from baba0c5 to 36a27ac Compare June 30, 2026 16:06
kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Jun 30, 2026
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from 36a27ac to ac1a642 Compare June 30, 2026 16:07
kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Jun 30, 2026
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from ac1a642 to 090c887 Compare June 30, 2026 16:10
@kdeldycke kdeldycke changed the title gh142035: Make TextWrapper ANSI-aware gh-142035: Make TextWrapper ANSI-aware Jun 30, 2026
@read-the-docs-community

read-the-docs-community Bot commented Jun 30, 2026 •

Copy link
Copy Markdown

Comment thread Doc/library/textwrap.rst Resolved
Comment thread Lib/test/test_textwrap.py Resolved
kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Jul 15, 2026
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from 8327c94 to ba63d75 Compare July 15, 2026 23:34
kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Jul 15, 2026
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from 219db60 to 8e3c15a Compare July 15, 2026 23:45
kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Jul 25, 2026
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from 8e3c15a to 3cdc69f Compare July 25, 2026 09:49
@kdeldycke kdeldycke changed the title gh-142035: Make TextWrapper ANSI-aware gh-154686: Make TextWrapper ANSI-aware Jul 25, 2026
@kdeldycke kdeldycke changed the title gh-154686: Make TextWrapper ANSI-aware gh-154686: Add text_len to TextWrapper Jul 25, 2026
kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Jul 25, 2026
For ANSI and variable character width awareness.

Removes local patch introduced in python#154634.
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from 3cdc69f to 847d242 Compare July 25, 2026 10:19
kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Jul 25, 2026
For ANSI and variable character width awareness.

Removes local patch introduced in python#154634.
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from 847d242 to 1497de0 Compare July 25, 2026 11:47
kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Jul 31, 2026
For ANSI and variable character width awareness.

Removes local patch introduced in python#154634.
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from 4f1e56d to 27b653d Compare July 31, 2026 16:48
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Nov 6, 2026
kdeldycke added a commit to kdeldycke/cpython that referenced this pull request Nov 6, 2026
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from 27b653d to 56ea2d3 Compare November 6, 2026 16:17
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 6, 2026
@kdeldycke

kdeldycke commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

I just reworked this PR to focus on the cases of wrapping variable-width characters, add more edge-cases around these and drop all references to ANSI so I do not need to touch the argparse workaround introduced in #154634.

This PR relates to the following issues and does not try to fix them but need them for full coverage of edge-cases:

So if any of the one above get merged first I will need to rebase this one and rework it a bit.

@kdeldycke
kdeldycke requested a review from merwok November 6, 2026 16:34
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Nov 11, 2026

@sirosen sirosen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions/suggestions, but this looks good! 👍

Comment thread Lib/textwrap.py Resolved
Comment thread Lib/textwrap.py Resolved
@kdeldycke
kdeldycke force-pushed the ANSI-aware-TextWrapper branch from 56ea2d3 to 430115f Compare December 1, 2026 13:08
@kdeldycke
kdeldycke requested a review from sirosen December 1, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add text_len to TextWrapper for variable character width awareness textwrap.wrap: add control for fonts with different character widths

3 participants