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.17.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.17-py3-none-manylinux_2_17_x86_64.whl (1.5 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

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

Uploaded Python 3manylinux: glibc 2.17+ ARM64

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

Uploaded Python 3macOS 11.0+ ARM64

htty-0.1.17-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.17.tar.gz.

File metadata

  • Download URL: htty-0.1.17.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.17.tar.gz
Algorithm Hash digest
SHA256 444aed242aac3fd9ba3c523ebef965a9c6910b2e3f61a5a251c67eae8e74c3b7
MD5 6f487953822c24a3a9963238c7e6b16a
BLAKE2b-256 f21ea8cf63c33df6eefc1ec055a9da84eb61e1a1758fe829e64b1e4ddfdbba16

See more details on using hashes here.

Provenance

The following attestation bundles were made for htty-0.1.17.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.17-py3-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for htty-0.1.17-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 ef457f6e2f43ec2597621dae85b89d4b8447c8eca7d96cff9afe2ebc0a7cf945
MD5 3cfae9b493c5ca61f09ba84ed570b587
BLAKE2b-256 7ae6c6960e05c9054dc815e7f614167bdcc06c5969d71ac1420f3e3634864e9b

See more details on using hashes here.

Provenance

The following attestation bundles were made for htty-0.1.17-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.17-py3-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for htty-0.1.17-py3-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 f560fe722e0dfc58372cd13cc79afcea75e78bc04d027ae6c5b7facbcf61b165
MD5 4c8232d857df9a01577871f75b3a8911
BLAKE2b-256 5910a5f619dcded9817d09b9c1f7bda76494fba730410157e26161b859596f89

See more details on using hashes here.

Provenance

The following attestation bundles were made for htty-0.1.17-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.17-py3-none-macosx_11_0_arm64.whl.

File metadata

  • Download URL: htty-0.1.17-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.17-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c0d44b2daa77f57eaf170b7cf532c68970453dd02291c3e7a8b2615984b573a3
MD5 5cc6463c47499c5da4c6cb17bf5841b6
BLAKE2b-256 d0bfe0a70cb7e6614fb5dd54de615568bba4c3214f25bae4494d5996b6f0c984

See more details on using hashes here.

Provenance

The following attestation bundles were made for htty-0.1.17-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.17-py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for htty-0.1.17-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a7a9baaf829e1739c3ea8a697f00c4637cf493be39b820d334628d90425dfa3c
MD5 153afe9eb560cced1c7d97ae0c6ca629
BLAKE2b-256 7771ff746602ef738cb273e1ac02e3b1bc7d2e22ccf7bff4b80c93f1d01b2619

See more details on using hashes here.

Provenance

The following attestation bundles were made for htty-0.1.17-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