Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looser coupling with vt100 #152

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ categories = ["command-line-interface", "command-line-utilities"]
# ]

[features]
default = ["vt100"]
unstable = ["dep:portable-pty"]

[dependencies]
ratatui = "0.26.1"
vt100 = "0.15.2"
vt100 = { version = "0.15.2", optional = true }
portable-pty = { version = "0.8.1", optional = true }

[dev-dependencies]
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! use vt100::Parser;
//!
//! let mut parser = vt100::Parser::new(24, 80, 0);
//! let pseudo_term = PseudoTerminal::new(&parser.screen())
//! let pseudo_term = PseudoTerminal::new(parser.screen())
//! .block(Block::default().title("Terminal").borders(Borders::ALL))
//! .style(
//! Style::default()
Expand All @@ -49,10 +49,13 @@
//! sequences, but future versions may introduce support for alternative backends.

mod state;
#[cfg(feature = "vt100")]
mod vt100_imp;
pub mod widget;

#[cfg(feature = "unstable")]
pub mod controller;

/// Reexport of the vt100 crate to ensure correct version compatibility
#[cfg(feature = "vt100")]
pub use vt100;
124 changes: 4 additions & 120 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Modifier, Style},
};
use ratatui::{buffer::Buffer, layout::Rect};

use crate::widget::PseudoTerminal;
use crate::widget::{Cell, PseudoTerminal, Screen};

/// Draw the [`Screen`] to the [`Buffer`],
/// area is the designated area that the consumer provides
pub fn handle(term: &PseudoTerminal, area: Rect, buf: &mut Buffer) {
pub fn handle<S: Screen>(term: &PseudoTerminal<S>, area: Rect, buf: &mut Buffer) {
let cols = area.width;
let rows = area.height;
let col_start = area.x;
Expand All @@ -30,29 +26,8 @@ pub fn handle(term: &PseudoTerminal, area: Rect, buf: &mut Buffer) {

if let Some(screen_cell) = screen.cell(row, col) {
if screen_cell.has_contents() {
let fg = screen_cell.fgcolor();
let bg = screen_cell.bgcolor();

let cell = buf.get_mut(buf_col, buf_row);
cell.set_symbol(&screen_cell.contents());
let fg: Color = fg.into();
let bg: Color = bg.into();
let mut style = Style::reset();
if screen_cell.bold() {
style = style.add_modifier(Modifier::BOLD);
}
if screen_cell.italic() {
style = style.add_modifier(Modifier::ITALIC);
}
if screen_cell.underline() {
style = style.add_modifier(Modifier::UNDERLINED);
}
if screen_cell.inverse() {
style = style.add_modifier(Modifier::REVERSED);
}
cell.set_style(style);
cell.set_fg(fg.into());
cell.set_bg(bg.into());
screen_cell.apply(cell);
}
}
}
Expand All @@ -76,94 +51,3 @@ pub fn handle(term: &PseudoTerminal, area: Rect, buf: &mut Buffer) {
}
}
}

/// Represents a foreground or background color for cells.
/// Intermediate translation layer between
/// [`vt100::Screen`] and [`ratatui::style::Color`]
#[allow(dead_code)]
enum Color {
Reset,
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
Gray,
DarkGray,
LightRed,
LightGreen,
LightYellow,
LightBlue,
LightMagenta,
LightCyan,
White,
Rgb(u8, u8, u8),
Indexed(u8),
}

impl From<vt100::Color> for Color {
#[inline]
fn from(value: vt100::Color) -> Self {
match value {
vt100::Color::Default => Self::Reset,
vt100::Color::Idx(i) => Self::Indexed(i),
vt100::Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
}
}
}

impl From<Color> for vt100::Color {
#[inline]
fn from(value: Color) -> Self {
match value {
Color::Reset => Self::Default,
Color::Black => Self::Idx(0),
Color::Red => Self::Idx(1),
Color::Green => Self::Idx(2),
Color::Yellow => Self::Idx(3),
Color::Blue => Self::Idx(4),
Color::Magenta => Self::Idx(5),
Color::Cyan => Self::Idx(6),
Color::Gray => Self::Idx(7),
Color::DarkGray => Self::Idx(8),
Color::LightRed => Self::Idx(9),
Color::LightGreen => Self::Idx(10),
Color::LightYellow => Self::Idx(11),
Color::LightBlue => Self::Idx(12),
Color::LightMagenta => Self::Idx(13),
Color::LightCyan => Self::Idx(14),
Color::White => Self::Idx(15),
Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
Color::Indexed(i) => Self::Idx(i),
}
}
}

impl From<Color> for ratatui::style::Color {
#[inline]
fn from(value: Color) -> Self {
match value {
Color::Reset => Self::Reset,
Color::Black => Self::Black,
Color::Red => Self::Red,
Color::Green => Self::Green,
Color::Yellow => Self::Yellow,
Color::Blue => Self::Blue,
Color::Magenta => Self::Magenta,
Color::Cyan => Self::Cyan,
Color::Gray => Self::Gray,
Color::DarkGray => Self::DarkGray,
Color::LightRed => Self::LightRed,
Color::LightGreen => Self::LightGreen,
Color::LightYellow => Self::LightYellow,
Color::LightBlue => Self::LightBlue,
Color::LightMagenta => Self::LightMagenta,
Color::LightCyan => Self::LightCyan,
Color::White => Self::White,
Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
Color::Indexed(i) => Self::Indexed(i),
}
}
}
151 changes: 151 additions & 0 deletions src/vt100_imp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use ratatui::style::{Modifier, Style};

use crate::widget::{Cell, Screen};

impl Screen for vt100::Screen {
type C = vt100::Cell;

#[inline]
fn cell(&self, row: u16, col: u16) -> Option<&Self::C> {
self.cell(row, col)
}

#[inline]
fn hide_cursor(&self) -> bool {
self.hide_cursor()
}

#[inline]
fn cursor_position(&self) -> (u16, u16) {
self.cursor_position()
}
}

impl Cell for vt100::Cell {
#[inline]
fn has_contents(&self) -> bool {
self.has_contents()
}

#[inline]
fn apply(&self, cell: &mut ratatui::buffer::Cell) {
fill_buf_cell(self, cell)
}
}

#[inline]
fn fill_buf_cell(screen_cell: &vt100::Cell, buf_cell: &mut ratatui::buffer::Cell) {
let fg = screen_cell.fgcolor();
let bg = screen_cell.bgcolor();

buf_cell.set_symbol(&screen_cell.contents());
let fg: Color = fg.into();
let bg: Color = bg.into();
let mut style = Style::reset();
if screen_cell.bold() {
style = style.add_modifier(Modifier::BOLD);
}
if screen_cell.italic() {
style = style.add_modifier(Modifier::ITALIC);
}
if screen_cell.underline() {
style = style.add_modifier(Modifier::UNDERLINED);
}
if screen_cell.inverse() {
style = style.add_modifier(Modifier::REVERSED);
}
buf_cell.set_style(style);
buf_cell.set_fg(fg.into());
buf_cell.set_bg(bg.into());
}

/// Represents a foreground or background color for cells.
/// Intermediate translation layer between
/// [`vt100::Screen`] and [`ratatui::style::Color`]
#[allow(dead_code)]
enum Color {
Reset,
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
Gray,
DarkGray,
LightRed,
LightGreen,
LightYellow,
LightBlue,
LightMagenta,
LightCyan,
White,
Rgb(u8, u8, u8),
Indexed(u8),
}

impl From<vt100::Color> for Color {
#[inline]
fn from(value: vt100::Color) -> Self {
match value {
vt100::Color::Default => Self::Reset,
vt100::Color::Idx(i) => Self::Indexed(i),
vt100::Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
}
}
}

impl From<Color> for vt100::Color {
#[inline]
fn from(value: Color) -> Self {
match value {
Color::Reset => Self::Default,
Color::Black => Self::Idx(0),
Color::Red => Self::Idx(1),
Color::Green => Self::Idx(2),
Color::Yellow => Self::Idx(3),
Color::Blue => Self::Idx(4),
Color::Magenta => Self::Idx(5),
Color::Cyan => Self::Idx(6),
Color::Gray => Self::Idx(7),
Color::DarkGray => Self::Idx(8),
Color::LightRed => Self::Idx(9),
Color::LightGreen => Self::Idx(10),
Color::LightYellow => Self::Idx(11),
Color::LightBlue => Self::Idx(12),
Color::LightMagenta => Self::Idx(13),
Color::LightCyan => Self::Idx(14),
Color::White => Self::Idx(15),
Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
Color::Indexed(i) => Self::Idx(i),
}
}
}

impl From<Color> for ratatui::style::Color {
#[inline]
fn from(value: Color) -> Self {
match value {
Color::Reset => Self::Reset,
Color::Black => Self::Black,
Color::Red => Self::Red,
Color::Green => Self::Green,
Color::Yellow => Self::Yellow,
Color::Blue => Self::Blue,
Color::Magenta => Self::Magenta,
Color::Cyan => Self::Cyan,
Color::Gray => Self::Gray,
Color::DarkGray => Self::DarkGray,
Color::LightRed => Self::LightRed,
Color::LightGreen => Self::LightGreen,
Color::LightYellow => Self::LightYellow,
Color::LightBlue => Self::LightBlue,
Color::LightMagenta => Self::LightMagenta,
Color::LightCyan => Self::LightCyan,
Color::White => Self::White,
Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
Color::Indexed(i) => Self::Indexed(i),
}
}
}
Loading
Loading