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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

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

Uploaded Python 3manylinux: glibc 2.17+ x86-64

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

Uploaded Python 3macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: htty-0.1.9-py3-none-manylinux_2_17_x86_64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: Python 3, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for htty-0.1.9-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 56cc0306b2b057457a8b496b74a38e14f13b11131e83f1c3a6c929ede3218cb0
MD5 70cc887611e1bdbf8409901514fd3725
BLAKE2b-256 ed6f13806b6978af45fa46e64157b4f466f8a3b1f72898e4e0de36af6c3072d0

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: htty-0.1.9-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.9-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f599677db355e36bcf51b5c97096abed2169868702ff4b6bc30e1b1ab9c39db1
MD5 94890a5772b91ba04c21cd766131f63b
BLAKE2b-256 3712133aa797d7541ba2332cce540ed57a96af83cbfefe527a08371a198df3eb

See more details on using hashes here.

Provenance

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

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