fp_tui 0.2.2

A very basic tui library
Documentation
use crate::common::Dimensions;

/// #### Description
/// A display buffer is the buffer that holds all of the information
/// about what characters to display to the screen
///
#[derive(Debug)]
pub struct DisplayBuffer {
    /// #### Description
    /// The dimensions of the buffer
    ///
    dimensions: Dimensions,

    /// #### Description
    /// The heap allocated buffer of characters
    ///
    buffer: Box<[Box<[char]>]>
}

impl DisplayBuffer {
    /// #### Description
    /// Creates a new display buffer with the given dimensions
    ///
    /// #### Arguments
    /// * `dimensions`: [Dimensions] - The dimensions of the display buffer
    ///
    /// #### Returns
    /// [DisplayBuffer] - A display buffer with the specified attributes
    ///
    pub fn new(dimensions: Dimensions) -> Self {
        // Generate the buffer
        let mut buffer_vec = vec![];

        for _ in 0..dimensions.get_height() {
            let mut row = vec![];

            for _ in 0..dimensions.get_width() {
                row.push(' ');
            }

            buffer_vec.push(row.into_boxed_slice());
        }

        let buffer = buffer_vec.into_boxed_slice();
        
        return Self {dimensions, buffer};
    }

    /// #### Description
    /// Draws the buffer to the terminal
    ///
    pub fn draw(&self) {
        // Combine buffer into a single string
        let output = self.buffer.iter().map(|row| {
            let mut row_str = row.iter().collect::<String>();
            row_str.push('\n');
            return row_str;
        }).collect::<String>();

        // write the string to the terminal
        println!("{}", output);
    }

    /// #### Description
    /// Retrieves the width of the display buffer
    ///
    /// #### Returns
    /// [u32] - The width of the display buffer
    ///
    pub fn get_width(&self) -> u32 {
        return self.dimensions.get_width();
    }

    /// #### Description
    /// Retrieves the height of the display buffer
    ///
    /// #### Returns
    /// [u32] - The height of the display buffer
    ///
    pub fn get_height(&self) -> u32 {
        return self.dimensions.get_height();
    }

    /// #### Description
    /// Writes a character to the display buffer
    ///
    /// If the coordinates are out of the bounds of the buffer,
    /// it ignores the write
    ///
    /// #### Arguments
    /// * `c`: [char] - The character to insert
    /// * `x`: [u32] - The x position to write to
    /// * `y`: [u32] - The y position to write to
    ///
    pub fn set_char(&mut self, c: char, x: i32, y: i32) {
        // Guard for detecting if the coordinates are in bounds
        if self.dimensions.get_width() <= x as u32 || 
           self.dimensions.get_height() <= y as u32 ||
           x < 0 ||
           y < 0
        {
            return;
        }
        else {
            self.buffer[y as usize][x as usize] = c;
        }
    }

    /// #### Description
    /// Clears the display buffer to a specified character
    ///
    /// #### Arguments
    /// * `c`: [char] - The character to clear with
    ///
    pub fn clear(&mut self, c: char) {
        for y in 0..self.dimensions.get_height() {
            for x in 0..self.dimensions.get_width() {
                self.buffer[y as usize][x as usize] = c;
            }
        }
    }
}