rocketsplash-rt 0.3.0

Runtime library for loading and rendering Rocketsplash assets (.rst, .rsf)
Documentation
// <FILE>crates/rocketsplash-rt/src/splash/cls_splash.rs</FILE>
// <DESC>Runtime splash representation and output helpers</DESC>
// <VERS>VERSION: 2.0.0</VERS>
// <WCTX>Foundation crates 0.3.0 (RELEASE_PLAN F2 cycle-break)</WCTX>
// <CLOG>2.0.0: Splash::load/from_bytes moved beside the loader (fnc_load_splash.rs) so this file stays a leaf. 1.2.1: clippy zero-warning pass (P0, auto-fix). 1.2.0: replace inherent to_string with a std::fmt::Display impl. 1.1.0: add buffer() accessor for cross-crate cell iteration. 1.0.0: initial Splash type.</CLOG>

use std::io::{self, Write};

use rocketsplash_formats::SplashMeta;

use crate::render::write_ansi;
use crate::{ColorMode, RenderBuffer};

#[derive(Clone, Debug)]
pub struct Splash {
    pub meta: SplashMeta,
    pub(crate) buffer: RenderBuffer,
}

impl Splash {
    pub fn print(&self) {
        let _ = self.write_with_mode(ColorMode::TrueColor, &mut io::stdout());
    }

    pub fn print_at(&self, row: u16, col: u16) {
        let mut stdout = io::stdout();
        let _ = write!(stdout, "\x1b[{};{}H", row, col);
        let _ = self.write_with_mode(ColorMode::TrueColor, &mut stdout);
    }

    pub fn print_with_mode(&self, mode: ColorMode) {
        let _ = self.write_with_mode(mode, &mut io::stdout());
    }

    pub fn write_to<W: Write>(&self, w: &mut W) -> io::Result<()> {
        self.write_with_mode(ColorMode::TrueColor, w)
    }

    pub fn dimensions(&self) -> (usize, usize) {
        (self.buffer.width, self.buffer.height)
    }

    /// Borrow the underlying [`RenderBuffer`] so callers can iterate cells
    /// directly — for example to blit a rocketsplash asset into a
    /// `tui-vfx-types::Grid`, a `ratatui::Buffer`, or any other cell
    /// surface. Each cell carries the rendered character, optional fg/bg
    /// colors, text styling, and opacity produced by the splash loader.
    ///
    /// Prefer the higher-level `print`/`print_at`/`write_to` helpers when
    /// you just need ANSI output; reach for `buffer()` when you need
    /// cell-level control (composition, filtering, masking, layering on
    /// top of another grid).
    pub fn buffer(&self) -> &RenderBuffer {
        &self.buffer
    }

    fn write_with_mode<W: Write>(&self, mode: ColorMode, w: &mut W) -> io::Result<()> {
        write_ansi(&self.buffer, mode, w)
    }
}

impl std::fmt::Display for Splash {
    /// Renders the splash as a TrueColor ANSI string (same output previously
    /// returned by the inherent `to_string`); yields an empty string on write failure.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut buffer = Vec::new();
        if self
            .write_with_mode(ColorMode::TrueColor, &mut buffer)
            .is_ok()
        {
            f.write_str(&String::from_utf8_lossy(&buffer))
        } else {
            Ok(())
        }
    }
}

// <FILE>crates/rocketsplash-rt/src/splash/cls_splash.rs</FILE>
// <VERS>END OF VERSION: 1.2.1</VERS>