PTY-based terminal emulator with web backend for running TUI applications via HTTP/WebSocket APIs
Project description
Terminal Wrapper
A full-featured terminal emulator with web backend that can run any TUI (Text User Interface) application and control it via HTTP/WebSocket APIs.
Features
- PTY-based Terminal Emulation: Full pseudo-terminal support for running terminal applications
- FastAPI Backend: RESTful API and WebSocket endpoints for terminal control
- Session Management: Create, manage, and control multiple terminal sessions
- Python CLI Client: Interactive CLI for connecting to remote terminal sessions
- Comprehensive Tests: Unit, integration, and end-to-end tests
What's Supported
โ Fully Working
- Simple commands:
ls,cat,echo, shell scripts - Interactive programs: Python REPL, bash, interactive shells
- Complex TUI apps:
vim,less,nano(tested with web frontend) - AI CLI tools:
claudeCLI (tested with both print and interactive modes) - Full-screen apps: Full support via xterm.js web frontend
- ANSI colors & formatting: Complete support for escape sequences
- Terminal resize: Dynamic window resizing with SIGWINCH
- Multiple sessions: Concurrent terminal sessions with session management
๐ง Backend Only (Raw Output)
When using the REST API without a terminal emulator frontend:
- Complex TUI apps output raw ANSI escape sequences
- Pair with a terminal emulator like xterm.js for rendering
Included: Our web frontend (frontend/) provides full xterm.js integration with mobile support.
Architecture
โโโโโโโโโโโโโโโโโโโ
โ CLI Client โ โ Python client for interactive control
โโโโโโโโโโฌโโโโโโโโโ
โ HTTP/WebSocket
โ
โโโโโโโโโโโโโโโโโโโ
โ FastAPI Server โ โ Web backend with REST + WebSocket
โโโโโโโโโโฌโโโโโโโโโ
โ
โ
โโโโโโโโโโโโโโโโโโโ
โ Session Manager โ โ Manages multiple terminal sessions
โโโโโโโโโโฌโโโโโโโโโ
โ
โ
โโโโโโโโโโโโโโโโโโโ
โ PTY Terminal โ โ Pseudo-terminal for running TUI apps
โโโโโโโโโโฌโโโโโโโโโ
โ
โ
โโโโโโโโโโโโโโโโโโโ
โ TUI App โ โ Any terminal application (vim, htop, etc.)
โโโโโโโโโโโโโโโโโโโ
Quick Start
1. Install Dependencies
uv sync
2. Start the Server
uv run python main.py
Server will start on http://localhost:8000
3. Run a TUI App
Option A: Using the CLI Client
uv run python -m terminal_wrapper.cli python3 examples/simple_tui.py
Option B: Using HTTP API
# Create a session
curl -X POST http://localhost:8000/sessions \
-H "Content-Type: application/json" \
-d '{"command": ["python3", "examples/simple_tui.py"], "rows": 24, "cols": 80}'
# Returns: {"session_id": "xxx-xxx-xxx"}
# Get output
curl http://localhost:8000/sessions/{session_id}/output
# Send input
curl -X POST http://localhost:8000/sessions/{session_id}/input \
-H "Content-Type: application/json" \
-d '{"data": "+"}'
# Delete session
curl -X DELETE http://localhost:8000/sessions/{session_id}
API Reference
REST Endpoints
POST /sessions- Create a new terminal sessionGET /sessions- List all active sessionsGET /sessions/{id}- Get session informationDELETE /sessions/{id}- Delete a sessionPOST /sessions/{id}/input- Send input to terminalPOST /sessions/{id}/resize- Resize terminal windowGET /sessions/{id}/output- Get terminal output
WebSocket Endpoint
WS /sessions/{id}/ws- Real-time bidirectional terminal I/O
Testing
Run all tests:
uv run pytest tests/ -v
Run specific test suites:
# Unit tests
uv run pytest tests/test_terminal.py tests/test_api.py -v
# End-to-end tests
uv run pytest tests/test_e2e.py -v
# Integration tests with TUI apps
uv run pytest tests/test_ink_integration.py -v
# Vim tests
uv run pytest tests/test_vim.py -v
Application Reports
We've tested various TUI applications with detailed reports:
| Application | Status | Report | Tests |
|---|---|---|---|
| vim | โ Fully Functional | reports/vim_report.md | tests/test_vim.py |
| htop | โ Fully Functional | reports/htop_report.md | tests/test_htop.py |
| Claude CLI | โ Fully Functional | reports/claude_report.md | tests/test_claude.py |
Each report includes:
- Test methodology and results
- Usage examples (HTTP & WebSocket)
- Technical details (escape sequences, commands)
- Performance metrics
- Best practices
Want to add a new application? See reports/TESTING_GUIDE.md for a comprehensive step-by-step guide on testing and documenting new TUI applications.
Example TUI Apps
Python TUI App (examples/simple_tui.py)
A simple counter application demonstrating:
- Terminal control codes
- Raw mode input handling
- Interactive key bindings
Controls:
+: Increment counter-: Decrement counterr: Reset counterq: Quit
Project Structure
terminal_wrapper/
โโโ terminal_wrapper/ # Main package
โ โโโ terminal.py # PTY-based terminal emulator
โ โโโ session_manager.py # Session management
โ โโโ api.py # FastAPI backend
โ โโโ cli.py # CLI client
โโโ tests/ # Test suite
โ โโโ test_terminal.py # Terminal emulator tests
โ โโโ test_api.py # API tests
โ โโโ test_e2e.py # End-to-end tests
โ โโโ test_ink_integration.py # TUI app tests
โโโ examples/ # Example applications
โ โโโ simple_example.py # Simple HTTP API usage
โ โโโ vim_example.py # Vim automation example
โ โโโ simple_tui.py # Python TUI demo
โโโ frontend/ # Web frontend with xterm.js
โโโ docs/ # Documentation
โโโ scripts/ # Utility scripts
โโโ main.py # Server entry point
โโโ pyproject.toml # Project configuration
Development
Built with:
- uv - Fast Python package manager
- FastAPI - Modern web framework
- uvicorn - ASGI server
- pytest - Testing framework
- websockets - WebSocket support
License
ISC
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 term_wrapper-0.1.0.tar.gz.
File metadata
- Download URL: term_wrapper-0.1.0.tar.gz
- Upload date:
- Size: 63.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71e68589bcb48a4bdf41c56c75d16c3fc3abce166fe7732a3ac9c1f1d71f1763
|
|
| MD5 |
5b1d07d671dc700665030267156b476b
|
|
| BLAKE2b-256 |
9964dda6984a9b4424bde080a1236d689c9ce9b23df50986d4b6d2e41d2844fb
|
File details
Details for the file term_wrapper-0.1.0-py3-none-any.whl.
File metadata
- Download URL: term_wrapper-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9516c809bb3b878acb15f5df29aceaaa8cdc2e76492297ebaeb9055316e639a
|
|
| MD5 |
08cc6d7790e3463a71a8091b5791ab59
|
|
| BLAKE2b-256 |
9f9d96c346c451fd0a4f53c7d204c7f5d04ae44ef50e416370a776457c98b13a
|