No project description provided
Project description
terminable
Python library for cross-platform terminal input.
- Tested on Windows, macOS, and Linux
- Does not require root access
- Provides keyboard, mouse, and terminal-resize events
- Simple API that you can plug into your own event loop
- Does not require a full-screen terminal app or separate window
Install
python3 -m pip install terminable
Usage
import terminable
with terminable.capture_input() as terminal_input:
while True:
returned_value = terminal_input.read()
print(f"Input received: {returned_value}\r")
Sample output:
Input received: KeyEvent(Char(a), KeyModifiers.NONE)
Input received: KeyEvent(Char(s), KeyModifiers.NONE)
Input received: KeyEvent(Char(A), KeyModifiers.SHIFT)
Input received: KeyEvent(Char(S), KeyModifiers.SHIFT)
Input received: KeyEvent(Char(F), KeyModifiers.SHIFT)
Input received: KeyEvent(Key.F1, KeyModifiers.NONE)
Input received: KeyEvent(Key.F3, KeyModifiers.NONE)
Input received: KeyEvent(Key.F2, KeyModifiers.NONE)
Input received: KeyEvent(Char(l), KeyModifiers.CONTROL)
Input received: KeyEvent(Char(k), KeyModifiers.CONTROL)
Input received: KeyEvent(Char(p), KeyModifiers.CONTROL)
Input received: MouseEvent(MouseEventKind.MOVED, None, 54, 20, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.MOVED, None, 53, 20, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.MOVED, None, 52, 20, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.MOVED, None, 51, 20, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.MOVED, None, 54, 19, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.SCROLL_DOWN, None, 54, 19, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.SCROLL_DOWN, None, 54, 19, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.SCROLL_DOWN, None, 54, 19, KeyModifiers.NONE)
Input received: MouseEvent(MouseEventKind.SCROLL_DOWN, None, 54, 19, KeyModifiers.NONE)
Input received: ResizeEvent(104, 31)
Input received: ResizeEvent(100, 31)
Input received: ResizeEvent(98, 31)
Input received: ResizeEvent(95, 31)
Types of input
- Keyboard
- Characters
- Arrow keys
- Function keys
- Enter, Esc, Backspace, etc.
- Modifiers:
CONTROL,SHIFT,ALT, etc.
- Mouse
- Move
- Down
- Up
- Drag
- Scroll (scroll wheel, trackpad scroll gestures)
- Resize of terminal window
Implementation
terminable is a thin Python wrapper around the excellent crossterm Rust library.
crossterm does all the heavy lifting. terminable exposes a small subset of crossterm functionality to Python using PyO3.
API
capture_input
terminable has a single function:
def capture_input() -> InputCapture:
...
InputCapture
The InputCapture object is a context manager object (intended to be used with Python's with statement).
When the InputCapture object is created, the terminal is placed into raw mode, such that:
- Input is not automatically echoed
printcalls do not automatically add a carriage return (\r)- Control sequences are not automatically processed
When the InputCapture object is destroyed, the terminal exits raw mode.
read
InputCapture has a single function:
def read(self) -> KeyEvent | MouseEvent | ResizeEvent:
...
read blocks until input is received.
Ctrl+C
terminable raises a KeyboardInterrupt exception on Ctrl+C.
Comparison with other libraries
There are other existing libraries for getting terminal input in Python, but each of them have their limitations:
cursesis not supported on Windows and requires a the terminal app to be full screentermiosis not supported on WindowsUniCursesis a cross-platform implementation ofcurses, but it is no longer maintained and users have reported issues on Windowspygameprovides comprehensive input functionality, but it requires a separate graphical window and is not well-suited for terminal inputkeyboardrequires root access on some platformspynputrequires root access on some platformstextualhas good cross-platform input handling in a terminal app, but it is tightly coupled with thetextualapplication model; you have to lettextualrun your event loop, for example.blessedhas excellent cross-platform keyboard input functionality that is easy to use in your own event loop, butblesseddoes not support mouse input.
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
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 terminable-0.1.2.tar.gz.
File metadata
- Download URL: terminable-0.1.2.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a545c1e76dde8f66dde6bb66beb8e76a8ecd21ca7ff4f53c30cf654539c483da
|
|
| MD5 |
dc8acb65b56a5444fd1ace75d0ee5bce
|
|
| BLAKE2b-256 |
f6cfb08bb1ce10f4c1f2662416f2ac65bba3d7b957c871497900b59d74476246
|
File details
Details for the file terminable-0.1.2-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: terminable-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 250.7 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d747a5255622d88ff1a478d6993b8a5e765b5917c810924ab9bd2448be5d12dd
|
|
| MD5 |
95b8f6c588d51193d968d71172b1f0b7
|
|
| BLAKE2b-256 |
642513313500494ebbf8383e106bc3845ab7c7303f1064350a681f26ace38313
|