Skip to content

gh-142035: Make TextWrapper ANSI-aware#152702

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

gh-142035: Make TextWrapper ANSI-aware#152702
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

Description

Add a text_len parameter to textwrap.wrap, textwrap.fill, textwrap.shorten and textwrap.TextWrapper. It customizes how the visible width of a string is measured, so text that contains zero-width or double-width characters, or invisible ANSI escape sequences, can be wrapped correctly.

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 is a direct port of that code to Python standard library.

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 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

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33377445 | 📁 Comparing 090c887 against main (f1c5363)

  🔍 Preview build  

3 files changed
± library/textwrap.html
± whatsnew/3.16.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Argparse wrapping is bugged when colors are involved textwrap.wrap: add control for fonts with different character widths

1 participant