[BUG] Text and Syntax measure tabs as zero width, so tables and Panel.fit wrap or cut the text
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start with Text.__rich_measure__ in rich/text.py and Syntax.__rich_measure__ in rich/syntax.py; compare how each measurement handles tabs with its renderer’s tab size. Reproduce the issue using the supplied Measurement.get example and table or panel examples. Done when measured widths account for expanded tabs and the affected output no longer wraps or cuts text.
Written by the indexing model from the issue text.
Description
- I've checked docs and closed issues for possible solutions.
- I can't find my issue in the FAQ.
Describe the bug
Tabs are expanded when text is rendered, but not when it is measured, so containers that size to their content come out too narrow:
from rich.console import Console
from rich.panel import Panel
from rich.syntax import Syntax
from rich.table import Table
console = Console(width=80)
table = Table("name", "value")
table.add_row("path\t/usr/bin", "1")
console.print(table)
console.print(Panel.fit(Syntax('func main() {\n\tfmt.Println("hello")\n}', "go")))
┏━━━━━━━━━━━━━━┳━━━━━━━┓
┃ name ┃ value ┃
┡━━━━━━━━━━━━━━╇━━━━━━━┩
│ path │ 1 │
│ /usr/bin │ │
└──────────────┴───────┘
╭──────────────────────╮
│ func main() { │
│ fmt.Println("hel │
│ } │
╰──────────────────────╯
There are 80 columns available, but the cell wraps and the Go line is cut. In a no_wrap=True column, "x\ty" becomes x ….
Measurement.get(console, console.options, Text("ab\tcd")) returns a maximum of 4, but the text renders 10 cells wide. Text.__rich_measure__ (rich/text.py) uses cell_len on the raw plain text, and Syntax.__rich_measure__ (rich/syntax.py) does the same on the raw code lines. Both renderers expand tabs (tab_size 8 for Text, 4 for Syntax).
Platform
Click to expand
Linux. Reproduced with rich 14.1.0 and master (3a109ccd), output to a plain Console(width=80).
- Dominant language
- Python
- Stars
- 57.8k
- Forks
- 2.6k
- Avg merge
- 20m
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/rich
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Textualize/rich#4269 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4268 ·
-
Needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4267 ·
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Textualize/rich#4266 ·
-
[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raiseOpen
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Textualize/rich#4265 ·
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day