use vte::{Params, Parser, Perform};
#[must_use]
pub fn render_terminal(text: &str) -> String {
let mut screen = LineRenderer::with_capacity(text.len());
let mut parser = Parser::new();
parser.advance(&mut screen, text.as_bytes());
screen.finish()
}
struct LineRenderer {
rendered: String,
line: Vec<char>,
cursor: usize,
}
impl LineRenderer {
fn with_capacity(capacity: usize) -> Self {
Self {
rendered: String::with_capacity(capacity),
line: Vec::new(),
cursor: 0,
}
}
fn finish(mut self) -> String {
self.rendered.extend(self.line.drain(..));
self.rendered
}
fn end_line(&mut self) {
self.rendered.extend(self.line.drain(..));
self.rendered.push('\n');
self.cursor = 0;
}
fn put(&mut self, c: char) {
while self.line.len() < self.cursor {
self.line.push(' ');
}
if self.cursor == self.line.len() {
self.line.push(c);
} else {
self.line[self.cursor] = c;
}
self.cursor += 1;
}
fn erase_in_line(&mut self, mode: u16) {
match mode {
0 => self.line.truncate(self.cursor.min(self.line.len())),
1 => {
let upto = self.cursor.min(self.line.len());
for cell in &mut self.line[..upto] {
*cell = ' ';
}
}
2 => self.line.clear(),
_ => {}
}
}
}
fn first_param(params: &Params, default: u16) -> u16 {
match params
.iter()
.next()
.and_then(|group| group.first().copied())
{
Some(0) | None => default,
Some(value) => value,
}
}
impl Perform for LineRenderer {
fn print(&mut self, c: char) {
self.put(c);
}
fn execute(&mut self, byte: u8) {
match byte {
b'\n' | 0x0b | 0x0c => self.end_line(),
b'\r' => self.cursor = 0,
b'\t' => self.put('\t'),
0x08 => self.cursor = self.cursor.saturating_sub(1),
_ => {}
}
}
fn csi_dispatch(&mut self, params: &Params, _intermediates: &[u8], ignore: bool, action: char) {
if ignore {
return;
}
match action {
'G' | '`' => self.cursor = (first_param(params, 1) as usize).saturating_sub(1),
'C' => self.cursor = self.cursor.saturating_add(first_param(params, 1) as usize),
'D' => self.cursor = self.cursor.saturating_sub(first_param(params, 1) as usize),
'K' => self.erase_in_line(first_param(params, 0)),
_ => {}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn strips_sgr_colour_sequences_without_residue() {
let coloured = "\u{1b}[1m\u{1b}[32m Compiling\u{1b}[0m aither-sandbox v0.1.0\n";
assert_eq!(
render_terminal(coloured),
" Compiling aither-sandbox v0.1.0\n"
);
}
#[test]
fn renders_carriage_return_progress_as_last_frame() {
let progress = "Downloading 10%\rDownloading 55%\rDownloading 100%\n";
assert_eq!(render_terminal(progress), "Downloading 100%\n");
}
#[test]
fn erase_in_line_clears_longer_previous_frame() {
let spinner = "working on a long step\r\u{1b}[2Kdone\n";
assert_eq!(render_terminal(spinner), "done\n");
}
#[test]
fn carriage_return_newline_ends_the_line() {
assert_eq!(render_terminal("alpha\r\nbeta\r\n"), "alpha\nbeta\n");
}
#[test]
fn lone_carriage_return_at_end_keeps_the_frame() {
assert_eq!(render_terminal("progress 99%\r"), "progress 99%");
}
#[test]
fn carriage_return_at_end_still_rewinds_for_what_follows() {
assert_eq!(render_terminal("abc\r\u{1b}[2Kxyz"), "xyz");
}
#[test]
fn output_without_a_final_newline_gains_none() {
assert_eq!(render_terminal("abc"), "abc");
assert_eq!(render_terminal("first\nsecond"), "first\nsecond");
}
#[test]
fn column_absolute_rewrites_from_the_requested_column() {
assert_eq!(render_terminal("abcdef\u{1b}[3GXY\n"), "abXYef\n");
}
#[test]
fn backspace_rewinds_one_column() {
assert_eq!(render_terminal("xyz\u{8}w\n"), "xyw\n");
}
#[test]
fn tabs_and_trailing_spaces_survive() {
assert_eq!(render_terminal("a\tb \n"), "a\tb \n");
}
#[test]
fn osc_window_title_leaves_no_text() {
assert_eq!(
render_terminal("\u{1b}]0;cargo test\u{7}running 1 test\n"),
"running 1 test\n"
);
}
#[test]
fn csi_with_too_many_parameters_leaves_no_residue() {
let params = (0..40)
.map(|value| value.to_string())
.collect::<Vec<_>>()
.join(";");
assert_eq!(render_terminal(&format!("x\u{1b}[{params}Gy\n")), "xy\n");
}
#[test]
fn two_character_escape_consumes_its_final_byte() {
assert_eq!(render_terminal("a\u{1b}cb\n"), "ab\n");
}
#[test]
fn trailing_escape_at_end_of_stream_is_dropped() {
assert_eq!(render_terminal("tail\u{1b}["), "tail");
}
#[test]
fn null_and_bell_bytes_are_dropped() {
assert_eq!(render_terminal("hello\0world\u{7}!\n"), "helloworld!\n");
}
#[test]
fn empty_input_renders_empty() {
assert_eq!(render_terminal(""), "");
}
#[test]
fn multibyte_text_is_preserved() {
assert_eq!(render_terminal("héllo → 世界\n"), "héllo → 世界\n");
}
}