diff --git a/CHANGES b/CHANGES index 2a10109..a651601 100644 --- a/CHANGES +++ b/CHANGES @@ -66,6 +66,12 @@ Deleting an attribute on {class}`~libvt.snapshot.Snapshot` now raises ### Documentation +#### Drive a Python child from the README (#131) + +The README quickstart uses {func}`~libvt.aio.open_pty` and +{meth}`~libvt.aio.PtySession.wait_for` to start a Python child in a POSIX +terminal, send input, and assert the child's committed response on screen. + #### Test selection and cancellation in fzf (#138) The {ref}`fzf recipe ` drives a live picker in a private diff --git a/README.md b/README.md index 3c4d7e0..ea3fcfb 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,34 @@ styles: Use [`Snapshot.find_cells()`][find_cells] to turn a visible label into cell coordinates for a click, highlight, or assertion. +### Drive a terminal program + +On POSIX, use [`open_pty()`][open_pty] to launch a program, send input, and +wait for a committed [`Snapshot`][snapshot] showing the child's response. The +child prints `Taken: key` only after it reads the command: + +```python +>>> import asyncio +>>> import os +>>> import sys +>>> from libvt import Snapshot +>>> from libvt.aio import open_pty + +>>> async def take_key() -> Snapshot: +... child = "print('Quay'); print('Taken: ' + input())" +... async with open_pty([sys.executable, "-u", "-c", child]) as session: +... await session.wait_for("Quay", timeout=5) +... session.paste("key") +... session.send_key("enter") +... return await session.wait_for( +... lambda snap: "Taken: key" in snap.lines, timeout=5 +... ) + +>>> if os.name == "posix": +... snap = asyncio.run(take_key()) +... assert "Taken: key" in snap.lines +``` + ### Assert screen boxes inline Most unit tests can stop at [`assert_screen_box()`][assert_screen_box], which @@ -224,6 +252,8 @@ MIT License. See [LICENSE](LICENSE). [find_cells]: https://libvt.git-pull.com/topics/snapshots/#locate-text-by-cell-coordinates [committed]: https://libvt.git-pull.com/api/terminal/#libvt.terminal.Terminal.committed [viewport]: https://libvt.git-pull.com/api/snapshot/#libvt.snapshot.Snapshot.viewport +[open_pty]: https://libvt.git-pull.com/api/aio/pty/#libvt.aio.open_pty +[snapshot]: https://libvt.git-pull.com/api/snapshot/#libvt.snapshot.Snapshot [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/