terminal-concepts
Comprehensive guide for building CLI and TUI applications - terminal internals, design principles, and battle-tested patterns When building CLI/TUI apps, implementing argument parsing, handling terminal input/output, escape codes, buffering, signals, or asking about terminal development concepts
What this skill does
# Terminal Concepts for Developers
## Overview
This agent provides comprehensive guidance for **building and developing** terminal applications (CLI tools and TUIs). Learn how terminals work, design principles from proven programs, and practical patterns for creating robust, user-friendly command-line applications.
**Philosophy**: Focus on timeless concepts and learn from battle-tested programs like git, vim, tmux, less, ripgrep, and fzf.
**Target Audience**: Developers at all levels building terminal applications in any language.
## What Developers Need to Know
Building terminal applications requires understanding several interconnected concepts:
1. **Terminal Fundamentals**: How TTYs, PTYs, streams, and buffering work
2. **CLI Design**: User-facing interface principles (arguments, output, errors)
3. **TUI Architecture**: Interactive full-screen applications
4. **Common Pitfalls**: Issues developers face and how to avoid them
## Learning from Proven Programs
The best way to understand terminal application design is to study programs that have stood the test of time:
- **git**: Consistent subcommand interface, porcelain vs plumbing separation
- **vim**: Modal editing, robust state management
- **tmux**: Client-server architecture, session management
- **less**: Pager pattern, search and navigation
- **ripgrep**: Sensible defaults, performance-focused design
- **fzf**: Interactive filtering, minimal interface
- **cargo**: Clear progress indicators, helpful error messages
Throughout this guide, we'll reference these programs to illustrate concepts.
---
# Part 1: Terminal Fundamentals for Developers
## Understanding the Terminal Stack
When you build a terminal application, you're working within a layered system:
```
┌─────────────────────────────────┐
│ Terminal Emulator │ ← User sees (renders text, sends input)
│ (iTerm2, Alacritty, etc.) │
└─────────────────────────────────┘
↕ (PTY)
┌─────────────────────────────────┐
│ Shell (bash, zsh, fish) │ ← Interprets commands
└─────────────────────────────────┘
↕ (fork/exec)
┌─────────────────────────────────┐
│ Your Program │ ← Your CLI/TUI application
└─────────────────────────────────┘
```
### TTYs and PTYs
**TTY** (Teletypewriter): Originally physical devices, now refers to the terminal driver in the operating system.
**PTY** (Pseudo-Terminal): A pair of virtual devices that emulate a TTY:
- **Master side**: Terminal emulator reads/writes here
- **Slave side**: Your program sees this as stdin/stdout/stderr
**Why This Matters for Developers**:
- Your program receives input filtered through the TTY driver
- Some control characters are intercepted by the OS (Ctrl-C, Ctrl-Z)
- Buffering behavior differs between TTY and pipes
- You need to detect if you're running in a TTY vs pipe
### Terminal Modes: Cooked vs Raw
**Cooked Mode** (Canonical Mode):
- Default mode for most programs
- OS provides line editing (backspace, Ctrl-U, Ctrl-W)
- Input delivered to your program line-by-line after Enter
- Control characters handled by OS (Ctrl-C sends SIGINT)
**Raw Mode**:
- Character-by-character input (no line buffering)
- Your program receives every keypress including Ctrl-C
- Required for TUIs (vim, less, htop)
- Your program must implement all input handling
**When to Use Each**:
- **Cooked mode**: Normal CLI tools (git, cargo, npm)
- **Raw mode**: Interactive TUIs, line editors
### Standard Streams and File Descriptors
Every process has three standard streams:
| Stream | FD | Purpose | Examples |
| ------ | --- | ------------------------- | ------------------------------- |
| stdin | 0 | Input from user or pipe | Reading commands, file content |
| stdout | 1 | Primary output | Results, listings, JSON |
| stderr | 2 | Errors, logs, diagnostics | Error messages, warnings, debug |
**Critical Design Principle**: Separate stdout and stderr properly.
**Why This Matters**:
```bash
# User wants to pipe your output
your-tool | jq . # Only works if output goes to stdout
# User wants to capture errors
your-tool 2> errors.log # Only works if errors go to stderr
```
**Examples from Proven Programs**:
- **git**: Errors to stderr, porcelain output to stdout
- **ripgrep**: Matches to stdout, warnings to stderr
- **cargo**: Build status to stderr, `--message-format=json` to stdout
### Detecting TTY vs Pipe
Your program must detect its output destination to format appropriately:
**Concept** (language-agnostic):
```
if isatty(stdout):
# Human is watching - use colors, progress bars
enable_colors()
show_progress()
else:
# Piped to another program - plain output
disable_colors()
no_progress()
```
**Real-world examples**:
- `ls --color=auto`: Colors only for TTY
- `git status`: Full output for TTY, shorter for pipes
- `ripgrep`: Colors and summaries for TTY, plain matches for pipes
**C Implementation**:
```c
#include <unistd.h>
if (isatty(STDOUT_FILENO)) {
// stdout is a TTY
}
```
**Rust Implementation**:
```rust
use std::io::IsTerminal;
if std::io::stdout().is_terminal() {
// stdout is a TTY
}
```
**Python Implementation**:
```python
import sys
if sys.stdout.isatty():
# stdout is a TTY
```
**Go Implementation**:
```go
import "golang.org/x/term"
if term.IsTerminal(int(os.Stdout.Fd())) {
// stdout is a TTY
}
```
---
## ASCII Control Characters
### The 33 Control Characters
Control characters are created by holding Ctrl and pressing a key. There are 33 total:
- Ctrl-A through Ctrl-Z (26 characters)
- Plus 7 more: Ctrl-@, Ctrl-[, Ctrl-\, Ctrl-], Ctrl-^, Ctrl-\_, Ctrl-?
### Three Categories
**1. OS-Handled (Terminal Driver Intercepts)**:
| Key | ASCII | Name | Function |
| ------- | ----- | ---- | ----------------------------- |
| Ctrl-C | 3 | ETX | Sends SIGINT (interrupt) |
| Ctrl-D | 4 | EOT | EOF when line is empty |
| Ctrl-Z | 26 | SUB | Sends SIGTSTP (suspend) |
| Ctrl-S | 19 | XOFF | Freezes output (flow control) |
| Ctrl-Q | 17 | XON | Resumes output |
| Ctrl-\ | 28 | FS | Sends SIGQUIT |
**2. Keyboard Literals**:
| Key | ASCII | Name | Usage |
| --------- | ----- | ---- | ------------------------- |
| Enter | 13 | CR | Line terminator |
| Tab | 9 | HT | Tab character |
| Backspace | 127 | DEL | Delete previous character |
| Ctrl-H | 8 | BS | Often same as backspace |
**3. Application-Specific** (Your Program Can Define):
| Key | Common Usage |
| -------- | ------------------------------------ |
| Ctrl-A | Move to line start (readline, emacs) |
| Ctrl-E | Move to line end |
| Ctrl-W | Delete word backwards |
| Ctrl-U | Delete line |
| Ctrl-K | Kill to end of line |
| Ctrl-R | Reverse search (shells) |
| Ctrl-L | Clear screen |
| Ctrl-P/N | Previous/Next (history navigation) |
### What Developers Can and Cannot Intercept
**In Cooked Mode**:
- OS handles: Ctrl-C, Ctrl-D, Ctrl-Z, Ctrl-S, Ctrl-Q
- You see: Ctrl-A, Ctrl-E, Ctrl-W (already processed by line editing)
**In Raw Mode** (TUIs):
- You can intercept almost everything including Ctrl-C
- **Exception**: Ctrl-Z often still suspends (OS-level)
- You must handle all line editing yourself
**Best Practice**: Respect user expectations. Don't redefine Ctrl-C unless you have a very good reason (and document it clearly).
### Limited Modifier Combinations
Unlike GUI applications, terminals have severe limitations:
- **Only 33 Ctrl combinations** total
- Ctrl-Shift-X **doesn't exist** as a distinct character
- Ctrl-[number] combinations limited
- Alt/Meta combinations inconsistent acRelated in Design
contribute
IncludedLocal-only OSS contribution command center. Auto-refreshes the user's in-flight PR and issue state on invoke so conversations start with full context — no need to brief Claude on what's in flight. Helps the user find issues to contribute to on GitHub, builds per-repo dossiers of what each upstream expects (CLA, DCO, branch convention, AI policy, draft-first, review bots, issue templates), runs deterministic gates before any external action so AI-assisted contributions don't reach maintainers as slop. State is markdown-only: candidate files at ~/.contribute-system/candidates/, repo dossiers at ~/.contribute-system/research/, append-only event log at ~/.contribute-system/log.jsonl. No database, no cloud calls. Use when the user asks about their PRs / issues / contributions, wants to find new work to take on, claim an issue, build/refresh a repo's dossier, or draft a Design Issue or PR. Trigger with "/contribute", "what's my PR status", "find a contribution", "claim issue X", "draft a Design Issue for Y", "refresh dossier for Z".
architectural-analysis
IncludedUser-triggered deep architectural analysis of a codebase or scoped subtree across eight modes — information architecture, data flow, integration points, UI surfaces, interaction patterns, data model, control flow, and failure modes. This skill should be used when the user asks to "diagram this codebase," "map the architecture," "show the data flow," "give me an ERD," "trace control flow," "find the integration points," "verify the layout pattern," "audit the UX architecture," or any similar request whose primary deliverable is mermaid diagrams plus cited reports under docs/architecture/. Dispatches haiku/sonnet sub-agents in parallel for per-mode exploration, then verifies every citation mechanically before any node lands in a diagram. Not for one-off prose explanations of code (use code-explanation) or for high-level system design from scratch (use system-design).
mcp
IncludedModel Context Protocol (MCP) server development and tool management. Languages: Python, TypeScript. Capabilities: build MCP servers, integrate external APIs, discover/execute MCP tools, manage multi-server configs, design agent-centric tools. Actions: create, build, integrate, discover, execute, configure MCP servers/tools. Keywords: MCP, Model Context Protocol, MCP server, MCP tool, stdio transport, SSE transport, tool discovery, resource provider, prompt template, external API integration, Gemini CLI MCP, Claude MCP, agent tools, tool execution, server config. Use when: building MCP servers, integrating external APIs as MCP tools, discovering available MCP tools, executing MCP capabilities, configuring multi-server setups, designing tools for AI agents.
react-native-skia
IncludedDesign, build, debug, and optimise high-polish animated graphics in React Native or Expo using @shopify/react-native-skia, Reanimated, and Gesture Handler. Use when the user wants canvas-driven UI, shaders, paths, rich text, image filters, sprite fields, Skottie, video frames, snapshots, web CanvasKit setup, or performance tuning for custom motion-heavy elements such as loaders, hero art, cards, charts, progress indicators, particle systems, or gesture-driven surfaces. Also use when the user asks for fluid, glow, glass, blob, parallax, 60fps/120fps, or GPU-friendly animated effects in React Native, even if they do not explicitly say "Skia". Do not use for ordinary form/layout work with standard views.
plaid
IncludedProduct Led AI Development — guides founders from idea to launched product. Six capabilities: Idea (discover a product idea), Validate (pressure-test the idea against fatal flaws, problem reality, competition, and 2-week MVP feasibility), Plan (vision intake + document generation), Design (translate image references into a design.md spec), Launch (go-to-market strategy), and Build (roadmap execution). Use when someone says "PLAID", "plaid idea", "help me find an idea", "product idea", "idea from my business", "idea from my expertise", "plaid validate", "validate my idea", "pressure-test", "is this idea good", "find fatal flaws", "validate the problem", "plan a product", "define my vision", "generate a PRD", "product strategy", "plaid design", "design from image", "translate image to design", "create design.md", "extract design tokens", "plaid launch", "go-to-market", "launch plan", "GTM strategy", "launch playbook", "plaid build", "build the app", "start building", or "execute the roadmap".
nextjs-framer-motion-animations
IncludedAdds production-safe Motion for React or Framer Motion animations to Next.js apps, including reveal, hover and tap micro-interactions, whileInView, stagger, AnimatePresence, layout and layoutId transitions, reorder, scroll-linked UI, and lightweight route-content transitions. Use when the user asks to add, refactor, or debug Motion or Framer Motion in App Router or Pages Router codebases, especially around server/client boundaries, reduced motion, LazyMotion, bundle size, hydration, or route transitions. Avoid for GSAP-style timelines, WebGL or 3D scenes, heavy scroll storytelling, or CSS-only effects unless Motion is explicitly requested.