rae 0.1.10

Renderer-neutral widget, layout, sanitization, and GLSL shader primitives for Rust desktop tools.
Documentation
use rae::{
    materialize_output, sanitize_title, strip_ansi_escapes, CodeTokenKind, OutputOptions,
    OutputRow, SanitizePolicy,
};

fn main() {
    let assistant_output = r#"# Patch

Here is a minimal safe code output. The incoming text can contain ANSI like red or OSC links.

```rust
pub fn greet(name: &str) -> String {
    // renderer gets sanitized tokens, not raw terminal escapes
    format!("hello {name}")
}
```

- Strip ANSI before rendering.
- Preserve copy-safe fenced code.
- Keep line numbers and syntax token classes renderer-neutral.
"#;

    let options = OutputOptions::for_width(72).with_code_line_numbers(true);
    let doc = materialize_output(assistant_output, options);

    println!("safe output model");
    println!("removed escape sequences: {}", doc.removed_escape_sequences);
    println!("removed control chars: {}", doc.removed_control_chars);
    println!("removed zero-width chars: {}", doc.removed_zero_width_chars);
    println!("truncated: {}", doc.truncated);
    println!();

    for row in &doc.rows {
        match row {
            OutputRow::Heading { level, text } => println!("H{level}: {text}"),
            OutputRow::Paragraph { text, continuation } => {
                println!("P{}: {text}", if *continuation { "+" } else { " " })
            }
            OutputRow::Bullet { text, continuation } => {
                println!("B{}: {text}", if *continuation { "+" } else { " " })
            }
            OutputRow::CodeFenceStart { language } => println!("CODE {language}"),
            OutputRow::Code {
                gutter,
                text,
                tokens,
                ..
            } => {
                let classes = tokens
                    .iter()
                    .filter(|token| token.kind != CodeTokenKind::Text)
                    .map(|token| format!("{:?}:{}", token.kind, token.text))
                    .collect::<Vec<_>>()
                    .join(", ");
                println!("  {} {text}", gutter.as_deref().unwrap_or(""));
                if !classes.is_empty() {
                    println!("      tokens: {classes}");
                }
            }
            OutputRow::CodeFenceEnd => println!("END CODE"),
            OutputRow::Quote { text, .. } => println!("Q: {text}"),
            OutputRow::Blank => println!(),
        }
    }

    println!("\ncopy-safe markdown:\n{}", doc.copy_text);

    let title = sanitize_title("build\u{202e} output \u{1b}[2J", 24);
    println!("safe title: {title}");

    let stripped = strip_ansi_escapes("\u{1b}]8;;https://example.com\u{7}link\u{1b}]8;;\u{7}");
    println!(
        "osc stripped: {:?}, removed={}",
        stripped.text, stripped.removed_escape_sequences
    );

    let strict = SanitizePolicy::code_output(32, 3);
    let strict_doc = materialize_output(
        assistant_output,
        OutputOptions::for_width(40).with_max_rows(6),
    );
    println!(
        "strict policy width={} max_lines={} rendered_rows={}",
        strict.max_line_width,
        strict.max_lines,
        strict_doc.rows.len()
    );
}