Skip to main content

A python wrapper around ht (a headless terminal utility)

Project description

htty

CI

htty programatically captures the appearance of a terminal application.

"htty taking a snapshot of a vim session"

It's a wrapper around a lightly modified version of ht. Which handles ANSI control sequences and gives you a friendly string instead.

You can run htty at the command line, or you can use python to import htuil.

Headless Terminal (ht)

ht connects a subprocess to a headless terminal. To understand why this is useful, consider the vim startup screen:

~                       VIM - Vi IMproved
~                       version 9.0.2136
~                   by Bram Moolenaar et al.
~          Vim is open source and freely distributable
~
~                 Help poor children in Uganda!

It looks like a string in your terminal, so it might be tempting to treat it like a string in code:

import subprocess
vimproc = subprocess.Popen(["vim"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = vimproc.communicate()
assert stdout.lines[0][1:].strip() == "VIM - Vi IMproved"

Or maybe like this:

"vim showing an error if you try to pipe its output to grep"

But these approaches won't work. If you captured vim's output you'd see that it looks quite different than what you see in your terminal:

Vi IMprovedversion 9.0.2136by Bram Moolenaar et al.Vim is open source and freely distributableHelp poor children in Uganda!

ht reads raw output like you see above and renders it as if a terminal was attached. htty provides a convenient way to use ht

htty CLI

Working with ht is a bit like having a chat session with a terminal. You make requests by writing JSON to stdin, requests like "press escape" or "take snapshot". You get responses as more JSON from stdout.

The htty CLI is not interactive like this. It aims to do everything in a single command:

  1. start the process
  2. send keys, take snapshots
  3. stop the process
  4. write the snapshots to stdout

You can take multiple snapshots in a single go:

"htty taking several snapshots of a vim session at different times"

In case you're vim-curious:

  • ihello,Escape enters insert mode types "hello" and goes back to normal mode.
  • Vyp,Escape enters line-wise visual mode with the the current line selected, yanks it, and puts it (so now there are two hello lines), and then goes back to normal mode.

For more on htty CLI usage, run htty --help or see the docs TODO: fix this link.

To understand which keys you can send, see keys.py. Anything which is not identified as a key will be sent as individual characters.

htty Python Library

As a python library, htty functions mostly like ht.

from htty import Press, ht_process, run

with ht_process("vim", rows=20, cols=50) as proc:
    snapshot = proc.snapshot()
    # ht_process terminates vim and cleans up ht on context exit

improved_line = next(
    line for line in snapshot.text.split("\n") if "IMproved" in line
)
assert improved_line == "~               VIM - Vi IMproved                 "

Alternative usage:

proc = run("vim", rows=20, cols=50)
snapshot = proc.snapshot()
improved_line = next(line for line in lines if "IMproved" in snapshot.text.split('\n'))
assert improved_line == "~               VIM - Vi IMproved                 "

proc.send_keys(":q!")
proc.send_keys(Press.ENTER)    # vim quits, but ht stays open in case you want to take another snapshot
proc.exit()                    # exit ht

For more on using htty as a python library, see the docs TODO: fix this link.

Contributing

See CONTRIBUTING.md for information on how to contribute to this project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

htty-0.1.16.tar.gz (15.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

htty-0.1.16-py3-none-manylinux_2_17_x86_64.whl (1.5 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

htty-0.1.16-py3-none-manylinux_2_17_aarch64.whl (1.5 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

htty-0.1.16-py3-none-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

htty-0.1.16-py3-none-macosx_10_9_x86_64.whl (1.4 MB view details)

Uploaded Python 3macOS 10.9+ x86-64

File details

Details for the file htty-0.1.16.tar.gz.

File metadata

  • Download URL: htty-0.1.16.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for htty-0.1.16.tar.gz
Algorithm Hash digest
SHA256 75e1f8a4ac87f8a15433b4cde2d8e3bc5844d1d30de508af33f4ab892fe354d7
MD5 b5fd8d3b19040a0e6d1e25f66d75370c
BLAKE2b-256 5a55ef2114868049269e7474f8d79248e0d253f866bb22d87aac27cd6e3f49cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for htty-0.1.16.tar.gz:

Publisher: ci.yml on MatrixManAtYrService/htty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file htty-0.1.16-py3-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for htty-0.1.16-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 a884fcdd0d68df620d0df0e4b539ead1a5a695450dc74a34d4f8a6e11f3ab603
MD5 d8ec6fee7ffefb1350ddfe1818a95b81
BLAKE2b-256 29bed68a0b22b2633a4c4f7b377577af7dcba487f7701de32922267894ab8b42

See more details on using hashes here.

Provenance

The following attestation bundles were made for htty-0.1.16-py3-none-manylinux_2_17_x86_64.whl:

Publisher: ci.yml on MatrixManAtYrService/htty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file htty-0.1.16-py3-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for htty-0.1.16-py3-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 2a178be497a95cfd541d77f69aa4a03d9f38fe2131219347b73c18abeee1f50c
MD5 d2c101cadd6afdc527272e2e3bfc6d85
BLAKE2b-256 9db934091926e9c4b61f9368313202118d84c06b9683149fb4966708b4d48d00

See more details on using hashes here.

Provenance

The following attestation bundles were made for htty-0.1.16-py3-none-manylinux_2_17_aarch64.whl:

Publisher: ci.yml on MatrixManAtYrService/htty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file htty-0.1.16-py3-none-macosx_11_0_arm64.whl.

File metadata

  • Download URL: htty-0.1.16-py3-none-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: Python 3, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for htty-0.1.16-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4629676a738dc22cc5ba11087c5a22133c4ae041280ba0b286e2f01a5f4cd9fa
MD5 bd86ae6b1c8f5277a12c327c867b3004
BLAKE2b-256 973adb5bae493b3db44fcfcca6694ca35ec551f170ed02b1951dbaaebcb0a280

See more details on using hashes here.

Provenance

The following attestation bundles were made for htty-0.1.16-py3-none-macosx_11_0_arm64.whl:

Publisher: ci.yml on MatrixManAtYrService/htty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file htty-0.1.16-py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for htty-0.1.16-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 90de7ad96968df2fc9dd66b76d42b64d3e91480cdf15593d3252974d875efb18
MD5 6be578183660ece6d9d5f06888732790
BLAKE2b-256 f58350d217c0a5016d1770928af52ebfdbd93c6ce8aa8a8f47e4df5c74265d0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for htty-0.1.16-py3-none-macosx_10_9_x86_64.whl:

Publisher: ci.yml on MatrixManAtYrService/htty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page