`Style` keeps the ANSI codes from the first color system it renders with
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
- 75/100
Hướng nghiên cứu
Lỗi nằm ở Style._make_ansi_codes() trong rich/style.py, hàm này lưu kết quả vào bộ nhớ đệm Style._ansi mà không ghi nhận hệ thống màu, còn Style.parse trả về một thực thể dùng chung đã được lưu đệm. Hãy chạy đoạn mã tái tạo lỗi trong issue với các hệ thống màu truecolor, standard và 256 theo thứ tự khác nhau để thấy các mã sai. Hoàn thành khi mỗi console nhận được mã cho hệ thống màu của chính nó và có một bài kiểm thử bao phủ việc render cùng một style qua hai console với hệ thống màu khác nhau.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Hi! I hit this while writing tests that render the same markup through consoles with different color systems.
Style._make_ansi_codes() caches its result in Style._ansi, but the cache doesn't record which color system the codes were made for. Since Style.parse is cached too, every console that parses the same style string shares one Style instance, so whichever console renders it first decides the codes for all the others.
import io
from rich.console import Console
def show(color_system):
console = Console(file=io.StringIO(), color_system=color_system, force_terminal=True)
console.print("[#ff8800]orange[/]", end="")
print(f"{color_system:>9}: {console.file.getvalue()!r}")
show("truecolor")
show("standard")
show("256")
Output on Rich 15.0.0 (and main at 3a109cc):
truecolor: '\x1b[38;2;255;136;0morange\x1b[0m'
standard: '\x1b[38;2;255;136;0morange\x1b[0m'
256: '\x1b[38;2;255;136;0morange\x1b[0m'
The standard and 256-color consoles get truecolor codes. The other way round, a truecolor console that renders after a standard one gets \x1b[91m:
standard: '\x1b[91morange\x1b[0m'
truecolor: '\x1b[91morange\x1b[0m'
I expected each console to get codes for its own color system:
truecolor: '\x1b[38;2;255;136;0morange\x1b[0m'
standard: '\x1b[91morange\x1b[0m'
256: '\x1b[38;5;208morange\x1b[0m'
It shows up in practice when a program has more than one console (say, one for the terminal and one recording to a file with a different color system), or in tests that create consoles with different settings in one process.
I have a small fix that stores the color system next to the cached codes, and I'm happy to open a PR.
Python 3.13, macOS.
- 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
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của Textualize/rich
-
Color.parse() raises raw ValueError instead of ColorParseError for an empty rgb(...) componentĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Textualize/rich#4269 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Textualize/rich#4268 ·
-
Needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Textualize/rich#4267 ·
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
Textualize/rich#4266 ·
-
[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raiseĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Textualize/rich#4265 ·
Tất cả issue của Textualize/rich
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
mohanraj00/entelos#200 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Dead url in mise.tomlĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
-
sweep
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
bketelsen/truenas-mcp#77 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 86/100
sandbox-quantum/switch#2163 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
openedx/xblock-lti-consumer#765 ·
Maintainer thường phản hồi trong vòng 1 ngày