wtrenderlib 0.1.0

Dependency-free terminal rendering primitives for RGB and RGBA frames
Documentation
mod iterm2;
mod kitty;
mod passthrough;
#[cfg(feature = "sixel")]
mod sixel;
mod unicode;

pub use iterm2::{Iterm2Options, render_iterm2};
pub use kitty::{KittyOptions, render_kitty};
pub use passthrough::{Multiplexer, write_passthrough};
#[cfg(feature = "sixel")]
pub use sixel::render_sixel;
pub use unicode::{QuadrantOptions, UnicodeOptions, render_unicode, render_unicode_quadrant};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackendKind {
    Unicode,
    Kitty,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Backend {
    Unicode(UnicodeOptions),
    Kitty(KittyOptions),
}

pub struct Renderer<W> {
    output: W,
    backend: Backend,
}

impl<W> Renderer<W> {
    pub fn new(output: W, backend: Backend) -> Self {
        Self { output, backend }
    }

    pub fn backend(&self) -> BackendKind {
        match self.backend {
            Backend::Unicode(_) => BackendKind::Unicode,
            Backend::Kitty(_) => BackendKind::Kitty,
        }
    }

    pub fn into_inner(self) -> W {
        self.output
    }
}

impl<W: std::io::Write> Renderer<W> {
    pub fn render(&mut self, frame: crate::FrameView<'_>) -> crate::Result<()> {
        match self.backend {
            Backend::Unicode(options) => render_unicode(frame, &mut self.output, options),
            Backend::Kitty(options) => render_kitty(frame, &mut self.output, options),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{FrameView, PixelFormat, RenderOptions};

    #[test]
    fn dispatches_to_selected_backend() {
        let frame = FrameView::new(&[255, 0, 0], 1, 1, 3, PixelFormat::Rgb8).unwrap();
        let mut renderer = Renderer::new(
            Vec::new(),
            Backend::Unicode(UnicodeOptions {
                render_options: RenderOptions::new(1, 1).unwrap(),
                hide_cursor: false,
            }),
        );
        renderer.render(frame).unwrap();
        assert!(renderer.into_inner().starts_with(b"\x1b[38;2;"));
    }

    struct FailingWriter;

    impl std::io::Write for FailingWriter {
        fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> {
            Err(std::io::Error::new(
                std::io::ErrorKind::BrokenPipe,
                "closed",
            ))
        }

        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }

    #[test]
    fn propagates_output_errors() {
        let frame = FrameView::new(&[255, 0, 0], 1, 1, 3, PixelFormat::Rgb8).unwrap();
        let mut renderer = Renderer::new(
            FailingWriter,
            Backend::Unicode(UnicodeOptions {
                render_options: RenderOptions::new(1, 1).unwrap(),
                hide_cursor: false,
            }),
        );
        assert!(matches!(
            renderer.render(frame),
            Err(crate::Error::Io {
                kind: std::io::ErrorKind::BrokenPipe,
                ..
            })
        ));
    }
}