This repository has been archived on 2026-10-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
maestro/crates/maestro-tui/src/pane.rs
T
Miguel Palhas c9d089534b
ci / web (push) Successful in 59s
ci / rust (push) Successful in 6m8s
ci / flake (push) Successful in 9m52s
enable session scrolling across clients
2026-08-25 18:46:26 +01:00

168 lines
5.2 KiB
Rust

//! Attach-pane widget — draws a `vt100::Screen` into a ratatui buffer
//! cell-by-cell, translating SGR (fg / bg / attrs) into `ratatui::Style`.
//!
//! The pane's `screen` is the same `Arc<Mutex<vt100::Parser>>` the
//! `SessionTask` writes into, so the renderer is always looking at the most
//! recent cell grid.
use std::sync::{Arc, Mutex};
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::Frame;
use crate::session_task::SessionState;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PaneStatus {
Connecting,
Live,
Reconnecting,
Ended,
}
impl From<SessionState> for PaneStatus {
fn from(s: SessionState) -> Self {
match s {
SessionState::Connecting => PaneStatus::Connecting,
SessionState::Live => PaneStatus::Live,
SessionState::Reconnecting => PaneStatus::Reconnecting,
SessionState::Ended => PaneStatus::Ended,
}
}
}
#[derive(Clone)]
pub struct Pane {
pub screen: Arc<Mutex<vt100::Parser>>,
pub status: PaneStatus,
pub focused: bool,
}
impl std::fmt::Debug for Pane {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Pane")
.field("status", &self.status)
.field("focused", &self.focused)
.finish_non_exhaustive()
}
}
impl Pane {
pub fn render(&self, f: &mut Frame, area: Rect) {
let parser = self.screen.lock().expect("vt100 parser mutex");
let screen = parser.screen();
let (screen_rows, screen_cols) = screen.size();
let rows = area.height.min(screen_rows);
let cols = area.width.min(screen_cols);
// Write cells. Scoped so the buffer borrow ends before set_cursor.
{
let buf = f.buffer_mut();
for r in 0..rows {
for c in 0..cols {
if let Some(cell) = screen.cell(r, c) {
let s = cell.contents();
let s = if s.is_empty() { " " } else { &s };
buf[(area.x + c, area.y + r)]
.set_symbol(s)
.set_style(translate_cell_style(cell));
}
}
}
}
// Place the terminal cursor at vt100's cursor position when we own
// focus and the cursor is on-screen. Terminals blink it on their own.
if self.focused && screen.scrollback() == 0 && !screen.hide_cursor() {
let (cr, cc) = screen.cursor_position();
if cr < rows && cc < cols {
f.set_cursor_position((area.x + cc, area.y + cr));
}
}
}
}
fn translate_cell_style(cell: &vt100::Cell) -> Style {
let mut s = Style::default();
if let Some(c) = translate_color(cell.fgcolor()) {
s = s.fg(c);
}
if let Some(c) = translate_color(cell.bgcolor()) {
s = s.bg(c);
}
if cell.bold() {
s = s.add_modifier(Modifier::BOLD);
}
if cell.italic() {
s = s.add_modifier(Modifier::ITALIC);
}
if cell.underline() {
s = s.add_modifier(Modifier::UNDERLINED);
}
if cell.inverse() {
s = s.add_modifier(Modifier::REVERSED);
}
s
}
/// PTY cell colours pass through verbatim: they belong to the program
/// running in the pane, not to the TUI's own chrome, so they are deliberately
/// not palette roles (see `crate::palette`).
fn translate_color(c: vt100::Color) -> Option<Color> {
match c {
vt100::Color::Default => None,
vt100::Color::Idx(i) => Some(Color::Indexed(i)),
vt100::Color::Rgb(r, g, b) => Some(Color::Rgb(r, g, b)),
}
}
#[cfg(test)]
mod tests {
use super::*;
use ratatui::backend::TestBackend;
use ratatui::Terminal;
fn render_with_bytes(rows: u16, cols: u16, bytes: &[u8]) -> String {
let parser = Arc::new(Mutex::new(vt100::Parser::new(rows, cols, 0)));
parser.lock().unwrap().process(bytes);
let pane = Pane {
screen: parser,
status: PaneStatus::Live,
focused: true,
};
let backend = TestBackend::new(cols, rows);
let mut terminal = Terminal::new(backend).unwrap();
terminal.draw(|f| pane.render(f, f.area())).unwrap();
let buf = terminal.backend().buffer().clone();
let mut out = String::new();
for y in 0..buf.area.height {
for x in 0..buf.area.width {
out.push_str(buf[(x, y)].symbol());
}
out.push('\n');
}
out
}
#[test]
fn pane_renders_vt100_chars_into_buffer() {
let rendered = render_with_bytes(3, 20, b"hello world");
assert!(rendered.contains("hello world"), "got:\n{rendered}");
}
#[test]
fn pane_status_round_trips_from_session_state() {
assert_eq!(
PaneStatus::from(SessionState::Connecting),
PaneStatus::Connecting
);
assert_eq!(PaneStatus::from(SessionState::Live), PaneStatus::Live);
assert_eq!(
PaneStatus::from(SessionState::Reconnecting),
PaneStatus::Reconnecting
);
assert_eq!(PaneStatus::from(SessionState::Ended), PaneStatus::Ended);
}
}