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

Đang mở Phù hợp với người mới
#4,255 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu với Text.__rich_measure__ trong rich/text.py và Syntax.__rich_measure__ trong rich/syntax.py; so sánh cách mỗi phép đo xử lý tab theo kích thước tab của bộ kết xuất tương ứng. Tái hiện sự cố bằng ví dụ Measurement.get được cung cấp và các ví dụ bảng hoặc panel. Hoàn tất khi độ rộng được đo tính đến các tab đã mở rộng và đầu ra bị ảnh hưởng không còn ngắt dòng hoặc cắt mất văn bản.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Python
Star
57.8k
Fork
2.6k
Merge trung bình
20 phút
Pull request đã merge (30 ngày)
3

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Textualize/rich

Tất cả issue của Textualize/rich

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.