render-ansi
render-ansi converts highlighted byte spans and resolved styles into ANSI/VT escaped terminal text.
What It Provides
- End-to-end helpers:
highlight_to_ansi(...) -> Stringhighlight_to_ansi_with_mode(..., ColorMode::{TrueColor|Ansi256|Ansi16}) -> Stringhighlight_lines_to_ansi_lines(...) -> Vec<String>highlight_lines_to_ansi_lines_with_mode(..., ColorMode::{TrueColor|Ansi256|Ansi16}) -> Vec<String>- High-level highlight helpers treat
normalas a base layer and fill uncovered ranges with that style. - ANSI output intentionally ignores
bgstyle fields so terminal background is not overridden.
- Incremental patching:
IncrementalRenderer::new(width, height)IncrementalRenderer::set_origin(row, col)IncrementalRenderer::set_color_mode(ColorMode::{TrueColor|Ansi256|Ansi16})IncrementalRenderer::render_patch(source, spans) -> StringIncrementalRenderer::highlight_to_patch(...) -> String
- Stream-safe single-line diff (no width/XY assumptions):
StreamLineRenderer::new()StreamLineRenderer::render_line_patch(source, spans) -> StringStreamLineRenderer::highlight_line_to_patch(...) -> String
- Low-level render helpers:
resolve_styled_spans(...)render_ansi(...)render_ansi_lines(...)
Quick Example
use Grammar;
use ;
use load_theme;
When To Use This Crate
- Use this crate when you want terminal-ready ANSI output.
- Incremental patching computes cursor columns by display width (grapheme-aware), not raw bytes.
StreamLineRendereruses relative single-line patching (CUB+ overwrite + optionalEL) and is useful when terminal width is unknown.- Tab cells are interpreted with an 8-column tab stop in the incremental path.
- Default ANSI color mode is truecolor (
38;2;r;g;b). UseAnsi256orAnsi16for constrained terminals. - If you have your own paint engine (for example native C), use
highlight-spans+theme-enginedirectly and skip ANSI rendering.
Examples
show_highlight: full-frame ANSI render for a file.zedit_bridge: machine-readable paint ops (start end fg_r fg_g fg_b bg_r bg_g bg_b flags).vt_patch_bridge: incremental VT patch output usingIncrementalRenderer.stream_line_bridge: width-independent single-line patch output usingStreamLineRenderer.
Incremental Origin Offset
If your editable text starts after a prompt (or inside a viewport sub-region), set an origin:
let mut renderer = new;
renderer.set_origin; // row 4, col 7 (1-based)
For the CLI bridge: