Skip to main content

rmux_types/
lib.rs

1#![forbid(unsafe_code)]
2#![deny(missing_docs)]
3
4//! Portable semantic newtypes shared by non-adjacent RMUX crates.
5
6/// A terminal geometry request.
7#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct TerminalSize {
10    /// The requested column count.
11    pub cols: u16,
12    /// The requested row count.
13    pub rows: u16,
14}
15
16impl TerminalSize {
17    /// Creates a terminal size value from column and row counts.
18    #[must_use]
19    pub const fn new(cols: u16, rows: u16) -> Self {
20        Self { cols, rows }
21    }
22
23    /// Returns this cell geometry wrapped as a terminal geometry with no pixel size.
24    #[must_use]
25    pub const fn into_geometry(self) -> TerminalGeometry {
26        TerminalGeometry::from_size(self)
27    }
28}
29
30/// Terminal pixel dimensions reported by terminals that expose `TIOCGWINSZ`
31/// pixel fields.
32#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
33#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
34pub struct TerminalPixels {
35    /// The terminal width in pixels.
36    pub width: u16,
37    /// The terminal height in pixels.
38    pub height: u16,
39}
40
41impl TerminalPixels {
42    /// Creates terminal pixel dimensions.
43    #[must_use]
44    pub const fn new(width: u16, height: u16) -> Self {
45        Self { width, height }
46    }
47}
48
49/// A terminal geometry request including cell dimensions and optional pixels.
50#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
52pub struct TerminalGeometry {
53    /// The terminal size in character cells.
54    pub size: TerminalSize,
55    /// The terminal size in pixels, when the outer terminal exposes it.
56    pub pixels: Option<TerminalPixels>,
57}
58
59impl TerminalGeometry {
60    /// Creates terminal geometry from cell dimensions.
61    #[must_use]
62    pub const fn new(cols: u16, rows: u16) -> Self {
63        Self {
64            size: TerminalSize::new(cols, rows),
65            pixels: None,
66        }
67    }
68
69    /// Creates terminal geometry from an existing cell size.
70    #[must_use]
71    pub const fn from_size(size: TerminalSize) -> Self {
72        Self { size, pixels: None }
73    }
74
75    /// Adds pixel dimensions to this geometry.
76    #[must_use]
77    pub const fn with_pixels(mut self, pixels: TerminalPixels) -> Self {
78        self.pixels = Some(pixels);
79        self
80    }
81}