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.18.tar.gz (16.0 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.18-py3-none-manylinux_2_17_x86_64.whl (1.5 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

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

Uploaded Python 3manylinux: glibc 2.17+ ARM64

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

Uploaded Python 3macOS 11.0+ ARM64

htty-0.1.18-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.18.tar.gz.

File metadata

  • Download URL: htty-0.1.18.tar.gz
  • Upload date:
  • Size: 16.0 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.18.tar.gz
Algorithm Hash digest
SHA256 6a16c139c4c8773903cc7b7f563107e29f5f611ebf5a8b0c91cfead8d976f7fc
MD5 3694f5b1b86f840641f12d10e454babc
BLAKE2b-256 555a3722263bbcaf859c670be124881e6d54caf3d36d70a48d1b5f898d85238e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for htty-0.1.18-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 dc0335e83d268e248ffbdd73c8f4275a4e7aa01b8a89a5096902a262883fc94d
MD5 3eacb8991f32631fe62b8c158cf7a07b
BLAKE2b-256 cf7fe8b2b2f730209c454bd91addbf8b4b760edf89e45606f27e7f6c617e343f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for htty-0.1.18-py3-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 084d750f41520a46c40c73b80d97f8eca046732f245f0f659dcf260b195ad7d0
MD5 810f824882550724faecb32d8140f863
BLAKE2b-256 c70fbf3822f4ebb731b5cb65bbe14acce4cd93b2409754319008362c18d6fcaa

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: htty-0.1.18-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.18-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7bcdda0a065dc6c1925958534459b7fd29f89e2344e83c48c6e8b596bca6ce76
MD5 f7ee52802b066398bfb768912f765d21
BLAKE2b-256 196c9ec19ddda580b0a8297cbb9f5a857fc2d79128792f6f39b600a13dbdfee1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for htty-0.1.18-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 271f5f5ce4cd758e60116cc79a522dab6656bbd1f971c9c5899dc448d17aeacf
MD5 e4508ba6b86378749fb7b7cf40fe3234
BLAKE2b-256 7fa8a40189e284f72d71a836200fc45c04bd7e5de61c48e5afe5d5b7a3cd7312

See more details on using hashes here.

Provenance

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