`Style` keeps the ANSI codes from the first color system it renders with

Đang mở Phù hợp với người mới
#4,261 0 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
75/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
cli

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

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.