Skip to main content

PTY-based terminal emulator with web backend for running TUI applications via HTTP/WebSocket APIs

Project description

Terminal Wrapper

CI

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: claude CLI (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 session
  • GET /sessions - List all active sessions
  • GET /sessions/{id} - Get session information
  • DELETE /sessions/{id} - Delete a session
  • POST /sessions/{id}/input - Send input to terminal
  • POST /sessions/{id}/resize - Resize terminal window
  • GET /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 counter
  • r : Reset counter
  • q : 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

term_wrapper-0.1.0.tar.gz (63.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

term_wrapper-0.1.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

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

Hashes for term_wrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 71e68589bcb48a4bdf41c56c75d16c3fc3abce166fe7732a3ac9c1f1d71f1763
MD5 5b1d07d671dc700665030267156b476b
BLAKE2b-256 9964dda6984a9b4424bde080a1236d689c9ce9b23df50986d4b6d2e41d2844fb

See more details on using hashes here.

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

Hashes for term_wrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a9516c809bb3b878acb15f5df29aceaaa8cdc2e76492297ebaeb9055316e639a
MD5 08cc6d7790e3463a71a8091b5791ab59
BLAKE2b-256 9f9d96c346c451fd0a4f53c7d204c7f5d04ae44ef50e416370a776457c98b13a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page