`Style` keeps the ANSI codes from the first color system it renders with
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 75/100
Línea de trabajo
El error está en Style._make_ansi_codes() en rich/style.py, que guarda su resultado en caché en Style._ansi sin registrar el sistema de colores, y Style.parse devuelve una instancia compartida en caché. Ejecuta el fragmento de reproducción del issue con los sistemas de color truecolor, standard y 256 en distinto orden para ver los códigos incorrectos. Listo cuando cada consola obtenga códigos para su propio sistema de color y una prueba cubra el renderizado de un mismo estilo en dos consolas con sistemas de color distintos.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 57.8k
- Forks
- 2.6k
- Merge medio
- 20 min
- PR fusionados (30 d)
- 3
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de Textualize/rich
-
Color.parse() raises raw ValueError instead of ColorParseError for an empty rgb(...) componentAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
Textualize/rich#4269 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Textualize/rich#4268 ·
-
Needs triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Textualize/rich#4267 ·
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 88/100
Textualize/rich#4266 ·
-
[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raiseAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Textualize/rich#4265 ·
Todos los issues de Textualize/rich
Issues similares
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskAbierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 86/100
sandbox-quantum/switch#2163 ·
Los mantenedores suelen responder en 1 día
-
bug pydanty:is-working
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
pydantic/pydantic-ai#13939 ·
Los mantenedores suelen responder en 1 día
-
priority: low python
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
LearningCircuit/local-deep-research#8849 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 86/100
francescopace/espectre#223 ·
Los mantenedores suelen responder en 3 días
-
good first issue Status: Triaged
Dificultad 1/5 Menos de una hora Aptitud para principiantes 84/100
canonical/imagecraft#538 · 1 comentario ·
Los mantenedores suelen responder en 2 días