Add `text_len` to `TextWrapper` for ANSI and variable character width awareness
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando l’implementazione nella PR #152702 e la discussione correlata nelle issue #142035 e #154634. Confronta il parametro text_len proposto in textwrap.wrap, fill, shorten e TextWrapper, quindi verifica che il caso d’uso della larghezza visibile e il comportamento esistente siano coperti prima di considerare concluso il lavoro.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100