little-kitty 0.0.3

A low-level interface for the Kitty Graphics Protocol
Documentation
use super::writer::*;

use std::io::*;

//
// PlaceholderPrinter
//

/// Kitty graphics protocol placeholder printer.
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct PlaceholderPrinter {
    /// Image ID.
    ///
    /// The ID range depends on the terminal color range, e.g. 8 bits for 256 color mode and 24
    /// bits for true color mode.
    pub id: usize,

    /// Absolute position (column, row).
    ///
    /// [None] implies the current cursor position. If position is [Some] will restore the current
    /// cursor position after printing.
    pub position: Option<(usize, usize)>,

    /// Background color.
    ///
    /// See [chart for Xterm](https://www.ditig.com/256-colors-cheat-sheet).
    ///
    /// [None] implies transparency.
    pub background_color: Option<usize>,
}

impl PlaceholderPrinter {
    /// Constructor.
    pub fn new(id: usize) -> Self {
        Self { id, background_color: None, position: None }
    }

    /// Set background color.
    pub fn set_background_color(&mut self, background_color: usize) {
        self.background_color = Some(background_color);
    }

    /// Set background color.
    ///
    /// Chainable.
    pub fn with_background_color(mut self, background_color: usize) -> Self {
        self.set_background_color(background_color);
        self
    }

    /// Set position.
    pub fn set_position(&mut self, x: usize, y: usize) {
        self.position = Some((x, y));
    }

    /// Set position.
    ///
    /// Chainable.
    pub fn with_position(mut self, x: usize, y: usize) -> Self {
        self.set_position(x, y);
        self
    }

    /// Print cell.
    ///
    /// Cells outside of the virtual image size will just use the background color.
    pub fn print_cell(&self, column: usize, row: usize) {
        let mut writer = stdout().lock();
        writer
            .write_placeholder_cell(self.id, column, row, self.position, self.background_color)
            .expect("write_placeholder_cell");
        writer.flush().expect("flush");
    }

    /// Print row.
    ///
    /// Cells outside of the virtual image size will just use the background color.
    pub fn print_row(&self, start_column: usize, row: usize, columns: usize) {
        let mut writer = stdout().lock();
        writer
            .write_placeholder_row(self.id, start_column, row, columns, self.position, self.background_color)
            .expect("write_placeholder_row");
        writer.flush().expect("flush");
    }

    /// Print block.
    ///
    /// Cells outside of the virtual image size will just use the background color.
    pub fn print_block(&self, start_column: usize, start_row: usize, columns: usize, rows: usize) {
        let mut writer = stdout().lock();
        writer
            .write_placeholder_block(
                self.id,
                start_column,
                start_row,
                columns,
                rows,
                self.position,
                self.background_color,
            )
            .expect("write_placeholder_block");
        writer.flush().expect("flush");
    }
}