mod csi;
mod edit;
mod escape;
mod grid;
mod modes;
mod print;
mod stream;
#[cfg(test)]
mod tests;
use crate::ansi::{self, Token};
use crate::renderer::cell::{Cell, Link, Pen};
use crate::unicode::WidthMethod;
use grid::Grid;
use std::collections::BTreeSet;
#[derive(Debug, Clone, Copy, Default)]
struct Cursor {
x: u16,
y: u16,
wrap: bool,
}
#[derive(Debug, Clone, Copy, Default)]
struct Saved {
x: u16,
y: u16,
pen: Pen,
}
#[derive(Debug)]
pub(crate) struct Vt {
columns: u16,
rows: u16,
main: Grid,
alt: Grid,
on_alt: bool,
cursor: Cursor,
last: Option<(u16, u16)>,
pen: Pen,
link: Link,
saved: Saved,
saved_alt: Saved,
top: u16,
bottom: u16,
modes: BTreeSet<u16>,
cursor_style: u8,
title: String,
titles: Vec<String>,
scrollback: Vec<String>,
sync_frames: usize,
osc_log: Vec<String>,
unknown: Vec<String>,
partial: Vec<u8>,
method: WidthMethod,
}
impl Vt {
pub(crate) fn new(columns: u16, rows: u16) -> Self {
let (columns, rows) = (columns.max(1), rows.max(1));
Self {
columns,
rows,
main: Grid::new(columns, rows, &Cell::BLANK),
alt: Grid::new(columns, rows, &Cell::BLANK),
on_alt: false,
cursor: Cursor::default(),
last: None,
pen: Pen::DEFAULT,
link: None,
saved: Saved::default(),
saved_alt: Saved::default(),
top: 0,
bottom: rows - 1,
modes: BTreeSet::from([7, 25]),
cursor_style: 0,
title: String::new(),
titles: Vec::new(),
scrollback: Vec::new(),
sync_frames: 0,
osc_log: Vec::new(),
unknown: Vec::new(),
partial: Vec::new(),
method: WidthMethod::default(),
}
}
pub(crate) fn with_width_method(mut self, method: WidthMethod) -> Self {
self.method = method;
self
}
pub(crate) fn feed(&mut self, bytes: &[u8]) {
self.partial.extend_from_slice(bytes);
let (text, held) = stream::split_utf8(&self.partial);
let cut = stream::complete_len(&text, self.method);
self.partial = text.as_bytes()[cut..].to_vec();
self.partial.extend(held);
for token in ansi::tokens(&text[..cut], self.method) {
self.token(token);
}
}
fn token(&mut self, token: Token<'_>) {
if !keeps_last(&token) {
self.last = None;
}
match token {
Token::Text(cluster, width) => self.print(cluster, width),
Token::Control(byte) => self.control(byte),
Token::Csi { params, final_byte } => csi::dispatch(self, params, final_byte),
Token::Osc(payload) => self.osc(payload),
Token::Other(s) => self.escape(s),
}
}
pub(crate) fn resize(&mut self, columns: u16, rows: u16) {
let (columns, rows) = (columns.max(1), rows.max(1));
self.main.resize(columns, rows);
self.alt.resize(columns, rows);
(self.columns, self.rows) = (columns, rows);
(self.top, self.bottom) = (0, rows - 1);
self.move_to(self.cursor.x, self.cursor.y);
}
pub(crate) fn cell(&self, x: u16, y: u16) -> &Cell {
self.screen().cell(x, y)
}
pub(crate) fn row_text(&self, y: u16) -> String {
self.screen().row_text(y)
}
pub(crate) fn text(&self) -> String {
let mut lines: Vec<String> = (0..self.rows).map(|y| self.row_text(y)).collect();
while lines.last().is_some_and(String::is_empty) {
drop(lines.pop());
}
lines.join("\n")
}
pub(crate) fn cursor(&self) -> (u16, u16) {
(self.cursor.x, self.cursor.y)
}
pub(crate) fn cursor_visible(&self) -> bool {
self.mode(25)
}
pub(crate) fn cursor_style(&self) -> u8 {
self.cursor_style
}
pub(crate) fn alt_screen(&self) -> bool {
self.on_alt
}
pub(crate) fn autowrap(&self) -> bool {
self.mode(7)
}
pub(crate) fn mode(&self, n: u16) -> bool {
self.modes.contains(&n)
}
pub(crate) fn pen(&self) -> Pen {
self.pen
}
pub(crate) fn title(&self) -> &str {
&self.title
}
pub(crate) fn scrollback(&self) -> &[String] {
&self.scrollback
}
pub(crate) fn in_sync(&self) -> bool {
self.mode(2026)
}
pub(crate) fn sync_frames(&self) -> usize {
self.sync_frames
}
pub(crate) fn osc_log(&self) -> &[String] {
&self.osc_log
}
pub(crate) fn unknown(&self) -> &[String] {
&self.unknown
}
fn screen(&self) -> &Grid {
if self.on_alt { &self.alt } else { &self.main }
}
fn screen_mut(&mut self) -> &mut Grid {
if self.on_alt {
&mut self.alt
} else {
&mut self.main
}
}
fn blank(&self) -> Cell {
let pen = Pen {
bg: self.pen.bg,
..Pen::DEFAULT
};
Cell { pen, ..Cell::BLANK }
}
fn move_to(&mut self, x: u16, y: u16) {
self.cursor = Cursor {
x: x.min(self.columns - 1),
y: y.min(self.rows - 1),
wrap: false,
};
}
fn log(&mut self, what: String) {
self.unknown.push(what);
}
}
fn keeps_last(token: &Token<'_>) -> bool {
matches!(
token,
Token::Text(..)
| Token::Osc(_)
| Token::Csi {
final_byte: b'm',
..
}
)
}