---
title: "App and Frame"
description: "Terminal apps, written in TypeScript and compiled to native executables with scriptc."
canonical_url: "https://tui-sdk.dev/docs/reference/app"
---
# App and Frame

## runApp

`runApp(options)` starts an app in the terminal and returns immediately. The app runs until it calls `stop` or `exit`, or until Ctrl+C when nothing else takes the key. A timer or other code that changes the app's state calls `app.redraw()` to draw the change. Redraws are coalesced: any number of changes in one turn of the event loop draw one frame.

```ts
import { runApp, text } from "tui"

let count = 0

runApp({
  view: (frame) => {
    text(frame, 1, 1, `Count: ${count}`, frame.theme.heading)
    text(frame, 1, 2, "Press + or - to change it. Ctrl+C quits.", frame.theme.muted)
  },
  keys: (key) => {
    if (key.name !== "char") {
      return false
    }
    if (key.text === "+") {
      count++
      return true
    }
    if (key.text === "-") {
      count--
      return true
    }
    return false
  },
})
```

| Option     | Type                                  | Description                                                                             |
| ---------- | ------------------------------------- | --------------------------------------------------------------------------------------- |
| `view`     | `(frame: Frame) => void`              | Draws the whole screen. Called again for every frame that changed.                      |
| `keys`     | `(key: Key, frame: Frame) => boolean` | Keys no focused part took. Return `true` when handled.                                  |
| `output`   | `TerminalOutput`                      | Where frames and control sequences go. `process.stdout` by default.                     |
| `input`    | `TerminalInput`                       | Where keys come from. `process.stdin` by default.                                       |
| `colors`   | `ColorDepth`                          | Colors to draw in. Read from the environment by default.                                |
| `theme`    | `Theme`                               | The styles components draw with. `defaultTheme` when it is not given.                   |
| `rows`     | `number`                              | Draws inline in this many rows below the cursor, instead of on the alternate screen.    |
| `fps`      | `number`                              | Draws this many frames a second. Without it, the app draws only when something changes. |
| `heldKeys` | `boolean`                             | Tracks which keys are down for `frame.held`.                                            |
| `mouse`    | `boolean`                             | Takes the mouse on the alternate screen.                                                |
| `agent`    | `AppAgent`                            | Lets AI agents use the app. `agentServer` from `tui/agent` makes one.                   |

`runApp` returns an app object with `frame`, `stop()`, `exit(code?)`, `redraw()`, `settle()`, `press(shortcut)`, `type(text)`, and `screen()`.

`output` takes any writable stream, such as `process.stderr`. The app reads its size, and whether it is a terminal, from that stream: pass `process.stderr` to keep stdout free for data another program reads. `input` takes a stream with the same shape as `process.stdin`.

## createTestApp

`createTestApp(options)` runs the same app on a screen of its own, with nothing written to the terminal. It takes every `runApp` option plus `columns` (default 80), `rows` (default 24), and `graphics` (draw images as a terminal with the kitty graphics protocol would get them).

| Method                   | Description                                                              |
| ------------------------ | ------------------------------------------------------------------------ |
| `input(data)`            | Sends raw terminal input, such as `"\r"` or a bracketed paste.           |
| `press(shortcut)`        | Presses one key by its shortcut: `"down"`, `"enter"`, `"ctrl+k"`, `"x"`. |
| `type(text)`             | Types text one keystroke at a time.                                      |
| `screen()`               | The screen as lines of text, without trailing spaces.                    |
| `find(text)`             | The cell where some text starts, or `null`.                              |
| `cell(x, y)`             | One cell's text, colors, attributes, and link.                           |
| `click(x, y)`            | Clicks the left button on a cell, counted from 0 at the top left.        |
| `wheel(x, y, direction)` | Turns the wheel once over a cell, `"up"` or `"down"`.                    |
| `advance(ms)`            | Moves the app's clock and draws the frame a timer would.                 |
| `resize(columns, rows)`  | Changes the screen's size.                                               |
| `focused()`              | The id of the part with the focus.                                       |
| `copied()`               | What the app put on the clipboard, oldest first.                         |
| `graphics()`             | What images sent the terminal, and empties it.                           |
| `settle()`               | Draws now what changed.                                                  |

Each key is drawn before the next one is sent, as a terminal would show it.

## Frame

| Field     | Type                       | Description                                                              |
| --------- | -------------------------- | ------------------------------------------------------------------------ |
| `cells`   | `Cells`                    | The screen as typed arrays.                                              |
| `styles`  | `Styles`                   | The app's style table.                                                   |
| `theme`   | `Theme`                    | The app's theme.                                                         |
| `rect`    | `Rect`                     | The whole screen.                                                        |
| `cursor`  | `{ x, y } \| null`         | Where the cursor shows, or `null` to hide it. A focused `Input` sets it. |
| `time`    | `number`                   | Milliseconds from the app's start to this frame.                         |
| `delta`   | `number`                   | Milliseconds since the frame before.                                     |
| `fps`     | `number`                   | Frames the terminal got in the last second. Always 0 in a test app.      |
| `held`    | `(key: string) => boolean` | Whether a key is down, in an app with `heldKeys`.                        |
| `copy`    | `(text: string) => void`   | Puts text on the system clipboard through the terminal.                  |
| `animate` | `() => void`               | Asks for another frame a sixtieth of a second later.                     |
| `pointer` | `{ x, y }`                 | The cell the mouse was last at, or -1 for both until it moves.           |

## Theme

A `Theme` maps roles to styles: `primary`, `success`, `destructive`, `muted`, `highlight`, `selected`, `code`, `link`, `heading`, `border`, and `match`. `defaultTheme` uses the terminal's palette. `mergeStyles(under, over)` puts one style over another, each of `over`'s settings replacing `under`'s.

## Rect helpers

- `text(frame, x, y, value, style?, clip?)` writes one line and returns the column after it.
- `paint(frame, rect, style?)` fills a rect with spaces in a style.
- `split(rect, "rows" | "columns", sizes, gap?)` divides a rect. A negative size is a share of what is left.
- `inset(rect, padding)` shrinks a rect by a number of cells, or by `[vertical, horizontal]`.
- `center(rect, columns, rows)` returns a rect of that size in the middle of another.
