# How hotty works

A hotty program writes HTML to its terminal, and the terminal renders it
inside the character grid. When the user clicks an element, the terminal
writes an event to the program's input stream alongside normal keystrokes.
Because all messages travel over standard terminal I/O, hotty works
anywhere a terminal does: over SSH, inside containers, and through
multiplexers that either act as hosts or pass hotty sequences through.

```text
 program                                   host (the terminal)
   │  is there a host? (a=q)  ───────────▶  │
   │  ◀─────────────  yes, and what it can  │
   │  a document (a=doc)      ───────────▶  │  keeps the HTML
   │  a placement (a=place)   ───────────▶  │  lays it out on cells, draws it
   │  a change (a=delta)      ───────────▶  │  changes those elements
   │  ◀───────────────  an event (a=ev)     │  the user clicked, typed, dragged
```

This page walks through that loop with a single card. The
[protocol](https://hotty.neuroplast.io/docs/protocol) defines the full specification; each section below
cites its corresponding spec section.

## The envelope

Every message in either direction is a single OSC escape sequence (§3):

```text
ESC ] 7279 ; a=doc:s=card ; PGJ1dHRvbiBpZD1nbz5HbzwvYnV0dG9uPg== ST
```

- `ESC ]` starts an Operating System Command (the same mechanism used to set
  a window title), and `ST` terminates it. `7279` is the ASCII code for
  `H` and `O`.
- The **control** section (`a=doc:s=card`) holds `key=value` pairs. `a` is
  the action, and `s` is the surface name.
- The **payload** is base64-encoded: HTML for documents, and msgpack for
  structured messages.

Terminals without hotty support ignore unrecognized OSC sequences, which
makes probing for a host safe.

## Is there a host?

A program queries the terminal at startup (§4):

```text
program → ESC ] 7279 ; a=q:n=1:v=0.10 ST  CSI c
host    → ESC ] 7279 ; a=ok:n=1:re=q ; <the capabilities> ST  CSI ? 62 ; 22 c
```

`CSI c` requests Primary Device Attributes (DA1), which every terminal
answers. If the DA1 reply arrives without a hotty response ahead of it, the
terminal is not a host. The program then falls back to cell rendering after
a brief grace period rather than waiting for a long timeout.

A host's reply includes its **capabilities**: the negotiated protocol
version, cell dimensions in CSS pixels, light or dark color scheme, and
supported optional features.

## A document, then a place for it

A **surface** is a named HTML document. The program sends the document first,
then places it on the grid (§5):

```text
program → ESC ] 7279 ; a=doc:s=card ; <button id=go>Go</button> ST
program → ESC ] 7279 ; a=place:s=card:c=40:r=auto:n=2 ST
host    → ESC ] 7279 ; a=ok:n=2:s=card:re=place:c=40:r=2 ST
```

(Payloads are shown decoded here; on the wire they are base64-encoded.)

A placement reserves a cell rectangle starting at the cursor: here, 40
columns wide and as many rows as the content requires (`r=auto`). The host
reports the resulting row count in its reply. The surface is anchored to
that line, scrolls up with surrounding output, and remains in scrollback
after the program exits. Full-screen applications instead position surfaces
at explicit coordinates and reposition them as the layout changes.

The host renders the document with standard CSS, inheriting the terminal's
font and colors by default ([Styling](https://hotty.neuroplast.io/docs/concepts/styling)). Inside the rectangle is
HTML; outside it, the terminal behaves normally.

## Changes by id

To update a surface, the program sends a **delta**: a list of operations
targeting elements by `id` (§6):

```text
program → ESC ] 7279 ; a=delta:s=card:q=2 ; {"ops": [{"op": "text", "t": "go", "v": "Start"}]} ST
```

The button label updates to "Start". Wire cost scales with the size of the
change rather than the size of the document—updating a live counter takes a
few dozen bytes even in a large document. [Updates](https://hotty.neuroplast.io/docs/concepts/updates) covers all
delta operations, including morphing an element in place without losing user
state.

## Events come back as input

When the user clicks the button, the host writes an **event** to the
program's input stream alongside regular keystrokes (§9):

```text
host → ESC ] 7279 ; a=ev:s=card:e=click:t=go ; {"area": {"c": 0, "r": 0, "w": 4, "h": 1}} ST
```

`s` identifies the surface, `e` the event type, and `t` the target element's
`id`. The program handles the event as it sees fit.

Most routine interactions happen locally in the host without a round trip.
Typing in a text field, hovering, scrolling, and toggling checkboxes are
handled by the terminal; the program receives only the events it subscribed
to, such as `change` when a field commits or `submit` with the form values.
See [Input](https://hotty.neuroplast.io/docs/concepts/input) and [Keyboard and focus](https://hotty.neuroplast.io/docs/concepts/keyboard) for details on
event reporting.

## What hotty is not

- **Not a browser.** Surfaces do not run JavaScript. The program owns all
  application state and event logic; the host only renders HTML and reports
  events.
- **Not a network client.** All assets, including images, are sent through
  the program's output stream. A host fetches external resources only when
  explicitly permitted by the user and requested by the document
  ([Images and the network](https://hotty.neuroplast.io/docs/concepts/resources)).
- **Not all or nothing.** A CLI tool can emit a single card in normal shell
  output, or a full-screen TUI can render every panel as HTML. On terminals
  without a host, the program falls back to cell rendering
  ([Degrading](https://hotty.neuroplast.io/docs/concepts/degrading)).
