---
title: "Rendering Model"
description: "Terminal apps, written in TypeScript and compiled to native executables with scriptc."
canonical_url: "https://tui-sdk.dev/docs/concepts/rendering-model"
---
# Rendering Model

A tui app draws its whole screen from its state. `runApp` calls the view for every frame that changed, routes a key to the focused part, and draws again before the next key. You do not keep a terminal cursor or patch a cell by hand.

The HTML page runs [`examples/rendering.ts`](https://github.com/vercel-labs/tui/blob/main/examples/rendering.ts) here, in a terminal in the page.

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

runApp({
  view: (frame) => {
    text(frame, 0, 0, "Hello")
  },
  keys: (key) => {
    if (matchesShortcut(key, "ctrl+c")) {
      process.exit(0)
    }
    return false
  },
})
```

`frame` describes the screen and the frame's time:

- `frame.cells` is the screen as typed arrays. `write`, `setCell`, `setCluster`, `fill`, and `clear` change it.
- `frame.rect` is the whole screen, `{ x: 0, y: 0, columns, rows }`.
- `frame.styles` keeps each style once. Pass a `StyleInput` to `text` or `write` and tui gets its number.
- `frame.theme` holds the styles components draw with.
- `frame.cursor` is where the terminal cursor shows, or `null` to hide it. A focused `Input` sets it.
- `frame.time` and `frame.delta` are milliseconds since the app started and since the frame before.
- `frame.fps` is the frames the terminal got in the last second. It is always 0 in a test app.
- `frame.held("w")` says whether a key is down in an app with `heldKeys`.
- `frame.copy(text)` puts text on the system clipboard through the terminal.
- `frame.animate()` asks for another frame a sixtieth of a second later.

## When frames are drawn

Without `fps`, the app draws only when something changes: a key, a resize, a call to `frame.animate()`, or a call to `app.redraw()`. `runApp` returns the app immediately, so a timer or other code that changes state calls `app.redraw()` to draw the change. Any number of changes in one turn of the event loop draw one frame. With `fps`, the app draws that many frames a second, and each frame carries its own `time` and `delta`.

Springs and transitions call `frame.animate()` while they move, so an app that only animates a panel needs no `fps`. A game that moves every frame usually sets `fps`.

## The renderer

`createRenderer` turns cells into terminal output. After the first frame it writes only the cells that differ from the frame before, inside synchronized output so the terminal never shows half a frame.

The lower-level path is `bindTerminal`, `createCells`, and `createRenderer`:

```ts
import {
  bindTerminal,
  clear,
  createCells,
  createRenderer,
  createStyles,
  drawBox,
  matchesShortcut,
  write,
} from "tui"

const styles = createStyles()
const accent = styles.style({ fg: "#3291ff", bold: true })
const renderer = createRenderer(styles)
let draft = ""

const terminal = bindTerminal({
  onKey: (key) => {
    if (matchesShortcut(key, "ctrl+c")) {
      terminal.restore()
      process.exit(0)
    }
    if (key.name === "char") {
      draft += key.text
    }
    render()
  },
  onResize: render,
})

function render() {
  const cells = createCells(terminal.columns, terminal.rows)
  clear(cells)
  const inside = drawBox(
    cells,
    { x: 0, y: 0, columns: cells.columns, rows: cells.rows },
    { title: "Hello" },
  )
  write(cells, inside.x + 1, inside.y, "❯ ", accent)
  write(cells, inside.x + 3, inside.y, draft)
  terminal.write(renderer.render(cells, { x: inside.x + 3 + draft.length, y: inside.y }))
}

render()
```

`bindTerminal` takes the alternate screen, enables bracketed paste, and restores the terminal on `restore()`, on SIGTERM, and on a crash. `onResize` receives the new size, and `onKey` receives every key and paste.

Use `runApp` unless you need to control the terminal loop yourself. It combines these pieces and adds focus, components, and the test app.
