tuyere 0.1.0

A powerful TUI framework based on The Elm Architecture 🔮
Documentation
//! Terminal renderer.

use crossterm::{cursor, execute, terminal};
use std::io::{self, stdout, Write};

/// Renders content to the terminal.
pub struct Renderer {
    last_render: String,
}

impl Renderer {
    /// Create a new renderer.
    pub fn new() -> Self {
        Self {
            last_render: String::new(),
        }
    }

    /// Render content to the terminal.
    pub fn render(&mut self, content: &str) -> io::Result<()> {
        // Only re-render if content changed
        if content == self.last_render {
            return Ok(());
        }

        let mut stdout = stdout();

        // Move cursor to top-left and clear screen
        execute!(
            stdout,
            cursor::MoveTo(0, 0),
            terminal::Clear(terminal::ClearType::All)
        )?;

        // Write content
        write!(stdout, "{}", content)?;
        stdout.flush()?;

        self.last_render = content.to_string();

        Ok(())
    }

    /// Clear the screen.
    pub fn clear(&mut self) -> io::Result<()> {
        let mut stdout = stdout();
        execute!(stdout, terminal::Clear(terminal::ClearType::All))?;
        self.last_render.clear();
        Ok(())
    }

    /// Get terminal size.
    #[must_use]
    pub fn size() -> (u16, u16) {
        terminal::size().unwrap_or((80, 24))
    }
}

impl Default for Renderer {
    fn default() -> Self {
        Self::new()
    }
}