Skip to main content

rmux_core/input/
cell.rs

1//! Cell state (attributes, colours, character set).
2
3use super::colour::{Colour, COLOUR_DEFAULT};
4
5/// Grid cell attributes matching tmux `GRID_ATTR_*`.
6#[allow(non_snake_case, non_upper_case_globals)]
7pub mod GridAttr {
8    /// Bold.
9    pub const BRIGHT: u16 = 0x1;
10    /// Dim.
11    pub const DIM: u16 = 0x2;
12    /// Single underline.
13    pub const UNDERSCORE: u16 = 0x4;
14    /// Blink.
15    pub const BLINK: u16 = 0x8;
16    /// Reverse video.
17    pub const REVERSE: u16 = 0x10;
18    /// Hidden.
19    pub const HIDDEN: u16 = 0x20;
20    /// Italics.
21    pub const ITALICS: u16 = 0x40;
22    /// ACS line-drawing charset.
23    pub const CHARSET: u16 = 0x80;
24    /// Strikethrough.
25    pub const STRIKETHROUGH: u16 = 0x100;
26    /// Double underline.
27    pub const UNDERSCORE_2: u16 = 0x200;
28    /// Curly underline.
29    pub const UNDERSCORE_3: u16 = 0x400;
30    /// Dotted underline.
31    pub const UNDERSCORE_4: u16 = 0x800;
32    /// Dashed underline.
33    pub const UNDERSCORE_5: u16 = 0x1000;
34    /// Overline.
35    pub const OVERLINE: u16 = 0x2000;
36    /// Explicitly no inherited attributes.
37    pub const NOATTR: u16 = 0x4000;
38
39    /// All underscore variants combined.
40    pub const ALL_UNDERSCORE: u16 =
41        UNDERSCORE | UNDERSCORE_2 | UNDERSCORE_3 | UNDERSCORE_4 | UNDERSCORE_5;
42}
43
44/// Grid cell, holding fg/bg/us colours and attributes.
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub(crate) struct GridCell {
47    pub attr: u16,
48    pub fg: Colour,
49    pub bg: Colour,
50    pub us: Colour,
51    pub link: u32,
52}
53
54impl Default for GridCell {
55    fn default() -> Self {
56        Self {
57            attr: 0,
58            fg: COLOUR_DEFAULT,
59            bg: COLOUR_DEFAULT,
60            us: COLOUR_DEFAULT,
61            link: 0,
62        }
63    }
64}
65
66/// Character set and cell state for the parser, matching tmux `input_cell`.
67#[derive(Debug, Clone, Default, PartialEq, Eq)]
68pub struct CellState {
69    /// Grid cell attributes.
70    pub(crate) cell: GridCell,
71    /// Active character set: 0 = G0, 1 = G1.
72    pub set: i32,
73    /// G0 set: 0 = normal, 1 = ACS.
74    pub g0set: i32,
75    /// G1 set: 0 = normal, 1 = ACS.
76    pub g1set: i32,
77}
78
79impl CellState {
80    /// Returns the current foreground colour.
81    #[must_use]
82    pub fn fg(&self) -> Colour {
83        self.cell.fg
84    }
85
86    /// Returns the current background colour.
87    #[must_use]
88    pub fn bg(&self) -> Colour {
89        self.cell.bg
90    }
91
92    /// Returns the current underline colour.
93    #[must_use]
94    pub fn us(&self) -> Colour {
95        self.cell.us
96    }
97
98    /// Returns the current attribute flags.
99    #[must_use]
100    pub fn attr(&self) -> u16 {
101        self.cell.attr
102    }
103
104    /// Returns the hyperlink ID.
105    #[must_use]
106    pub fn link(&self) -> u32 {
107        self.cell.link
108    }
109
110    pub(crate) fn reset(&mut self) {
111        self.cell = GridCell::default();
112        self.set = 0;
113        self.g0set = 0;
114        self.g1set = 0;
115    }
116}
117
118/// Saved state for DECSC/DECRC.
119#[derive(Debug, Clone, Default, PartialEq, Eq)]
120pub struct SavedState {
121    pub(crate) cell: CellState,
122    pub(crate) cx: u32,
123    pub(crate) cy: u32,
124    pub(crate) mode_origin: bool,
125}
126
127impl SavedState {
128    /// Returns the saved rendition and character-set state.
129    #[must_use]
130    pub const fn cell(&self) -> &CellState {
131        &self.cell
132    }
133
134    /// Returns the cursor saved by DECSC/SCP.
135    #[must_use]
136    pub const fn cursor_position(&self) -> (u32, u32) {
137        (self.cx, self.cy)
138    }
139
140    /// Returns whether origin mode was active at save time.
141    #[must_use]
142    pub const fn origin_mode(&self) -> bool {
143        self.mode_origin
144    }
145}