[BUG] Text and Syntax measure tabs as zero width, so tables and Panel.fit wrap or cut the text

Open Beginner friendly
#4,255 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

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

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

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.