Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preprocess ANSI Input for --export-svg and --export-html #97

Open
mgaitan opened this issue Feb 21, 2025 · 0 comments
Open

Preprocess ANSI Input for --export-svg and --export-html #97

mgaitan opened this issue Feb 21, 2025 · 0 comments

Comments

@mgaitan
Copy link

mgaitan commented Feb 21, 2025

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! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant