Skip to main content

HeadlessTerm

Struct HeadlessTerm 

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

A headless terminal for testing without real terminal I/O.

Processes ANSI escape sequences through TerminalModel and provides assertion helpers, snapshot comparison, and export capabilities.

Implementations§

Source§

impl HeadlessTerm

Source

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

Create a new headless terminal with the given dimensions.

§Panics

Panics if width or height is 0.

Source

pub fn width(&self) -> u16

Terminal width in columns.

Source

pub fn height(&self) -> u16

Terminal height in rows.

Source

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

Current cursor position as (column, row), 0-indexed.

Source

pub fn process(&mut self, bytes: &[u8])

Process raw bytes through the terminal emulator.

Bytes are parsed as ANSI escape sequences and applied to the internal grid, just as a real terminal would.

Source

pub fn row_text(&self, row: usize) -> String

Get the text content of a single row, trimmed of trailing spaces.

Returns an empty string for out-of-bounds rows.

Source

pub fn screen_text(&self) -> Vec<String>

Get all rows as text, trimmed of trailing spaces.

Source

pub fn screen_string(&self) -> String

Get all rows as a single string joined by newlines.

Source

pub fn model(&self) -> &TerminalModel

Access the underlying TerminalModel for advanced queries.

Source

pub fn captured_output(&self) -> &[u8]

Access all captured output bytes (everything passed to process).

Source

pub fn reset(&mut self)

Reset the terminal to its initial state (blank screen, cursor at origin).

Source

pub fn assert_matches(&self, expected: &[&str])

Assert that the screen content matches the expected lines exactly.

Trailing spaces in both actual and expected lines are trimmed before comparison. The number of expected lines must match the terminal height.

§Panics

Panics with a human-readable diff if the content doesn’t match.

Source

pub fn assert_row(&self, row: usize, expected: &str)

Assert that a specific row matches the expected text.

Trailing spaces are trimmed before comparison.

§Panics

Panics if the row content doesn’t match.

Source

pub fn assert_cursor(&self, col: u16, row: u16)

Assert that the cursor is at the expected position (column, row), 0-indexed.

§Panics

Panics if the cursor position doesn’t match.

Source

pub fn diff(&self, expected: &[&str]) -> Option<ScreenDiff>

Compare screen content with expected lines and return the diff.

Returns None if the content matches exactly.

Source

pub fn export(&self, path: &Path) -> Result<()>

Export the screen content to a file for debugging.

Writes a human-readable text representation including:

  • Terminal dimensions
  • Cursor position
  • Screen content (with line numbers)
  • Captured output size
Source

pub fn export_string(&self) -> String

Export the screen content as a formatted string (for inline debugging).

Trait Implementations§

Source§

impl Debug for HeadlessTerm

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> 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, 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.