Rusty-tui 0.1.0

A simple TUI library for making CLI applications with ease.
Documentation
// Label.rs

use std::io::{self, Write}; // Importing necessary modules for input/output operations

/// A structure representing a text label that can be drawn on a console at a specific position.
pub struct Label {
    text: String, // The text to be displayed by the label
    x: u16,       // The x-coordinate (horizontal position) where the label will be drawn
    y: u16,       // The y-coordinate (vertical position) where the label will be drawn
}

impl Label {
    /// Creates a new Label instance.
    ///
    /// # Arguments
    ///
    /// * `text` - A string slice that holds the text of the label.
    /// * `x` - The horizontal position (x-coordinate) where the label will be drawn.
    /// * `y` - The vertical position (y-coordinate) where the label will be drawn.
    ///
    /// # Returns
    ///
    /// A new instance of `Label`.
    pub fn new(text: &str, x: u16, y: u16) -> Self {
        Label {
            text: text.to_string(), // Convert the string slice into a String
            x,                      // Set the x-coordinate
            y,                      // Set the y-coordinate
        }
    }

    /// Draws the label at the specified (x, y) coordinates in the console.
    ///
    /// # Arguments
    ///
    /// * `stdout` - A mutable reference to any type that implements the `Write` trait, allowing
    ///              the method to write the label's text to the specified output.
    ///
    /// # Errors
    ///
    /// Returns an `io::Result<()>`, which will contain an error if the write operation fails.
    ///
    /// # Example
    ///
    /// ```
    /// use std::io::{self, Write};
    /// let mut stdout = io::stdout();
    /// let label = Label::new("Hello, world!", 10, 5);
    /// label.draw(&mut stdout).unwrap(); // Draws the label
    /// ```
    pub fn draw(&self, stdout: &mut impl Write) -> io::Result<()> {
        // Move the cursor to the (x, y) position in the console
        write!(stdout, "\x1B[{};{}H", self.y, self.x)?;

        // Print the text of the label
        write!(stdout, "{}", self.text)?;

        // Flush the output buffer to ensure the text is displayed immediately
        stdout.flush()?;

        Ok(())
    }
}