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

Aperta Adatta ai principianti
#4,261 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
cli

Direzione di ricerca

Il bug si trova in Style._make_ansi_codes() in rich/style.py, che memorizza nella cache il suo risultato in Style._ansi senza registrare il sistema di colori, e Style.parse restituisce un'istanza condivisa in cache. Esegui lo snippet di riproduzione dell'issue con i sistemi di colori truecolor, standard e 256 in ordini diversi per vedere i codici errati. Completato quando ogni console riceve i codici per il proprio sistema di colori e un test copre il rendering di uno stesso stile attraverso due console con sistemi di colori diversi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
57.8k
Fork
2.6k
Merge medio
20m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/rich

Tutte le issue di Textualize/rich

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.