Add `text_len` to `TextWrapper` for ANSI and variable character width awareness
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
Feature or enhancement
Proposal:
This is a proposal to add a text_len parameter to textwrap.wrap, textwrap.fill, textwrap.shorten and textwrap.TextWrapper.
That way we can teach these functions how to measure the length of visible text.
The main use-case is ignoring ANSI sequences like so:
>>> import re, textwrap
>>> visible_len = lambda s: len(re.sub(r'\x1b\[[0-9;]*m', '', s))
>>> colored = 'normal \x1b[31mcolored\x1b[0m words here'
>>> lines = textwrap.wrap(colored, width=14, text_len=visible_len)
>>> [re.sub(r'\x1b\[[0-9;]*m', '', line) for line in lines]
['normal colored', 'words here']
This allow flexibility without having to maintain or introduce full ANSI awareness to all the string-manipulating utilities.
And I am pretty sure some users can come up with creative usage other than ANSI, and it provides a future escape-hatch.
An implementation is available at #152702
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
This is a direct follow up on https://github.com/python/cpython/issues/142035 , which introduced a local patch in _split_lines via https://github.com/python/cpython/pull/154634 to address that bug. A new issue has been requested by a project member to track the generalization of that local patch.
Linked PRs
- gh-152702
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先查看 PR #152702 中的實作,以及 issues #142035 和 #154634 中的相關討論。比較 textwrap.wrap、fill、shorten 和 TextWrapper 中提議的 text_len 參數,然後確認可見寬度使用情境和現有行為都已涵蓋,再考慮將工作視為完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100