termcinema_cli/render/
mod.rs

1//! Rendering orchestration module for `termcinema`.
2//!
3//! This module coordinates the full rendering pipeline,
4//! including input resolution, theme application, layout generation,
5//! and SVG rendering. It supports both CLI and SDK usage.
6//!
7//! It includes:
8//! - Input loading from `--input`, `--script`, or stdin
9//! - Escape sequence processing (unless `--no-escape`)
10//! - Theme resolution with CLI patching
11//! - Layout construction from dimensions and alignment
12//! - Final SVG rendering via `termcinema-engine`
13//!
14//! For SDKs and embedders, [`render_svg_direct`] provides a simplified entrypoint.
15
16mod font;
17mod handle;
18mod input;
19mod output;
20mod text;
21mod theme;
22
23pub(crate) use font::print_font_families;
24pub(crate) use input::load_input;
25pub(crate) use output::write_svg;
26pub(crate) use text::resolve_plain_text;
27pub(crate) use theme::{print_theme_list, resolve_and_patch_theme};
28
29#[allow(unused_imports)]
30pub use crate::render::theme::available_theme_names;
31
32use crate::args::CliArgs;
33use crate::render::handle::{handle_render, prepare_svg};
34use std::process::exit;
35
36/// Entrypoint for CLI execution.
37///
38/// Called by the `main.rs` binary. Delegates to [`handle_render`] to
39/// run the complete rendering pipeline: input → theme/layout → SVG → output.
40///
41/// Prints any fatal error to stderr and exits with code 1.
42#[allow(dead_code)]
43pub(crate) fn run(args: CliArgs) {
44    if let Err(e) = handle_render(&args) {
45        eprintln!("❌ {}", e);
46        exit(1);
47    }
48}
49
50/// SDK-friendly rendering entrypoint.
51///
52/// This function is designed for programmatic use, such as:
53/// - WASM bindings
54/// - Python FFI
55/// - Embedded browser demos
56///
57/// It skips all CLI-specific input logic (stdin, `--input`, `--script`),
58/// and directly renders an SVG from a raw input string + [`CliArgs`] config.
59///
60/// Script mode is disabled internally, and only layout/style-related fields are respected.
61///
62/// # Arguments
63/// - `raw`: Raw input text (e.g. `"echo hello\\nworld"`)
64/// - `args`: CLI-style arguments (theme, font, layout, etc)
65///
66/// # Returns
67/// - `Ok(svg_string)` on success
68/// - `Err(...)` if any step fails (invalid theme, layout error, etc)
69#[allow(dead_code)]
70pub fn render_svg_direct(raw: &str, args: &CliArgs) -> anyhow::Result<String> {
71    prepare_svg(raw, false, args)
72}