-
Notifications
You must be signed in to change notification settings - Fork 89
Description
When exporting to SVG or HTML, rich-cli
does not interpret ANSI escape codes from piped input. Instead of converting them into styled output, it writes the raw escape sequences directly into the file. This leads to broken exports where the ANSI codes appear as literal text or cause XML parsing errors.
For example, running:
rich -p "[green]hello" | rich --export-svg hello.svg -
should generate an SVG with "hello" in green. Instead, the file contains the raw ANSI codes, making the output unusable.
A solution could be to preprocess the input and convert ANSI sequences into Rich’s internal format before rendering. Adding a flag like --preprocess-ansi
would ensure that exports correctly reflect the intended formatting.
This behavior is already possible in Python using rich.ansi.AnsiDecoder
, as shown in this minimal script:
#!/usr/bin/env python3
import sys
from rich.console import Console
from rich.ansi import AnsiDecoder
from pathlib import Path
console = Console(record=True)
decoder = AnsiDecoder()
input_text = sys.stdin.read()
for segment in decoder.decode(input_text):
console.print(segment)
Path("output.svg").write_text(console.export_svg())
A similar approach within rich-cli
would allow users to export properly formatted SVG or HTML files without losing ANSI colors. This would make exports consistent with Rich’s behavior in the terminal.
Would love to see this implemented—thanks for all the great work on rich
! 🚀