Metadata-Version: 2.5
Name: libvt
Version: 0.0.1a5
Summary: Headless terminal emulator and VT parser for Python.
Project-URL: Bug Tracker, https://github.com/tmux-python/libvt/issues
Project-URL: Documentation, https://libvt.git-pull.com
Project-URL: Repository, https://github.com/tmux-python/libvt
Project-URL: Changes, https://github.com/tmux-python/libvt/blob/master/CHANGES
Author-email: Tony Narlock <tony@git-pull.com>
License: MIT
License-File: LICENSE
Keywords: ansi,emulator,pytest,terminal,tmux,vt,vt100,vt500
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Framework :: Pytest
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Terminals :: Terminal Emulators/X Terminals
Classifier: Typing :: Typed
Requires-Python: <4.0,>=3.12
Provides-Extra: pytest
Requires-Dist: pytest>=8.0; extra == 'pytest'
Provides-Extra: syrupy
Requires-Dist: pytest>=8.0; extra == 'syrupy'
Requires-Dist: syrupy>=7.0.3; extra == 'syrupy'
Description-Content-Type: text/markdown

# libvt

[![PyPI version](https://img.shields.io/pypi/v/libvt.svg)](https://pypi.org/project/libvt/)
[![tests](https://github.com/tmux-python/libvt/actions/workflows/tests.yml/badge.svg)](https://github.com/tmux-python/libvt/actions/workflows/tests.yml)
[![license](https://img.shields.io/badge/license-MIT-blue)](https://github.com/tmux-python/libvt/blob/master/LICENSE)

`libvt` is a headless terminal emulator and VT parser for Python. Feed it
terminal output to inspect screen contents, cursor positions, and cell styles,
or use it to test terminal programs.

Requires Python 3.12 or later (CPython and PyPy). Written in Python, with type
annotations and no runtime dependencies.

## Installation

Install with `uv`:

```console
$ uv add "libvt>=0.0.1a5,<0.1"
```

Install with `pip`:

```console
$ pip install "libvt>=0.0.1a5,<0.1"
```

Optional extras install the `pytest` fixture plugin (`libvt[pytest]`) or the
`syrupy` `.ambr` snapshot adapter (`libvt[syrupy]`):

```console
$ uv add --dev "libvt[pytest]>=0.0.1a5,<0.1"
```

```console
$ uv add --dev "libvt[syrupy]>=0.0.1a5,<0.1"
```

`libvt` is in pre-alpha (`0.0.1a5`). Minor and `0.0.x` releases may include
breaking API changes; pin `libvt>=0.0.1a5,<0.1` while the package is pre-1.0.

## Quickstart

### Feed bytes and inspect immutable snapshots

Each call to [`feed()`][feed] updates the screen. Read live text and cursor
coordinates directly, or capture an immutable [`Snapshot`][committed] that holds
the last completed frame during synchronized redraws (`?2026`) and pages
backward through scrollback with [`viewport()`][viewport]:

```python
>>> import libvt
>>> from libvt.testing import serialize_vt_snapshot
>>> term = libvt.Terminal(rows=2, cols=20)
>>> term.feed(b"booting\r\nLoading 10%")
>>> term.feed(b"\r\x1b[2K\x1b[1;32mLoading 20%\x1b[0m\r\ndone")
>>> snap = term.committed
>>> print(serialize_vt_snapshot(snap))
size=20x2 cursor=(1,4,vis=True) alt=False modes=[]
┌────────────────────┐
│Loading 20%         │
│done                │
└────────────────────┘
styles:
  r0[0..11]: bold fg=green
>>> snap.viewport(offset=1).lines
('booting', 'Loading 20%')
```

### Assert screen boxes inline

Most unit tests can stop at [`assert_screen_box()`][assert_screen_box], which
checks visible text, wide-character column alignment, and cursor coordinates
inline without extra dependencies or snapshot files:

```python
>>> import libvt
>>> from libvt.testing import assert_screen_box
>>> t = libvt.Terminal(rows=2, cols=8)
>>> t.feed("日本ok\r\ndone".encode())
>>> assert_screen_box(
...     t,
...     """
...     ┌────────┐
...     │日本ok  │
...     │done    │
...     └────────┘
...     """,
...     cursor=(1, 4),
... )
```

### Serialize and scrub multi-row TUIs (`libvt.testing`)

When a TUI, progress panel, or `tmux` layout spans many styled rows or dynamic
IDs, [`vt_screen()`][vt_screen] and [`serialize_vt_snapshot()`][serialize_vt_snapshot]
in `libvt.testing` emit escape-free boxes with run-length `styles:` spans while
absorbing regex scrubber width changes inside `│` borders with no extra
dependencies (`unittest`, `assert`, `inline-snapshot`, or `pytest-regressions`):

```python
>>> import libvt
>>> from libvt.testing import serialize_vt_snapshot, vt_screen
>>> ui = libvt.Terminal(rows=3, cols=22)
>>> ui.feed(
...     b"\x1b[?1049h\x1b[H"
...     b"\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
...     b"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
...     b"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
...     b"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
...     b"\xe2\x94\x80\xe2\x94\x90\r\n"
...     b"\xe2\x94\x82 \x1b[1;32mREADY\x1b[0m  pid=48210   \xe2\x94\x82\r\n"
...     b"\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
...     b"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
...     b"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
...     b"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
...     b"\xe2\x94\x80\xe2\x94\x98"
... )
>>> print(serialize_vt_snapshot(vt_screen(ui, scrubbers=[(r"pid=\d+", "pid=<PID>")])))
size=22x3 cursor=(2,21,vis=True) alt=True modes=[]
┌──────────────────────┐
│┌────────────────────┐│
││ READY  pid=<PID>   ││
│└────────────────────┘│
└──────────────────────┘
styles:
  r1[2..7]: bold fg=green
```

### Test with `pytest` fixtures (`libvt[pytest]`)

[`libvt.pytest_plugin`][pytest_plugin] (`libvt[pytest]`) provides `vt_terminal`
(wired to a 4 KiB `FlightRecorder`), `vt_factory`, `vt_recorder`, `vt_parser`,
and POSIX `vt_pty` fixtures with no snapshot-tool dependencies:

```python
>>> import libvt
>>> from libvt.pytest_plugin import TerminalFactory
>>> from libvt.testing import assert_screen_box
>>> def test_prompt(vt_factory: TerminalFactory) -> None:
...     term = vt_factory(rows=2, cols=10)
...     term.feed(b"\x1b[1;32mOK\x1b[0m\r\nready> ")
...     assert_screen_box(
...         term,
...         """
...         ┌──────────┐
...         │OK        │
...         │ready>    │
...         └──────────┘
...         """,
...         cursor=(1, 7),
...     )
>>> test_prompt(libvt.Terminal)
```

### Persist `.ambr` snapshots (`libvt[syrupy]`)

[`libvt.contrib.syrupy`][contrib_syrupy] (`libvt[syrupy]`) adapts
`libvt.testing` into `VtSnapshotExtension` for `syrupy` `.ambr` golden files and
conditionally registers [`vt_snapshot`][vt_snapshot] when `syrupy` is active:

```python
>>> import libvt
>>> from libvt.contrib.syrupy import VtSnapshotExtension
>>> term = libvt.Terminal(rows=1, cols=12)
>>> term.feed(b"\x1b[1;32mPASS\x1b[0m")
>>> print(VtSnapshotExtension().serialize(term))
size=12x1 cursor=(0,4,vis=True) alt=False modes=[]
┌────────────┐
│PASS        │
└────────────┘
styles:
  r0[0..4]: bold fg=green
```

### Render, strip, and explain VT byte streams

[`render()`][render] returns screen text after processing terminal output.
[`strip()`][strip] removes control sequences without applying cursor movements.
[`explain()`][explain] identifies the sequences in a byte stream:

```python
>>> import libvt
>>> from libvt.codec import explain
>>> libvt.render(b"\x1b[1;32mready\x1b[0m\r\nitem 1", rows=4, cols=20)
'ready\nitem 1'
>>> libvt.strip(b"\x1b[31merror:\x1b[0m failed")
'error: failed'
>>> [s.mnemonic for s in explain(b"\x1b[31merror:\x1b[0m")]
['SGR', 'TEXT', 'SGR']
```

## Features

- Check screen contents and cursor positions inline or with `.ambr` `SGR`
  snapshots, showing expected and actual screens on failure.
- Handle wide characters and emoji sequences using Unicode 17 width and
  grapheme rules.
- Keep scrollback and reflow wrapped lines when the primary screen is resized.
- Inspect completed redraws from applications that use synchronized output
  (`?2026`).
- Encode keyboard, mouse, focus, and paste events according to the active
  terminal modes.
- Run terminal programs on POSIX PTYs or connect to `asyncio` streams.

Read `tmux` captures as screen cells and decode control-mode output with
[`libvt.contrib.tmux`](https://libvt.git-pull.com/api/contrib_tmux/). Full
guides and API reference are at [libvt.git-pull.com](https://libvt.git-pull.com/).

## License

MIT License. See [LICENSE](LICENSE).

[feed]: https://libvt.git-pull.com/api/terminal/#libvt.terminal.Terminal.feed
[committed]: https://libvt.git-pull.com/api/terminal/#libvt.terminal.Terminal.committed
[viewport]: https://libvt.git-pull.com/api/terminal/#libvt.terminal.Snapshot.viewport
[assert_screen_box]: https://libvt.git-pull.com/api/testing/#libvt.testing.assert_screen_box
[contrib_syrupy]: https://libvt.git-pull.com/api/contrib_syrupy/
[pytest_plugin]: https://libvt.git-pull.com/api/pytest_plugin/
[vt_snapshot]: https://libvt.git-pull.com/api/contrib_syrupy/#libvt.contrib.syrupy.vt_snapshot
[vt_screen]: https://libvt.git-pull.com/api/testing/#libvt.testing.vt_screen
[serialize_vt_snapshot]: https://libvt.git-pull.com/api/testing/#libvt.testing.serialize_vt_snapshot
[render]: https://libvt.git-pull.com/api/terminal/#libvt.terminal.render
[strip]: https://libvt.git-pull.com/api/terminal/#libvt.terminal.strip
[explain]: https://libvt.git-pull.com/api/codec/#libvt.codec.explain
