Skip to main content

rocketsplash_rt/splash/
cls_splash.rs

1// <FILE>crates/rocketsplash-rt/src/splash/cls_splash.rs</FILE>
2// <DESC>Runtime splash representation and output helpers</DESC>
3// <VERS>VERSION: 1.0.0</VERS>
4// <WCTX>Runtime library implementation</WCTX>
5// <CLOG>Add Splash type with load and output APIs</CLOG>
6
7use std::io::{self, Write};
8use std::path::Path;
9
10use rocketsplash_formats::SplashMeta;
11
12use crate::render::write_ansi;
13use crate::splash::load_splash_from_bytes;
14use crate::{ColorMode, Error, RenderBuffer};
15
16#[derive(Clone, Debug)]
17pub struct Splash {
18    pub meta: SplashMeta,
19    pub(crate) buffer: RenderBuffer,
20}
21
22impl Splash {
23    pub fn load(path: impl AsRef<Path>) -> Result<Self, Error> {
24        let bytes = std::fs::read(path)?;
25        load_splash_from_bytes(&bytes)
26    }
27
28    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
29        load_splash_from_bytes(bytes)
30    }
31
32    pub fn print(&self) {
33        let _ = self.write_with_mode(ColorMode::TrueColor, &mut io::stdout());
34    }
35
36    pub fn print_at(&self, row: u16, col: u16) {
37        let mut stdout = io::stdout();
38        let _ = write!(stdout, "\x1b[{};{}H", row, col);
39        let _ = self.write_with_mode(ColorMode::TrueColor, &mut stdout);
40    }
41
42    pub fn print_with_mode(&self, mode: ColorMode) {
43        let _ = self.write_with_mode(mode, &mut io::stdout());
44    }
45
46    pub fn to_string(&self) -> String {
47        let mut buffer = Vec::new();
48        if self
49            .write_with_mode(ColorMode::TrueColor, &mut buffer)
50            .is_ok()
51        {
52            return String::from_utf8_lossy(&buffer).into_owned();
53        }
54        String::new()
55    }
56
57    pub fn write_to<W: Write>(&self, w: &mut W) -> io::Result<()> {
58        self.write_with_mode(ColorMode::TrueColor, w)
59    }
60
61    pub fn dimensions(&self) -> (usize, usize) {
62        (self.buffer.width, self.buffer.height)
63    }
64
65    fn write_with_mode<W: Write>(&self, mode: ColorMode, w: &mut W) -> io::Result<()> {
66        write_ansi(&self.buffer, mode, w)
67    }
68}
69
70// <FILE>crates/rocketsplash-rt/src/splash/cls_splash.rs</FILE>
71// <VERS>END OF VERSION: 1.0.0</VERS>