libvt

Headless terminal emulator and VT parser for Python.

class libvt.Appearance
class libvt.Appearance

Bases: object

Immutable terminal theme, dynamic colors, and palette overrides.

References

Examples

>>> from libvt.config import ColorScheme
>>> from libvt.style import Appearance
>>> app = Appearance(scheme=ColorScheme.LIGHT, bg_rgb=(255, 255, 255))
>>> (app.scheme, app.bg_rgb)
(<ColorScheme.LIGHT: 2>, (255, 255, 255))
class libvt.Cell
class libvt.Cell

Bases: object

Immutable view of a single terminal grid cell.

Examples

>>> term = Terminal(rows=1, cols=8)
>>> term.feed("\x1b[1;32m日\x1b[0mok".encode())
>>> snap = term.committed
>>> snap[0, 0].text, snap[0, 0].width, snap[0, 0].style.bold
('日', 2, True)
>>> snap[0, 1].width_role.name, snap[0, 1].width
('SPACER_TAIL', 0)
class libvt.Event
class libvt.Event

Bases: object

Open nominal base class for structured terminal events.

Built-in and integration-defined event records inherit from Event with keyword-only fields so new attributes can be added with defaults without breaking callers or match class patterns. Use KnownEvent when static exhaustiveness over built-in core events is desired.

Examples

>>> from dataclasses import dataclass
>>> from libvt.events import Bell, Event
>>> isinstance(Bell(), Event)
True
>>> @dataclass(frozen=True, slots=True, kw_only=True, match_args=False)
... class CustomMuxEvent(Event):
...     pane: str
>>> issubclass(CustomMuxEvent, Event)
True
exception libvt.LibVtError
exception[source]
exception[source]
exception libvt.LibVtError

Bases: Exception

Base exception for all libvt errors.

class libvt.Limits
class libvt.Limits

Bases: object

Resource ceilings protecting memory and CPU against hostile byte streams.

class libvt.Policy
class libvt.Policy

Bases: object

Default-deny host security and strictness policy.

class libvt.Profile
class libvt.Profile

Bases: object

Peer-neutral terminal capability profile composed of domain groups.

References

Examples

>>> from libvt.config import Profile, ScreenBehavior
>>> prof = Profile(screen=ScreenBehavior(cell_width_px=10, cell_height_px=20))
>>> (prof.screen.cell_width_px, prof.screen.cell_height_px)
(10, 20)
exception libvt.ProtocolError
exception[source]
exception[source]
exception libvt.ProtocolError

Bases: LibVtError

Raised when Policy.strict rejects a malformed or unsupported sequence.

class libvt.Screen
class libvt.Screen

Bases: object

Mutable 2D character grid with FrozenRow RLE scrollback and reflow.

Examples

>>> term = Terminal(rows=2, cols=10)
>>> term.feed(b"line 1\r\nline 2\r\nline 3")
>>> term.screen.lines, [r.text for r in term.screen.history]
(('line 2', 'line 3'), ['line 1'])
class libvt.Snapshot
class libvt.Snapshot

Bases: object

Immutable point-in-time capture of terminal screen, history, and metadata.

Obtain instances via committed (tear-free across ?2026 synchronized frames) or snapshot() (immediate working buffer). Once captured, a Snapshot never mutates when later bytes arrive on the Terminal and can be shared across threads without locking.

Index snap[row] for a RowView, snap[row, col] for an immutable Cell, or snap[r0:r1, c0:c1] for a rectangular RegionView. Render a visual grid box with format_screen_box() or include header metadata and SGR spans with serialize_vt_snapshot().

Examples

>>> from libvt.terminal import Terminal
>>> from libvt.testing import serialize_vt_snapshot
>>> term = Terminal(rows=2, cols=14)
>>> term.feed(b"\x1b[1;32mREADY\x1b[0m  api\r\n\x1b[7m> worker\x1b[0m")
>>> snap = term.committed
>>> print(serialize_vt_snapshot(snap))
size=14x2 cursor=(1,8,vis=True) alt=False modes=[]
┌──────────────┐
│READY  api    │
│> worker      │
└──────────────┘
styles:
  r0[0..5]: bold fg=green
  r1[0..8]: reverse
>>> snap[0, 0].text, snap[0, 0].style.bold, snap[1, 0].style.reverse
('R', True, True)
class libvt.Style
class libvt.Style

Bases: object

Immutable SGR graphic rendition attributes for a cell or active pen.

class libvt.Terminal
class libvt.Terminal

Bases: object

Headless terminal emulator over a streaming VT500 parser.

Every method call and attribute read on a Terminal instance (including capturing state via snapshot() or committed) belongs to a single owning thread or asyncio event loop unless externally synchronized. Returned Snapshot values are immutable and safe to pass across threads without locking.

Examples

>>> from libvt.terminal import Terminal
>>> from libvt.testing import serialize_vt_snapshot
>>> term = Terminal(rows=2, cols=16)
>>> term.feed(b"\x1b[1;32mBUILD\x1b[0m ok\r\n2 passed")
>>> print(serialize_vt_snapshot(term.committed))
size=16x2 cursor=(1,8,vis=True) alt=False modes=[]
┌────────────────┐
│BUILD ok        │
│2 passed        │
└────────────────┘
styles:
  r0[0..5]: bold fg=green
libvt.render(data, /, *, rows=24, cols=80, profile=DEFAULT_PROFILE, newline_mode=False, unwrap=False)
function[source]
function[source]
libvt.render(data, /, *, rows=24, cols=80, profile=DEFAULT_PROFILE, newline_mode=False, unwrap=False)

Emulate data on a fresh (rows, cols) terminal and return screen text.

Parameters:
  • data (collections.abc.Buffer | str) – Raw VT byte stream or UTF-8 string to feed.

  • rows (int) – Screen height in rows.

  • cols (int) – Screen width in columns.

  • profile (Profile) – Compatibility profile governing emulation quirks.

  • newline_mode (bool) – When True, enable ANSI LNM (LF implies CR).

  • unwrap (bool) – When True, return Terminal.unwrapped_text with soft-wrapped rows joined instead of Terminal.text.

Returns:

Rendered visible text with trailing blank rows omitted.

Return type:

str

Examples

>>> import libvt
>>> libvt.render(b"loading 10%\r\x1b[2Kloading 100%", rows=1, cols=20)
'loading 100%'
>>> libvt.render(b"hello world", rows=2, cols=8, unwrap=True)
'hello world'
libvt.strip(data, /)
function[source]
function[source]
libvt.strip(data, /)

Strip VT escape, CSI, OSC, DCS, and APC sequences, keeping plain text.

Unlike render(), strip() removes control sequences without applying cursor movements or line erasures.

Examples

>>> import libvt
>>> libvt.strip("\x1b[1;32mPASS\x1b[0m\n")
'PASS\n'
Parameters:

data (Buffer | str)

Return type:

str