tui_term/
vt100_imp.rs

1use ratatui::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::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::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    buf_cell.set_style(style);
59    buf_cell.set_fg(fg.into());
60    buf_cell.set_bg(bg.into());
61}
62
63/// Represents a foreground or background color for cells.
64/// Intermediate translation layer between
65/// [`vt100::Screen`] and [`ratatui::style::Color`]
66#[allow(dead_code)]
67enum Color {
68    Reset,
69    Black,
70    Red,
71    Green,
72    Yellow,
73    Blue,
74    Magenta,
75    Cyan,
76    Gray,
77    DarkGray,
78    LightRed,
79    LightGreen,
80    LightYellow,
81    LightBlue,
82    LightMagenta,
83    LightCyan,
84    White,
85    Rgb(u8, u8, u8),
86    Indexed(u8),
87}
88
89impl From<vt100::Color> for Color {
90    #[inline]
91    fn from(value: vt100::Color) -> Self {
92        match value {
93            vt100::Color::Default => Self::Reset,
94            vt100::Color::Idx(i) => Self::Indexed(i),
95            vt100::Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
96        }
97    }
98}
99
100impl From<Color> for vt100::Color {
101    #[inline]
102    fn from(value: Color) -> Self {
103        match value {
104            Color::Reset => Self::Default,
105            Color::Black => Self::Idx(0),
106            Color::Red => Self::Idx(1),
107            Color::Green => Self::Idx(2),
108            Color::Yellow => Self::Idx(3),
109            Color::Blue => Self::Idx(4),
110            Color::Magenta => Self::Idx(5),
111            Color::Cyan => Self::Idx(6),
112            Color::Gray => Self::Idx(7),
113            Color::DarkGray => Self::Idx(8),
114            Color::LightRed => Self::Idx(9),
115            Color::LightGreen => Self::Idx(10),
116            Color::LightYellow => Self::Idx(11),
117            Color::LightBlue => Self::Idx(12),
118            Color::LightMagenta => Self::Idx(13),
119            Color::LightCyan => Self::Idx(14),
120            Color::White => Self::Idx(15),
121            Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
122            Color::Indexed(i) => Self::Idx(i),
123        }
124    }
125}
126
127impl From<Color> for ratatui::style::Color {
128    #[inline]
129    fn from(value: Color) -> Self {
130        match value {
131            Color::Reset => Self::Reset,
132            Color::Black => Self::Black,
133            Color::Red => Self::Red,
134            Color::Green => Self::Green,
135            Color::Yellow => Self::Yellow,
136            Color::Blue => Self::Blue,
137            Color::Magenta => Self::Magenta,
138            Color::Cyan => Self::Cyan,
139            Color::Gray => Self::Gray,
140            Color::DarkGray => Self::DarkGray,
141            Color::LightRed => Self::LightRed,
142            Color::LightGreen => Self::LightGreen,
143            Color::LightYellow => Self::LightYellow,
144            Color::LightBlue => Self::LightBlue,
145            Color::LightMagenta => Self::LightMagenta,
146            Color::LightCyan => Self::LightCyan,
147            Color::White => Self::White,
148            Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
149            Color::Indexed(i) => Self::Indexed(i),
150        }
151    }
152}