Skip to main content

Rusty_tui/
border.rs

1/// A structure representing a border with specific dimensions and position.
2pub struct Border {
3    /// The character or string used for the border.
4    pub border: String,
5    /// The height of the area defined by the border.
6    pub h: u16,
7    /// The width of the area defined by the border.
8    pub w: u16,
9    /// The x-coordinate position of the border's top-left corner.
10    pub x: u16,
11    /// The y-coordinate position of the border's top-left corner.
12    pub y: u16,
13}
14
15impl Border {
16    /// Creates a new instance of `Border`.
17    ///
18    /// # Parameters
19    ///
20    /// - `h`: The height of the border area.
21    /// - `w`: The width of the border area.
22    /// - `x`: The x-coordinate of the border's position.
23    /// - `y`: The y-coordinate of the border's position.
24    /// - `border`: A string representing the border character(s).
25    ///
26    /// # Returns
27    ///
28    /// Returns a new `Border` instance.
29    pub fn new(h: u16, w: u16, x: u16, y: u16, border: String) -> Border {
30        Border {
31            border, // Using shorthand syntax for field initialization
32            h,
33            w,
34            x,
35            y,
36        }
37    }
38
39    /// Draws the border on the console.
40    ///
41    /// This method will print the border to the console starting at the specified
42    /// (x, y) position. The border consists of repeated characters specified
43    /// in the `border` field, and it is drawn for the height and width defined
44    /// in the struct.
45    ///
46    /// # Example
47    ///
48    /// ```
49    /// let border = Border::new(5, 10, 2, 1, "*".to_string());
50    /// border.draw();
51    /// ```
52    ///
53    /// This will create a border 10 characters wide and 5 characters high, starting
54    /// from the x-coordinate 2 and y-coordinate 1.
55    pub fn draw(&self) {
56        // Calculate the top-left corner based on the position
57        for row in 0..self.h {
58            // Move cursor to (self.x, self.y + row)
59            println!("{:width$}", "", width = self.x as usize);
60            
61            // Draw the border line
62            let border_line = self.border.repeat(self.w as usize);
63            println!("{}", border_line);
64        }
65    }
66}