rocketsplash-rt 0.2.2

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: 1.1.0</VERS>
// <WCTX>Expose the underlying RenderBuffer so cross-crate consumers (tui-vfx, gt-design) can walk cells and blit into their own compositing grids.</WCTX>
// <CLOG>1.1.0: add `pub fn buffer(&self) -> &RenderBuffer` accessor for cross-crate cell iteration.
// 1.0.0: initial; Splash type with load and output APIs.</CLOG>

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

use rocketsplash_formats::SplashMeta;

use crate::render::write_ansi;
use crate::splash::load_splash_from_bytes;
use crate::{ColorMode, Error, RenderBuffer};

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

impl Splash {
    pub fn load(path: impl AsRef<Path>) -> Result<Self, Error> {
        let bytes = std::fs::read(path)?;
        load_splash_from_bytes(&bytes)
    }

    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
        load_splash_from_bytes(bytes)
    }

    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 to_string(&self) -> String {
        let mut buffer = Vec::new();
        if self
            .write_with_mode(ColorMode::TrueColor, &mut buffer)
            .is_ok()
        {
            return String::from_utf8_lossy(&buffer).into_owned();
        }
        String::new()
    }

    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)
    }
}

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