# PTY & ANSI
## Input / output pipeline
```
Keyboard (winit KeyEvent)
│
▼
input::keyboard::key_to_bytes()
│ ModifiersState tracked via WindowEvent::ModifiersChanged
▼
pty::Session::write(&[u8])
│ Blocking write with partial-write retry loop
▼
PTY master fd
│
▼
Shell (zsh) running in PTY slave
│ Processes input, produces output + escape sequences
▼
PTY master fd
│ Non-blocking poll + read in reader thread
▼
EventLoopProxy::send_event(UserEvent::PtyOutput(data))
│
▼ (main thread event loop)
ansi::Parser::advance(&[u8], &mut Grid)
│ vte::Parser state machine → vte::Perform callbacks
│ - print(c) → grid.put_char(c)
│ - execute(c) → \n, \r, \b, \t, \x0c
│ - csi/esc/osc → ignored (no printed garbage)
▼
Grid::to_string() → Renderer::draw()
│
▼
wgpu swap chain → Metal → Screen
```
## PTY
- Uses `nix::pty::forkpty` to create a pseudoterminal pair
- Child process exec's `$SHELL` (default: `zsh`)
- `Winsize` set on spawn; `TIOCSWINSZ` + `SIGWINCH` on resize
- **Reader thread**: polls master fd via `nix::poll` (100 ms timeout), sends data to main thread via `winit::EventLoopProxy`
- **Write loop**: handles partial writes (POSIX can return short count)
- **Write method** returns `usize` (bytes written) for instrumentation
## ANSI parser
Uses the `vte` crate (state machine used by Alacritty, Kitty) to correctly parse
VT escape sequences:
| `print(c)` | Printable characters | `grid.put_char(c)` |
| `execute(byte)` | C0 control codes | `\n`→newline, `\r`→CR, `\b`→BS, `\t`→tab, `\x0c`→clear |
| `hook/put/unhook` | DCS (Device Control Strings) | Ignored |
| `osc_dispatch` | OSC (Operating System Commands) | Ignored |
| `csi_dispatch` | CSI (Control Sequence Introducer) | Ignored — M3 |
| `esc_dispatch` | ESC sequences | Ignored — M2+ |
CSI sequences (`ESC[`) and OSC sequences (`ESC]`) are **absorbed without printing**,
so escape sequence garbage no longer appears in the terminal.
## Future work (Milestones 2–3)
- SGR colors and text attributes (bold, italic, underline)
- Cursor positioning (CUP, CUU, CUD, CUF, CUB)
- Erase in display/line (ED, EL)
- Alternate screen buffer
- Scrollback ring buffer
- Device Control Strings for clipboard, hyperlinks, etc.