rustom 0.1.0

A library for building rich, stateful terminal user interfaces and dashboards.
Documentation
use std::io::{self, Write};

use crossterm::{
    cursor::{Hide, MoveTo, Show},
    execute, queue,
    style::{
        Attribute as CtAttribute, Color as CtColor, Print, SetAttribute, SetBackgroundColor,
        SetForegroundColor,
    },
    terminal::{self, Clear, ClearType},
};

use crate::backend::Backend;
use crate::buffer::Cell;
use crate::layout::Rect;
use crate::style::{Color, Modifier};

/// A `Backend` implementation that drives the terminal through the
/// `crossterm` crate.
pub struct CrosstermBackend<W: Write> {
    writer: W,
}

impl<W: Write> CrosstermBackend<W> {
    pub fn new(writer: W) -> CrosstermBackend<W> {
        CrosstermBackend { writer }
    }
}

fn map_color(color: Color) -> CtColor {
    match color {
        Color::Reset => CtColor::Reset,
        Color::Black => CtColor::Black,
        Color::Red => CtColor::DarkRed,
        Color::Green => CtColor::DarkGreen,
        Color::Yellow => CtColor::DarkYellow,
        Color::Blue => CtColor::DarkBlue,
        Color::Magenta => CtColor::DarkMagenta,
        Color::Cyan => CtColor::DarkCyan,
        Color::Gray => CtColor::Grey,
        Color::DarkGray => CtColor::DarkGrey,
        Color::LightRed => CtColor::Red,
        Color::LightGreen => CtColor::Green,
        Color::LightYellow => CtColor::Yellow,
        Color::LightBlue => CtColor::Blue,
        Color::LightMagenta => CtColor::Magenta,
        Color::LightCyan => CtColor::Cyan,
        Color::White => CtColor::White,
        Color::Rgb(r, g, b) => CtColor::Rgb { r, g, b },
        Color::Indexed(i) => CtColor::AnsiValue(i),
    }
}

const ALL_MODIFIERS: &[(Modifier, CtAttribute)] = &[
    (Modifier::BOLD, CtAttribute::Bold),
    (Modifier::DIM, CtAttribute::Dim),
    (Modifier::ITALIC, CtAttribute::Italic),
    (Modifier::UNDERLINED, CtAttribute::Underlined),
    (Modifier::SLOW_BLINK, CtAttribute::SlowBlink),
    (Modifier::RAPID_BLINK, CtAttribute::RapidBlink),
    (Modifier::REVERSED, CtAttribute::Reverse),
    (Modifier::HIDDEN, CtAttribute::Hidden),
    (Modifier::CROSSED_OUT, CtAttribute::CrossedOut),
];

impl<W: Write> Backend for CrosstermBackend<W> {
    fn draw<'a, I>(&mut self, content: I) -> io::Result<()>
    where
        I: Iterator<Item = (u16, u16, &'a Cell)>,
    {
        let mut last_pos: Option<(u16, u16)> = None;
        let mut fg = Color::Reset;
        let mut bg = Color::Reset;
        let mut modifier = Modifier::empty();

        queue!(self.writer, SetAttribute(CtAttribute::Reset))?;

        for (x, y, cell) in content {
            if last_pos != Some((x, y)) {
                queue!(self.writer, MoveTo(x, y))?;
            }
            last_pos = Some((x + 1, y));

            if cell.modifier != modifier {
                for (flag, _attr) in ALL_MODIFIERS {
                    let was_set = modifier.contains(*flag);
                    let is_set = cell.modifier.contains(*flag);
                    if was_set && !is_set {
                        queue!(self.writer, SetAttribute(CtAttribute::Reset))?;
                        fg = Color::Reset;
                        bg = Color::Reset;
                        modifier = Modifier::empty();
                    }
                }
                for (flag, attr) in ALL_MODIFIERS {
                    if cell.modifier.contains(*flag) && !modifier.contains(*flag) {
                        queue!(self.writer, SetAttribute(*attr))?;
                    }
                }
                modifier = cell.modifier;
            }
            if cell.fg != fg {
                queue!(self.writer, SetForegroundColor(map_color(cell.fg)))?;
                fg = cell.fg;
            }
            if cell.bg != bg {
                queue!(self.writer, SetBackgroundColor(map_color(cell.bg)))?;
                bg = cell.bg;
            }
            queue!(self.writer, Print(&cell.symbol))?;
        }

        queue!(
            self.writer,
            SetForegroundColor(CtColor::Reset),
            SetBackgroundColor(CtColor::Reset),
            SetAttribute(CtAttribute::Reset)
        )?;
        Ok(())
    }

    fn hide_cursor(&mut self) -> io::Result<()> {
        execute!(self.writer, Hide)
    }

    fn show_cursor(&mut self) -> io::Result<()> {
        execute!(self.writer, Show)
    }

    fn set_cursor_position(&mut self, x: u16, y: u16) -> io::Result<()> {
        execute!(self.writer, MoveTo(x, y))
    }

    fn clear(&mut self) -> io::Result<()> {
        execute!(self.writer, Clear(ClearType::All))
    }

    fn size(&self) -> io::Result<Rect> {
        let (width, height) = terminal::size()?;
        Ok(Rect::new(0, 0, width, height))
    }

    fn flush(&mut self) -> io::Result<()> {
        self.writer.flush()
    }
}