Preprocess ANSI Input for `--export-svg` and `--export-html`

Open
#97 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
cli

Research direction

Trace how rich-cli handles piped input and the --export-svg and --export-html options, then compare that path with the provided rich.ansi.AnsiDecoder example. Verify the change with the documented shell command and confirm that ANSI-colored input produces styled, parseable SVG and HTML rather than literal escape sequences.

Written by the indexing model from the issue text.

Description

enhancement

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

Dominant language
Python
Stars
3.7k
Forks
117
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/rich-cli

All issues in Textualize/rich-cli

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.