No project description provided
Project description
libTerm
Quick Setup with examples:
git clone https://github.com/hoefkensj/libTerm
cd libTerm
python -m venv .venv && source .venv/bin/activate
pip install -e .
python Examples/ex_manualsnake.py
Quick Setup for use within Your project:
pip install libTerm
⚠️ WARNING:
While No code in this project was written by an LLM (not even autocomplete). Everything below this warning was generated generated by an LLM. It was Quickly skimmed for errors and or falsehoods, corrected where needed, but I would not trust it to much.
libTerm is a lightweight Python library for low-level terminal control and interactive TUI-style programs.
It provides a clean abstraction over ANSI escape codes, POSIX terminal modes, cursor movement, input handling, colors, and terminal size — without hiding the power of the terminal.
This library is best suited for:
- Interactive CLI tools
- Terminal games (snake, roguelikes, demos)
- Live terminal dashboards
- Custom key handling (arrows, raw input)
It works primarily on POSIX systems (Linux/macOS). An implementation for windows is in progress but not usable at the moment (running it in a 3rd party terminal emulator in windows could work).
Features
- 🖥 Terminal mode control (NORMAL / CONTROL)
- 🎯 Cursor positioning, movement, save/restore
- ⌨ Raw keyboard input & event polling
- 🎨 Terminal foreground/background color detection
- 📐 Terminal size tracking
- 🧱 Minimal abstraction — ANSI is still accessible
Quick Start
from libTerm import Term,Coord,Mode
term = Term()
print(term.size.xy) # Terminal size (columns, rows)
print(term.cursor.xy) # Cursor position
print(term.color.bg) # Background color
Terminal Modes
libTerm exposes terminal modes through Mode.
Modes
Mode.NORMAL– canonical input, echo on, cursor visibleMode.CONTROL– raw-ish input, no echo, cursor hidden
Switching Modes
from libTerm import Term, Mode
term = Term()
term.mode(Mode.CONTROL)
print("Press q to quit")
while True:
if term.stdin.event:
key = term.stdin.read()
if key == 'q':
break
term.mode(Mode.NORMAL)
⚠️ IMPORTANT: The terminal is automatically restored on exit using atexit, but you should still return to NORMAL mode explicitly when possible.
Keyboard Input (term.stdin)
Properties & Methods
| Member | Description |
|---|---|
event |
Non-blocking check for input |
read() |
Read buffered input as a string |
buffer() |
Fill internal buffer |
flush() |
Clear buffer |
count |
Number of buffered reads |
Example: Arrow Keys
from libTerm import Term, Mode
term = Term()
term.mode(Mode.CONTROL)
while True:
if term.stdin.event:
key = term.stdin.read()
if key == '\x1b[A':
print("UP")
elif key == '\x1b[B':
print("DOWN")
elif key == '\x1b[C':
print("RIGHT")
elif key == '\x1b[D':
print("LEFT")
elif key == 'q':
break
Cursor Control (term.cursor)
Cursor Properties
| Property | Description |
|---|---|
xy |
Get/set cursor position (Coord) |
x, y |
Individual coordinates |
visible |
Cursor visibility state |
hidden |
Cursor hidden state |
Cursor Movement
from libTerm import Term, Coord
term = Term()
term.cursor.xy = Coord(10, 5)
print("#")
term.cursor.move.down()
print("#")
term.cursor.move.right(5)
print("#")
term.cursor.move.abs(X=2, Y=12)
print("#")
Show / Hide Cursor
term.cursor.hide()
term.cursor.show()
Cursor Save / Restore (Store)
The cursor maintains a position stack.
Methods
| Method | Description |
|---|---|
save() |
Save current position |
undo() |
Restore previous position |
load(n) |
Load specific stored position |
Example
term.cursor.save()
print("Hello")
term.cursor.undo()
print("World")
Coordinates (Coord)
Coord is an immutable (x, y) structure.
from libTerm import Coord
c = Coord(5, 10)
print(c.x, c.y)
print(c.xy)
print(c + Coord(2, 3))
It supports:
- Indexing (
c[0],c['x']) - Addition
- Iteration
Terminal Size (term.size)
Properties
| Property | Description |
|---|---|
xy |
(columns, rows) |
width |
Columns |
height |
Rows |
changed |
Size changed recently |
changing |
Resize in progress |
term = Term()
print(term.size.xy)
if term.size.changed:
print("Terminal resized!")
Colors (term.color)
Reads current terminal colors.
print(term.color.fg)
print(term.color.bg)
print(term.color.bg.RGB)
Color supports:
R,G,BBITdepth.RGBtuple
ANSI Access (Advanced)
libTerm does not hide ANSI escape sequences. You can freely mix raw ANSI with the API.
print("\x1b[31mRED\x1b[m")
Cursor movement enums are exposed:
term.cursor.move.up(2)
term.cursor.move.left(5)
Example Use Cases
1. Interactive Input
- Custom keybinds
- Vim-like controls
- Raw arrow-key navigation
2. Terminal Games
- Snake (see
Examples/) - Roguelikes
- ASCII animations
3. Live Dashboards
- Terminal resizing detection
- Cursor-based rendering
- Zero external dependencies
4. Learning Tool
- Study ANSI sequences
- Understand terminal modes
- Experiment with POSIX I/O
Examples Included
Located in Examples/:
| File | Description |
|---|---|
example.py |
Basic usage demo |
ex3.py |
Raw arrow-key handling |
ex_autosnake.py |
Automated snake animation |
ex_manualsnake.py |
Keyboard-controlled snake |
Run them directly:
python Examples/example.py
Platform Notes
- ✅ Linux / macOS: Fully supported
- ⚠ Windows: Partial support (limited cursor/input features)
Philosophy
libTerm is intentionally low-level.
It aims to:
- Stay close to ANSI & POSIX
- Avoid heavy abstractions
- Let you see and control the terminal
If you want a framework — use something else. If you want control — use libTerm.
License
MIT License (or specify if different)
Contributing
Pull requests welcome. Bug reports and experimental ideas encouraged.
Happy hacking 🐍🖥
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 libterm-0.2.6.tar.gz.
File metadata
- Download URL: libterm-0.2.6.tar.gz
- Upload date:
- Size: 2.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5eb1991c90d30b0277f86bce4f1879671e813965579fb729ddecdd45fd4c265
|
|
| MD5 |
b787ccd98481dfa62fd66094bb9d56a8
|
|
| BLAKE2b-256 |
36ee746840a599a71af2333e0a60e4e4b8b68644c492230ea21d504b68278299
|
File details
Details for the file libterm-0.2.6-py3-none-any.whl.
File metadata
- Download URL: libterm-0.2.6-py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bdd193684beac62db323de550e830b4308b283036c34a28af5121b6a332839c4
|
|
| MD5 |
3a1d5f85b0cb116257290aed9ff23648
|
|
| BLAKE2b-256 |
c54ce16c87194bc1a046713cfb081790f9aa96de4e503beba50f0e608f31a84e
|