Skip to main content

tui_term/
vt100_imp.rs

1use ratatui_core::style::{Modifier, Style};
2
3use crate::widget::{Cell, Screen};
4
5impl Screen for vt100::Screen {
6    type C = vt100::Cell;
7
8    #[inline]
9    fn cell(&self, row: u16, col: u16) -> Option<&Self::C> {
10        self.cell(row, col)
11    }
12
13    #[inline]
14    fn hide_cursor(&self) -> bool {
15        self.hide_cursor()
16    }
17
18    #[inline]
19    fn cursor_position(&self) -> (u16, u16) {
20        self.cursor_position()
21    }
22}
23
24impl Cell for vt100::Cell {
25    #[inline]
26    fn has_contents(&self) -> bool {
27        self.has_contents()
28    }
29
30    #[inline]
31    fn apply(&self, cell: &mut ratatui_core::buffer::Cell) {
32        fill_buf_cell(self, cell)
33    }
34}
35
36#[inline]
37fn fill_buf_cell(screen_cell: &vt100::Cell, buf_cell: &mut ratatui_core::buffer::Cell) {
38    let fg = screen_cell.fgcolor();
39    let bg = screen_cell.bgcolor();
40    if screen_cell.has_contents() {
41        buf_cell.set_symbol(screen_cell.contents());
42    }
43    let fg: Color = fg.into();
44    let bg: Color = bg.into();
45    let mut style = Style::reset();
46    if screen_cell.bold() {
47        style = style.add_modifier(Modifier::BOLD);
48    }
49    if screen_cell.italic() {
50        style = style.add_modifier(Modifier::ITALIC);
51    }
52    if screen_cell.underline() {
53        style = style.add_modifier(Modifier::UNDERLINED);
54    }
55    if screen_cell.inverse() {
56        style = style.add_modifier(Modifier::REVERSED);
57    }
58    if screen_cell.dim() {
59        style = style.add_modifier(Modifier::DIM);
60    }
61    buf_cell.set_style(style.fg(fg.into()).bg(bg.into()));
62}
63
64/// Represents a foreground or background color for cells.
65/// Intermediate translation layer between
66/// [`vt100::Screen`] and [`ratatui_core::style::Color`]
67#[allow(dead_code)]
68enum Color {
69    Reset,
70    Black,
71    Red,
72    Green,
73    Yellow,
74    Blue,
75    Magenta,
76    Cyan,
77    Gray,
78    DarkGray,
79    LightRed,
80    LightGreen,
81    LightYellow,
82    LightBlue,
83    LightMagenta,
84    LightCyan,
85    White,
86    Rgb(u8, u8, u8),
87    Indexed(u8),
88}
89
90impl From<vt100::Color> for Color {
91    #[inline]
92    fn from(value: vt100::Color) -> Self {
93        match value {
94            vt100::Color::Default => Self::Reset,
95            vt100::Color::Idx(i) => Self::Indexed(i),
96            vt100::Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
97        }
98    }
99}
100
101impl From<Color> for vt100::Color {
102    #[inline]
103    fn from(value: Color) -> Self {
104        match value {
105            Color::Reset => Self::Default,
106            Color::Black => Self::Idx(0),
107            Color::Red => Self::Idx(1),
108            Color::Green => Self::Idx(2),
109            Color::Yellow => Self::Idx(3),
110            Color::Blue => Self::Idx(4),
111            Color::Magenta => Self::Idx(5),
112            Color::Cyan => Self::Idx(6),
113            Color::Gray => Self::Idx(7),
114            Color::DarkGray => Self::Idx(8),
115            Color::LightRed => Self::Idx(9),
116            Color::LightGreen => Self::Idx(10),
117            Color::LightYellow => Self::Idx(11),
118            Color::LightBlue => Self::Idx(12),
119            Color::LightMagenta => Self::Idx(13),
120            Color::LightCyan => Self::Idx(14),
121            Color::White => Self::Idx(15),
122            Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
123            Color::Indexed(i) => Self::Idx(i),
124        }
125    }
126}
127
128impl From<Color> for ratatui_core::style::Color {
129    #[inline]
130    fn from(value: Color) -> Self {
131        match value {
132            Color::Reset => Self::Reset,
133            Color::Black => Self::Black,
134            Color::Red => Self::Red,
135            Color::Green => Self::Green,
136            Color::Yellow => Self::Yellow,
137            Color::Blue => Self::Blue,
138            Color::Magenta => Self::Magenta,
139            Color::Cyan => Self::Cyan,
140            Color::Gray => Self::Gray,
141            Color::DarkGray => Self::DarkGray,
142            Color::LightRed => Self::LightRed,
143            Color::LightGreen => Self::LightGreen,
144            Color::LightYellow => Self::LightYellow,
145            Color::LightBlue => Self::LightBlue,
146            Color::LightMagenta => Self::LightMagenta,
147            Color::LightCyan => Self::LightCyan,
148            Color::White => Self::White,
149            Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
150            Color::Indexed(i) => Self::Indexed(i),
151        }
152    }
153}