A python wrapper around ht (a headless terminal utility)
Project description
htty
htty programatically captures the appearance of a terminal application.
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:
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 IMproved[6;37Hversion 9.0.2136[7;33Hby Bram Moolenaar et al.[8;24HVim is open source and freely distributable[10;32HHelp 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:
- start the process
- send keys, take snapshots
- stop the process
- write the snapshots to stdout
You can take multiple snapshots in a single go:
In case you're vim-curious:
ihello,Escapeenters insert mode types "hello" and goes back to normal mode.Vyp,Escapeenters 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a16c139c4c8773903cc7b7f563107e29f5f611ebf5a8b0c91cfead8d976f7fc
|
|
| MD5 |
3694f5b1b86f840641f12d10e454babc
|
|
| BLAKE2b-256 |
555a3722263bbcaf859c670be124881e6d54caf3d36d70a48d1b5f898d85238e
|
Provenance
The following attestation bundles were made for htty-0.1.18.tar.gz:
Publisher:
ci.yml on MatrixManAtYrService/htty
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
htty-0.1.18.tar.gz -
Subject digest:
6a16c139c4c8773903cc7b7f563107e29f5f611ebf5a8b0c91cfead8d976f7fc - Sigstore transparency entry: 246113692
- Sigstore integration time:
-
Permalink:
MatrixManAtYrService/htty@2a08e1dff08bdd5a2f6fce776c332fa606e4d0b1 -
Branch / Tag:
refs/tags/v0.1.18 - Owner: https://github.com/MatrixManAtYrService
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@2a08e1dff08bdd5a2f6fce776c332fa606e4d0b1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file htty-0.1.18-py3-none-manylinux_2_17_x86_64.whl.
File metadata
- Download URL: htty-0.1.18-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc0335e83d268e248ffbdd73c8f4275a4e7aa01b8a89a5096902a262883fc94d
|
|
| MD5 |
3eacb8991f32631fe62b8c158cf7a07b
|
|
| BLAKE2b-256 |
cf7fe8b2b2f730209c454bd91addbf8b4b760edf89e45606f27e7f6c617e343f
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
htty-0.1.18-py3-none-manylinux_2_17_x86_64.whl -
Subject digest:
dc0335e83d268e248ffbdd73c8f4275a4e7aa01b8a89a5096902a262883fc94d - Sigstore transparency entry: 246113694
- Sigstore integration time:
-
Permalink:
MatrixManAtYrService/htty@2a08e1dff08bdd5a2f6fce776c332fa606e4d0b1 -
Branch / Tag:
refs/tags/v0.1.18 - Owner: https://github.com/MatrixManAtYrService
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@2a08e1dff08bdd5a2f6fce776c332fa606e4d0b1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file htty-0.1.18-py3-none-manylinux_2_17_aarch64.whl.
File metadata
- Download URL: htty-0.1.18-py3-none-manylinux_2_17_aarch64.whl
- Upload date:
- Size: 1.5 MB
- Tags: Python 3, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
084d750f41520a46c40c73b80d97f8eca046732f245f0f659dcf260b195ad7d0
|
|
| MD5 |
810f824882550724faecb32d8140f863
|
|
| BLAKE2b-256 |
c70fbf3822f4ebb731b5cb65bbe14acce4cd93b2409754319008362c18d6fcaa
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
htty-0.1.18-py3-none-manylinux_2_17_aarch64.whl -
Subject digest:
084d750f41520a46c40c73b80d97f8eca046732f245f0f659dcf260b195ad7d0 - Sigstore transparency entry: 246113699
- Sigstore integration time:
-
Permalink:
MatrixManAtYrService/htty@2a08e1dff08bdd5a2f6fce776c332fa606e4d0b1 -
Branch / Tag:
refs/tags/v0.1.18 - Owner: https://github.com/MatrixManAtYrService
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@2a08e1dff08bdd5a2f6fce776c332fa606e4d0b1 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bcdda0a065dc6c1925958534459b7fd29f89e2344e83c48c6e8b596bca6ce76
|
|
| MD5 |
f7ee52802b066398bfb768912f765d21
|
|
| BLAKE2b-256 |
196c9ec19ddda580b0a8297cbb9f5a857fc2d79128792f6f39b600a13dbdfee1
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
htty-0.1.18-py3-none-macosx_11_0_arm64.whl -
Subject digest:
7bcdda0a065dc6c1925958534459b7fd29f89e2344e83c48c6e8b596bca6ce76 - Sigstore transparency entry: 246113695
- Sigstore integration time:
-
Permalink:
MatrixManAtYrService/htty@2a08e1dff08bdd5a2f6fce776c332fa606e4d0b1 -
Branch / Tag:
refs/tags/v0.1.18 - Owner: https://github.com/MatrixManAtYrService
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@2a08e1dff08bdd5a2f6fce776c332fa606e4d0b1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file htty-0.1.18-py3-none-macosx_10_9_x86_64.whl.
File metadata
- Download URL: htty-0.1.18-py3-none-macosx_10_9_x86_64.whl
- Upload date:
- Size: 1.4 MB
- Tags: Python 3, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
271f5f5ce4cd758e60116cc79a522dab6656bbd1f971c9c5899dc448d17aeacf
|
|
| MD5 |
e4508ba6b86378749fb7b7cf40fe3234
|
|
| BLAKE2b-256 |
7fa8a40189e284f72d71a836200fc45c04bd7e5de61c48e5afe5d5b7a3cd7312
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
htty-0.1.18-py3-none-macosx_10_9_x86_64.whl -
Subject digest:
271f5f5ce4cd758e60116cc79a522dab6656bbd1f971c9c5899dc448d17aeacf - Sigstore transparency entry: 246113698
- Sigstore integration time:
-
Permalink:
MatrixManAtYrService/htty@2a08e1dff08bdd5a2f6fce776c332fa606e4d0b1 -
Branch / Tag:
refs/tags/v0.1.18 - Owner: https://github.com/MatrixManAtYrService
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@2a08e1dff08bdd5a2f6fce776c332fa606e4d0b1 -
Trigger Event:
push
-
Statement type: