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

オープン 初心者向け
#4,255 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend

調査の方向性

rich/text.py の Text.__rich_measure__ と rich/syntax.py の Syntax.__rich_measure__ から始め、それぞれの測定がレンダラーのタブサイズに応じてタブをどのように扱うかを比較します。提供された Measurement.get の例と、テーブルまたはパネルの例を使って問題を再現します。測定幅が展開後のタブを考慮し、影響を受ける出力でテキストが折り返されたり切れたりしなくなれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
Python
スター
57.8k
フォーク
2.6k
平均マージ
20分
マージ済み PR(30日)
3

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Textualize/rich のほかの issue

Textualize/rich の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。