`Style` keeps the ANSI codes from the first color system it renders with
まだ誰も着手していません。
評価
調査の方向性
バグは rich/style.py の Style._make_ansi_codes() にあり、この関数は色システムを記録せずに結果を Style._ansi にキャッシュします。また Style.parse は共有のキャッシュ済みインスタンスを返します。issue の再現スニペットを truecolor、standard、256 の各色システムで異なる順序で実行し、誤ったコードが出ることを確認してください。各コンソールが自身の色システム用のコードを受け取り、異なる色システムを持つ2つのコンソールで同じスタイルを描画するテストが用意されていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Python
- スター
- 57.8k
- フォーク
- 2.6k
- 平均マージ
- 20分
- マージ済み PR(30日)
- 3
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Textualize/rich のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
Textualize/rich#4269 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Textualize/rich#4268 ·
-
Needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Textualize/rich#4267 ·
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
Textualize/rich#4266 ·
-
[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raiseオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
Textualize/rich#4265 ·
Textualize/rich の issue をすべて見る
似ている issue
-
integration: media_player
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
home-assistant/core#193344 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
daniel-ospina/tortoise#13607 ·
メンテナーはふだん 1 日以内に返信
-
documentation priority: medium
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
メンテナーはふだん 1 日以内に返信
-
needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信