Skip to main content

VirtualTerminal

Struct VirtualTerminal 

Source
pub struct VirtualTerminal { /* private fields */ }
Expand description

In-memory virtual terminal with cursor tracking and ANSI interpretation.

§Example

use ftui_pty::virtual_terminal::VirtualTerminal;

let mut vt = VirtualTerminal::new(80, 24);
vt.feed(b"Hello, World!");
assert_eq!(vt.char_at(0, 0), Some('H'));
assert_eq!(vt.char_at(12, 0), Some('!'));
assert_eq!(vt.cursor(), (13, 0));

Implementations§

Source§

impl VirtualTerminal

Source

pub fn new(width: u16, height: u16) -> Self

Create a new virtual terminal with the given dimensions.

§Panics

Panics if width or height is 0.

Source

pub fn with_quirks(width: u16, height: u16, quirks: QuirkSet) -> Self

Create a new virtual terminal with quirks enabled.

§Panics

Panics if width or height is 0.

Source

pub const fn width(&self) -> u16

Terminal width in columns.

Source

pub const fn height(&self) -> u16

Terminal height in rows.

Source

pub const fn cursor(&self) -> (u16, u16)

Current cursor position (x, y), 0-indexed.

Source

pub const fn cursor_visible(&self) -> bool

Whether the cursor is currently visible.

Source

pub const fn is_alternate_screen(&self) -> bool

Whether alternate screen mode is active.

Source

pub fn title(&self) -> &str

Current window title (set via OSC 0/2).

Source

pub const fn quirks(&self) -> QuirkSet

Active quirk set.

Source

pub fn set_quirks(&mut self, quirks: QuirkSet)

Override the active quirk set.

Source

pub fn scrollback_len(&self) -> usize

Number of lines in the scrollback buffer.

Source

pub fn set_max_scrollback(&mut self, max: usize)

Set the maximum scrollback lines.

Source

pub fn char_at(&self, x: u16, y: u16) -> Option<char>

Get the character at (x, y). Returns None if out of bounds.

Source

pub fn style_at(&self, x: u16, y: u16) -> Option<&CellStyle>

Get the style at (x, y). Returns None if out of bounds.

Source

pub fn cell_at(&self, x: u16, y: u16) -> Option<&VCell>

Get a reference to the cell at (x, y). Returns None if out of bounds.

Source

pub fn row_text(&self, y: u16) -> String

Get the text content of a row (trailing spaces trimmed).

Source

pub fn screen_text(&self) -> String

Get all visible text as a string (rows separated by newlines).

Source

pub fn scrollback_line(&self, idx: usize) -> Option<String>

Get a scrollback line by index (0 = oldest).

Source

pub fn feed(&mut self, data: &[u8])

Feed raw bytes into the terminal (ANSI-aware).

Source

pub fn feed_str(&mut self, s: &str)

Feed a string into the terminal.

Source

pub fn put_str(&mut self, s: &str)

Write a plain-text string to the terminal.

Each character is fed through put_char, which handles auto-wrap, wide characters, insert mode, and charset translation — but no ANSI escape sequences are interpreted. Use this when you have pre-sanitized text and want deterministic character-level output.

§Example
use ftui_pty::virtual_terminal::VirtualTerminal;

let mut vt = VirtualTerminal::new(10, 1);
vt.put_str("Hello");
assert_eq!(vt.row_text(0), "Hello");
assert_eq!(vt.cursor(), (5, 0));
Source

pub fn set_cursor_position(&mut self, x: u16, y: u16)

Move the cursor to an absolute position, clamped to terminal bounds.

Coordinates are 0-indexed. Out-of-range values are clamped: x to width - 1, y to height - 1.

§Example
use ftui_pty::virtual_terminal::VirtualTerminal;

let mut vt = VirtualTerminal::new(80, 24);
vt.set_cursor_position(5, 10);
assert_eq!(vt.cursor(), (5, 10));

// Out-of-range values are clamped.
vt.set_cursor_position(999, 999);
assert_eq!(vt.cursor(), (79, 23));
Source

pub fn clear(&mut self)

Clear the entire visible display, filling every cell with the current style’s background.

This is the programmatic equivalent of ESC[2J. The cursor position is not changed. Scrollback is not affected — use clear_scrollback for that.

§Example
use ftui_pty::virtual_terminal::VirtualTerminal;

let mut vt = VirtualTerminal::new(5, 1);
vt.put_str("Hello");
vt.clear();
assert_eq!(vt.row_text(0), "");
Source

pub fn clear_scrollback(&mut self)

Clear the scrollback buffer.

This removes all lines that have scrolled off the visible area. The visible display is not affected.

Source

pub fn cpr_response(&self) -> Vec<u8>

Generate a cursor position report (CPR) response. Format: ESC [ Py ; Px R (1-indexed).

Source

pub fn da1_response(&self) -> Vec<u8>

Generate a device attributes (DA1) response. Reports as a VT220 with ANSI color.

Source

pub fn put_char(&mut self, ch: char)

Place a single character at the current cursor position, applying all terminal output logic: charset translation, Unicode width detection, auto-wrap (DECAWM), wide-character handling, insert mode (IRM), and cursor advancement.

This is the character-level entry point that feed and feed_str use internally after ANSI/UTF-8 parsing. Call it directly when you already have decoded characters and want the terminal to handle wrapping, widths, and cursor movement.

Zero-width characters (combining marks, ZWJ) are silently skipped.

§Example
use ftui_pty::virtual_terminal::VirtualTerminal;

let mut vt = VirtualTerminal::new(10, 3);
vt.put_char('H');
vt.put_char('i');
assert_eq!(vt.row_text(0), "Hi");
assert_eq!(vt.cursor(), (2, 0));

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V