Terminal TTY IO Control
Project description
author: gk version: 2021.05.02
termcontrol
Terminal Command Mode Support & I/O Multiplexing
The tool is a slightly extended terminal I/O stream hijacker, based on https://github.com/j3parker/hijack.git.
But, instead of just forwarding stdin into the application, it allows to modify it, dynamically, before it hits the app. Stdout remains untouched, i.e. keeps as produced by the application.
Termcontrol also features a Python convenience wrapper.
Purpose
Multiplexing of Output
Tailing the out fifo duplicates the application's output into any other TTY byte by byte, i.e. not line-buffered,
thus producing better results than tee/tail -n
combinations - your colleague can watch you do, w/o having to use
tmux or screen, simply via tail -f
on the output fifo.
Note: If you want
only
this, then the omnipresentscript
utility is a better alternative:script -f <other tty>
on 2 terminals, with thetty
command delivering their names. And, as for any use case related to stream control,socat
would also be candidate - if you know how to configure it...
Modification of Input
Stdin
Via command line parameters you can parametrize the tool to modify the stdin stream before forwarding it into the application, so that the application can bind to - and see hotkey combinations, w/o you actually typing them. This enables, with limitations, something like mode support (like vim's insert and normal mode), for otherwise mode unaware apps, e.g. fzf or ncdu, which you would have to reload to get new bindings. See the screencast below for an example.
External
Stdin is combined with what is read from the "in" fifo. Means:
Any process, internal or external, with write perms on that fifo can control the app, incl. pressing hotkey combinations, simply by file writing of the appropriate bytes into the "in" fifo, e.g. 0x97
for a
, 0x270x97
for Alt-a
or 0x4
for CTRL-D.
Furthermore, termcontrol's own modification behaviour can be controlled by this, i.e. from inside or outside.
Example use case: The app offers a refresh hot key for the user and can spawn subprocesses. With termcontrol, such a subprocess can trigger the refresh hotkey on its parent process once done, so the user sees the update automatically, when produced.
Performance
When nodbody connects to the out fifo, then app performance is basically unchanged.
Security
Only based on file permissions - root can always see what you see - and type (he could that anyways, just not that convenient).
Example
This is an application which uses fzf from a python process, to let the user select and filter lists.
- It uses termcontrol's python API in its main method, in order to get restarted within termcontrol like this:
from termcontrol import wrap_process
d_io = wrap_process(cmd_mode=True, cmd_upper=True, ins_mode='/', cmd_signal=101,)
which means all lower case letters are converted to upper case ones already from app start - until '/' is entered.
-
Termcontrol by default listens to fast typed
jk
combinations, switching back to command mode. -
The
101
causes termcontrol to send analt-e
(=alt+chr(101)
) into the app, when this happens. -
When running fzf, the python calling process configures it to bind (uppercase) K/J to up/down and H/L to application specific back/forward actions, plus has it change the prompt, based on
alt+e
or '/' seen, all features which fzf provides out of the box. -
When user hits
/
, termcontrol is switched to 'insert' (i.e. filter) mode, not modifying what is typed - j and k keys then filter the lists. -
The app can, from within, switch to control (normal) mode at every new list presented to the user, by calling the python api (which writes control sequences into the 'in' fifo).
Testing
- pytest must run on Linux, w/o capture mode (
-s
) - xdotool is used to send stdin keystrokes into the test program
Installation
pip install termcontrol
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 Distribution
File details
Details for the file termcontrol-2021.5.2.tar.gz
.
File metadata
- Download URL: termcontrol-2021.5.2.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 378463cf7fa20adc0d2c67153ec28084d9be12d125a8581bee240940071fb226 |
|
MD5 | a69287ff18403dff96b8aa0f7f180e56 |
|
BLAKE2b-256 | 3b0add53bea221dd8da9694e6b209adfbf58355334bb6d749655a661c233c18e |
File details
Details for the file termcontrol-2021.5.2-py3-none-any.whl
.
File metadata
- Download URL: termcontrol-2021.5.2-py3-none-any.whl
- Upload date:
- Size: 31.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e543ac5556b9d7c327c7e80714e81aa67d8d6bac0c17434dea10a28de0ea7ec |
|
MD5 | 38aa4fd85df6a1dbf409c20dd60f0571 |
|
BLAKE2b-256 | a30a648af1e6f0834923cf9783937ee355385f4dd9bd3dd031f1e666e87b3e5c |