termcinema_engine/render/
glue.rs

1//! High-level SVG renderer dispatcher (glue layer).
2//!
3//! This module routes raw textual input to appropriate SVG rendering backends
4//! based on the display mode (typing or REPL shell).
5//!
6//! It supports:
7//! - Typing-style rendering with animated cursor and per-character fade-ins;
8//! - REPL-style rendering from structured shell script blocks;
9//! - Unified interface for embedding into CLI, web, or backend use cases;
10//! - Full access to visual and behavioral configuration via `StyleSpec`,
11//!   `LayoutSpec`, `CursorSpec`, and `ControlSpec`.
12//!
13//! Used by consumers to generate complete SVG strings from raw user input.
14
15use crate::parser::parse_shell_blocks;
16use crate::render::{render_repl_svg, render_typing_svg};
17use crate::{ContentSpec, ControlSpec, CursorSpec, LayoutSpec, StyleSpec};
18
19// ─────────────── 🔗 Public Glue-layer APIs ───────────────
20
21/// Render a full SVG output based on raw text input and display mode.
22///
23/// If `from_script` is `true`, the text is interpreted as a shell session
24/// (e.g. including prompt, commands, output) and rendered in REPL style.
25/// Otherwise, it is rendered as a continuous typing animation.
26///
27/// Returns the full SVG string.
28pub fn render_svg_from_input(
29    text: &str,
30    from_script: bool,
31    style: &StyleSpec,
32    layout: &LayoutSpec,
33    cursor: &CursorSpec,
34    control: &ControlSpec,
35) -> String {
36    if from_script {
37        render_repl_from_script(text, style, layout, control)
38    } else {
39        render_typing_from_text(text, style, layout, cursor, control)
40    }
41}
42
43/// Render a typing-style SVG from raw text.
44///
45/// The entire input is treated as a single block of text to be animated
46/// character-by-character using the provided visual configuration.
47pub fn render_typing_from_text(
48    text: &str,
49    style: &StyleSpec,
50    layout: &LayoutSpec,
51    cursor: &CursorSpec,
52    control: &ControlSpec,
53) -> String {
54    let content = ContentSpec {
55        text: text.to_string(),
56    };
57    render_typing_svg(&content, style, layout, cursor, control)
58}
59
60/// Render a REPL-style SVG from shell script input.
61///
62/// The script is parsed into a sequence of commands and outputs
63/// (e.g. `prompt → command → output` groups), and each is rendered
64/// with consistent spacing and layout.
65pub fn render_repl_from_script(
66    script: &str,
67    style: &StyleSpec,
68    layout: &LayoutSpec,
69    control: &ControlSpec,
70) -> String {
71    let groups = parse_shell_blocks(script);
72    render_repl_svg(&groups, style, layout, control)
73}