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

chore: use vendored vt100 for tui-term #7713

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 4 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ turborepo-scm = { path = "crates/turborepo-scm" }
wax = { path = "crates/turborepo-wax" }
turborepo-vercel-api = { path = "crates/turborepo-vercel-api" }
turborepo-vercel-api-mock = { path = "crates/turborepo-vercel-api-mock" }
turborepo-vt100 = { path = "crates/turborepo-vt100" }

# Be careful when selecting tls backend, including change default tls backend.
# If you changed, must verify with ALL build targets with next-swc to ensure
Expand Down Expand Up @@ -251,6 +252,7 @@ proc-macro2 = "1.0.79"
qstring = "0.7.2"
quote = "1.0.23"
rand = "0.8.5"
ratatui = "0.26.1"
regex = "1.7.0"
rstest = "0.16.0"
rustc-hash = "1.1.0"
Expand Down Expand Up @@ -278,6 +280,8 @@ tokio-util = { version = "0.7.7", features = ["io"] }
tracing = "0.1.37"
tracing-appender = "0.2.2"
tracing-subscriber = "0.3.16"
# Latest release (0.1.8) doesn't include necessary changes for us to use our vendored vt100
tui-term = { git = "https://github.com/a-kenji/tui-term.git", rev = "96da15fb9974bb8f77b0e937abbdb2c3b8e932aa", default-features = false }
url = "2.2.2"
urlencoding = "2.1.2"
webbrowser = "0.8.7"
Expand Down
5 changes: 3 additions & 2 deletions crates/turborepo-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ crossterm = "0.26.1"
dialoguer.workspace = true
indicatif = { workspace = true }
lazy_static = { workspace = true }
ratatui = "0.26.1"
ratatui = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
tui-term = "0.1.8"
tui-term = { workspace = true }
turbopath = { workspace = true }
turborepo-ci = { workspace = true }
turborepo-vt100 = { workspace = true }
2 changes: 1 addition & 1 deletion crates/turborepo-ui/examples/pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crossterm::{
use ratatui::{
backend::{Backend, CrosstermBackend},
text::Text,
widgets::{Paragraph, Widget},
widgets::Widget,
Terminal, TerminalOptions, Viewport,
};
use turborepo_ui::TerminalPane;
Expand Down
5 changes: 3 additions & 2 deletions crates/turborepo-ui/src/tui/pane.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{collections::BTreeMap, io::Write};

use ratatui::widgets::{Block, Borders, Widget};
use tui_term::{vt100, widget::PseudoTerminal};
use tui_term::widget::PseudoTerminal;
use turborepo_vt100 as vt100;

use super::Error;

Expand Down Expand Up @@ -107,7 +108,7 @@ impl<W> TerminalOutput<W> {

fn resize(&mut self, rows: u16, cols: u16) {
if self.rows != rows || self.cols != cols {
self.parser.set_size(rows, cols);
self.parser.screen_mut().set_size(rows, cols);
}
self.rows = rows;
self.cols = cols;
Expand Down
6 changes: 6 additions & 0 deletions crates/turborepo-vt100/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ categories = ["command-line-interface", "encoding"]
license = "MIT"
include = ["src/**/*", "LICENSE", "README.md", "CHANGELOG.md"]

[features]
default = ["tui-term"]
tui-term = ["dep:tui-term", "dep:ratatui"]

[dependencies]
itoa = "1.0.9"
log = "0.4.19"
ratatui = { workspace = true, optional = true }
tui-term = { workspace = true, optional = true }
unicode-width = "0.1.10"
vte = "0.11.1"

Expand Down
2 changes: 2 additions & 0 deletions crates/turborepo-vt100/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ mod perform;
mod row;
mod screen;
mod term;
#[cfg(feature = "tui-term")]
mod tui_term;

pub use attrs::Color;
pub use callbacks::Callbacks;
Expand Down
159 changes: 159 additions & 0 deletions crates/turborepo-vt100/src/tui_term.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
use ratatui::style::{Modifier, Style};

impl tui_term::widget::Screen for crate::Screen {
type C = crate::Cell;

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

fn hide_cursor(&self) -> bool {
self.hide_cursor()
}

fn cursor_position(&self) -> (u16, u16) {
self.cursor_position()
}
}

impl<'a> tui_term::widget::Screen for crate::EntireScreen<'a> {
type C = crate::Cell;

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

fn hide_cursor(&self) -> bool {
true
}

fn cursor_position(&self) -> (u16, u16) {
(0, 0)
}
}

impl tui_term::widget::Cell for crate::Cell {
fn has_contents(&self) -> bool {
self.has_contents()
}

fn apply(&self, cell: &mut ratatui::buffer::Cell) {
fill_buf_cell(self, cell);
}
}

fn fill_buf_cell(
screen_cell: &crate::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<crate::Color> for Color {
fn from(value: crate::Color) -> Self {
match value {
crate::Color::Default => Self::Reset,
crate::Color::Idx(i) => Self::Indexed(i),
crate::Color::Rgb(r, g, b) => Self::Rgb(r, g, b),
}
}
}

impl From<Color> for crate::Color {
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 {
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