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 #142035 , which introduced a local patch in _split_lines via #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
Feature or enhancement
Proposal:
This is a proposal to add a
text_lenparameter totextwrap.wrap,textwrap.fill,textwrap.shortenandtextwrap.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:
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 #142035 , which introduced a local patch in
_split_linesvia #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
text_lentoTextWrapper#152702