Skip to main content

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