devela/ui/front/term/
size.rs

1// devela::ui::front::term::size
2//
3//! Defines [`TermSize`].
4//
5
6/// The size of the terminal.
7///
8/// ## Used by
9/// - `LinuxTermios`.
10#[must_use]
11#[repr(C)] // field order matters!
12#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
13pub struct TermSize {
14    /// Rows of cells.
15    pub rows: u16,
16    /// Columns of cells.
17    pub cols: u16,
18    /// Horizontal pixels.
19    pub x: u16,
20    /// Vertical pixels.
21    pub y: u16,
22}
23
24impl TermSize {
25    /// WIP
26    pub const fn new(rows_cols: (u16, u16), xy: (u16, u16)) -> Self {
27        TermSize {
28            rows: rows_cols.0,
29            cols: rows_cols.1,
30            x: xy.0,
31            y: xy.1,
32        }
33    }
34
35    /// Returns the number of `(horizontal, vertical)` pixels.
36    #[must_use]
37    pub const fn pixels(self) -> (u16, u16) {
38        (self.x, self.y)
39    }
40    /// Returns the number of `(columns, rows)` of cells.
41    #[must_use]
42    pub const fn cells(self) -> (u16, u16) {
43        (self.cols, self.rows)
44    }
45    /// Returns the number of pixels per cell `(horizontal, vertical)`.
46    #[must_use]
47    pub const fn pixels_per_cell(self) -> (u16, u16) {
48        (self.x / self.cols, self.y / self.rows)
49    }
50}