termina/
lib.rs

1pub(crate) mod base64;
2pub mod escape;
3pub mod event;
4pub(crate) mod parse;
5pub mod style;
6mod terminal;
7
8use std::{fmt, num::NonZeroU16};
9
10pub use event::{reader::EventReader, Event};
11pub use parse::Parser;
12pub use terminal::{PlatformHandle, PlatformTerminal, Terminal};
13
14#[cfg(feature = "event-stream")]
15pub use event::stream::EventStream;
16
17/// A helper type which avoids tripping over Unix terminal's one-indexed conventions.
18///
19/// Coordinates and terminal dimensions are one-based in Termina on both Unix and Windows.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21// CREDIT: <https://github.com/wezterm/wezterm/blob/a87358516004a652ad840bc1661bdf65ffc89b43/termwiz/src/escape/mod.rs#L527-L588>.
22// This can be seen as a reimplementation on top of NonZeroU16.
23pub struct OneBased(NonZeroU16);
24
25impl OneBased {
26    pub const fn new(n: u16) -> Option<Self> {
27        match NonZeroU16::new(n) {
28            Some(n) => Some(Self(n)),
29            None => None,
30        }
31    }
32
33    pub const fn from_zero_based(n: u16) -> Self {
34        Self(unsafe { NonZeroU16::new_unchecked(n + 1) })
35    }
36
37    pub const fn get(self) -> u16 {
38        self.0.get()
39    }
40
41    pub const fn get_zero_based(self) -> u16 {
42        self.get() - 1
43    }
44}
45
46impl Default for OneBased {
47    fn default() -> Self {
48        Self(unsafe { NonZeroU16::new_unchecked(1) })
49    }
50}
51
52impl fmt::Display for OneBased {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        self.0.fmt(f)
55    }
56}
57
58impl From<NonZeroU16> for OneBased {
59    fn from(n: NonZeroU16) -> Self {
60        Self(n)
61    }
62}
63
64/// The dimensions of a terminal screen.
65///
66/// For both Unix and Windows, Termina returns the rows and columns.
67/// Pixel width and height are not supported on Windows.
68#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69pub struct WindowSize {
70    /// The width - the number of columns.
71    #[doc(alias = "width")]
72    pub cols: u16,
73    /// The height - the number of rows.
74    #[doc(alias = "height")]
75    pub rows: u16,
76    /// The height of the window in pixels.
77    pub pixel_width: Option<u16>,
78    /// The width of the window in pixels.
79    pub pixel_height: Option<u16>,
80}