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

Open Beginner friendly
#4,256 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
66/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start with Text.right_crop() and its tests in the Rich repository; the issue does not name a file or test, so locate them by searching for right_crop. Check how negative amounts should be handled and compare the text and cached length before and after cropping. Done means a negative amount cannot leave the text and its length or spans inconsistent, with a regression test covering the behavior.

Written by the indexing model from the issue text.

Description

  • 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.

Dominant language
Python
Stars
57.8k
Forks
2.6k
Avg merge
20m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/rich

All issues in Textualize/rich

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.