From 91f836884410285fb3adf151ba60057df9e8aeb8 Mon Sep 17 00:00:00 2001 From: Chris Olszewski Date: Tue, 13 Aug 2024 13:55:52 -0400 Subject: [PATCH] fix(tui): build new virtual terminal on resize --- crates/turborepo-ui/src/tui/app.rs | 2 +- crates/turborepo-ui/src/tui/term_output.rs | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/crates/turborepo-ui/src/tui/app.rs b/crates/turborepo-ui/src/tui/app.rs index ef72122773c5b..0c63d7f644db6 100644 --- a/crates/turborepo-ui/src/tui/app.rs +++ b/crates/turborepo-ui/src/tui/app.rs @@ -400,7 +400,7 @@ impl App { #[tracing::instrument(skip(self, output))] pub fn process_output(&mut self, task: &str, output: &[u8]) -> Result<(), Error> { let task_output = self.tasks.get_mut(task).unwrap(); - task_output.parser.process(output); + task_output.process(output); Ok(()) } } diff --git a/crates/turborepo-ui/src/tui/term_output.rs b/crates/turborepo-ui/src/tui/term_output.rs index 0ad2b1ee1d0fe..2eb2e52cf7da3 100644 --- a/crates/turborepo-ui/src/tui/term_output.rs +++ b/crates/turborepo-ui/src/tui/term_output.rs @@ -1,4 +1,4 @@ -use std::io::Write; +use std::{io::Write, mem}; use turborepo_vt100 as vt100; @@ -8,7 +8,10 @@ use super::{ Error, }; +const SCROLLBACK_LEN: usize = 1024; + pub struct TerminalOutput { + output: Vec, pub parser: vt100::Parser, pub stdin: Option, pub status: Option, @@ -27,7 +30,8 @@ enum LogBehavior { impl TerminalOutput { pub fn new(rows: u16, cols: u16, stdin: Option) -> Self { Self { - parser: vt100::Parser::new(rows, cols, 1024), + output: Vec::new(), + parser: vt100::Parser::new(rows, cols, SCROLLBACK_LEN), stdin, status: None, output_logs: None, @@ -47,9 +51,19 @@ impl TerminalOutput { self.parser.screen().size() } + pub fn process(&mut self, bytes: &[u8]) { + self.parser.process(bytes); + self.output.extend_from_slice(bytes); + } + pub fn resize(&mut self, rows: u16, cols: u16) { if self.parser.screen().size() != (rows, cols) { - self.parser.screen_mut().set_size(rows, cols); + let scrollback = self.parser.screen().scrollback(); + let mut new_parser = vt100::Parser::new(rows, cols, SCROLLBACK_LEN); + new_parser.process(&self.output); + new_parser.screen_mut().set_scrollback(scrollback); + // Completely swap out the old vterm with a new correctly sized one + mem::swap(&mut self.parser, &mut new_parser); } }