[BUG] Text.right_crop with a negative amount corrupts text length

Aperta Adatta ai principianti
#4,256 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
66/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia da Text.right_crop() e dai relativi test nel repository Rich; l’issue non indica alcun file o test, quindi individuali cercando right_crop. Verifica come devono essere gestiti i valori negativi e confronta il testo e la lunghezza memorizzata nella cache prima e dopo il taglio. Il lavoro è completato quando un valore negativo non può lasciare il testo e la relativa lunghezza o gli spans in uno stato incoerente, con un test di regressione che copra il comportamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

  • I've checked docs and searched open and closed issues for possible solutions.
  • I can't find my issue in the FAQ.

Describe the bug

Text.right_crop() behaves unexpectedly for a negative amount: it removes characters from the start of the text while increasing the cached length, leaving the Text object internally inconsistent.

Minimal reproduction:

from rich.text import Text

text = Text(abc)
text.right_crop(-1)
print(repr(text.plain), len(text))

Observed output:

'a' 4

A value of -4 leaves plain == abc but reports len(text) == 7. The implementation calculates self.plain[:-amount] and subtracts amount from the cached length, so negative values both slice from the wrong end and increase the cached length. Subsequent operations that rely on the cached length or span offsets can then behave incorrectly.

Expected behavior: a negative crop amount should not silently mutate the text or invalidate its length/spans. Rejecting negative values with ValueError would make the invalid input explicit; please advise if a different contract is preferred.

This report and reproduction were prepared with GitHub Copilot assistance; the example was run locally against the current checkout.

Platform

Click to expand

Windows, PowerShell. The behavior is in Text's internal string/length bookkeeping and does not depend on terminal rendering.

Lingua principale
Python
Stelle
57.8k
Fork
2.6k
Merge medio
20m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/rich

Tutte le issue di Textualize/rich

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.