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

## createCells

`createCells(columns, rows)` returns a `Cells` value: a screen of that size with a character, a style, and a width for each cell. A wide character's second cell has width 0. Characters of more than one code point, such as a family emoji, are kept in `clusters`, and the cell holds their index.

| Field            | Description                                              |
| ---------------- | -------------------------------------------------------- |
| `columns`        | Columns in the screen.                                   |
| `rows`           | Rows in the screen.                                      |
| `chars`          | The code point of each cell.                             |
| `styles`         | The style number of each cell.                           |
| `widths`         | The width of each cell. A continuation cell has width 0. |
| `clusters`       | The multi-code-point clusters.                           |
| `ambiguousWidth` | The width to give ambiguous characters. 1 by default.    |

`Rect` is `{ x: number; y: number; columns: number; rows: number }`.

## Write cells

- `write(cells, x, y, text, style?, right?)` writes one line, clipped to `right` (exclusive) and the screen. It returns the column after the last cell written. A wide character that would cross the edge is left out and its cell filled with a space.
- `setCell(cells, x, y, codePoint, style?)` sets one cell to a code point.
- `setCluster(cells, x, y, cluster, style?)` sets one cell to a character of several code points.
- `fill(cells, rect, style?)` fills a rect with spaces in a style.
- `clear(cells)` clears every cell to a space in style 0.
- `rowText(cells, y)` returns row `y` as text.

```ts
import { clear, createCells, createStyles, rowText, setCell, write } from "tui"

const cells = createCells(12, 2)
const styles = createStyles()
clear(cells)
write(cells, 0, 0, "tui", styles.style({ bold: true }))
setCell(cells, 4, 0, 0x2588, styles.style({ fg: "#3291ff" }))
console.log(rowText(cells, 0))
```

## Styles

`createStyles()` returns a `Styles` value. Each style is kept once, and cells hold its number, so asking for a style that already exists allocates nothing.

- `styles.style(input)` returns the style number for a `StyleInput`.
- `styles.colors(fg, bg)` returns a style of two `0xrrggbb` colors, or -1 for the terminal's default, with no attributes.
- `styles.foregrounds`, `styles.backgrounds`, `styles.attributes`, and `styles.links` are the tables the renderer reads.

A `Color` is `"default"`, a palette index from 0 to 255, or `#rrggbb`. A `StyleInput` has `fg`, `bg`, `bold`, `dim`, `italic`, `underline`, `inverse`, `strikethrough`, and `link`.

## createRenderer

`createRenderer(styles, options?)` turns frames of cells into what the terminal needs. After the first frame it writes only the cells that changed, inside synchronized output so a frame never shows half drawn. The bytes it returns are valid until the next call.

| Option   | Type         | Description                                                             |
| -------- | ------------ | ----------------------------------------------------------------------- |
| `colors` | `ColorDepth` | `"truecolor"`, `"256"`, `"16"`, or `"none"`. Defaults to `"truecolor"`. |
| `inline` | `boolean`    | Draw in the rows below the cursor, instead of on the whole screen.      |

| Method                   | Description                                                                                          |
| ------------------------ | ---------------------------------------------------------------------------------------------------- |
| `render(cells, cursor?)` | The bytes that turn the terminal's screen into `cells`, with the cursor shown at `cursor` or hidden. |
| `invalidate()`           | Draws everything next time.                                                                          |
| `resize(columns)`        | Draws everything next time, and works out where an inline frame starts.                              |
| `leave()`                | The bytes that end an inline frame.                                                                  |

`colorDepthOf(env)` reads `NO_COLOR`, `COLORTERM`, and `TERM` and returns the color depth to use.
