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