# The .tui language

URL: https://tuimark.dev/guide/language

> How a Tuimark document is structured, the closed set of tags, common attributes, text, screens and modals, and document versions.

A `.tui` file is a strict subset of XML that describes what is on the screen: containers, text, and widgets, with their data bindings and the names of the actions they fire. It holds no logic. How things look lives in [TCSS](/guide/tcss), the data in JSON, and what actions do in the host program.

## A document [#a-document]

```tui
<tui theme="auto" mouse="mouse_enabled">
  <style src="theme.tcss"/>
  <style>
    #title { bold: true; }
  </style>
  <keymap>
    <bind keys="q,ctrl+c" action="quit"/>
  </keymap>
  <screen id="main" focus="#query">
    <!-- containers, text and widgets -->
    <modal id="confirm" open="confirm_open">…</modal>
  </screen>
  <screen id="logs">…</screen>
</tui>
```

- **`<tui>`** is the root. It needs no `version` (see [versions](#versions)). `theme` is `dark` (the default), `light`, or `auto`. `mouse` turns mouse input on; see [the mouse](/guide/interaction#the-mouse).
- **`<style>`** loads a stylesheet with `src` (relative to the document), or holds one inline. Several are fine; they apply in order.
- **`<keymap>`** holds the `<bind>` rows: which keys fire which actions. See [Keymap, actions and focus](/guide/interaction).
- **`<screen>`** is a full terminal surface. A document can have several; exactly one is active, the first by default, and the host switches with `Set("@screen", "logs")`. `focus="#id"` picks the node focused when the screen appears.
- **`<modal>`** is a layer over its screen. Modals come last in their screen (`L004` otherwise).

## XML rules [#xml-rules]

Tuimark reads well-formed XML and nothing fancier:

- UTF-8, one root, lowercase tag and attribute names, values in double quotes.
- Comments are fine. No namespaces, DTDs, or processing instructions, and no entities beyond `&lt; &gt; &amp; &quot; &apos;`.
- A document that is not well-formed is `V005`; `tuimark fmt` refuses it and every other command reports it.

Control characters and ANSI escapes are removed from bound data before it is painted, so data can never repaint the terminal; in the document's own text they are an error (`V007`).

## Versions [#versions]

You don't write a version. A `<tui>` without `version` is the **current language**: everything in these guides and in the [reference](/reference/tags) is available to it. Tools that need a number for it use 4 (the `current` member of `tuimark catalog`, `"version": "0.4"` in `tuimark ir`), but there is no `version="4"`: like any value other than 1, 2, or 3 it is `V003`, and the document is read as `version="1"`.

A document that writes `version="1"`, `"2"`, or `"3"` is **pinned** to the meaning it had in tuimark 0.4.1, defaults included, and its dumps do not change. Two kinds of thing are only in the current language.

**New vocabulary.** A pinned document that uses it gets a diagnostic that ends in `(requires a <tui> without version)`, and the construct does nothing:

- the tags `checkbox` and `select`, the attribute `single` on `list` and `table`, and `indeterminate` on `progress` (see [widgets](/guide/widgets));
- `{path}` in a `<button>`'s label and body (see [bindings](/guide/bindings));
- the tags `include`, `fragment`, `template`, and `use` ([composition](#composing-a-document-from-files), below).

**Changed behavior.** A pinned document is not diagnosed here; it simply keeps the old behavior, and writes by hand what the current language does for you:

- the defaults: [layout](/guide/layout#the-current-languages-defaults) and the [built-in sheet](/guide/tcss#the-built-in-sheet) with its focus look;
- the [key and focus rules](/guide/interaction#a-modal-traps-the-keymap): `esc` closes a modal, a modal traps the keymap, and `tabs` is focusable;
- `ctrl+i`, `ctrl+j`, and `ctrl+m` in a keymap are `V003` in the current language, because a terminal delivers them as `tab` and `enter`. A pinned document still accepts them, and they never match there either.

The current language also reports `L008` and `L009`, two layout warnings about content a dump shows as gone:

- `L008`: a text cut without an ellipsis;
- `L009`: a node that cuts a child on an axis it does not scroll.

The dump marks the node with `clipped`. When a cut is intended, write `wrap: nowrap` or `overflow: hidden` and the warning goes away.

The pinned versions keep the vocabulary they had:

- **`version="1"`** is the original vocabulary. Its meaning, and its dumps, never change.
- **`version="2"`** accepts everything `version="1"` does, with the same meaning, plus the 0.2 additions: the tags `table`, `column`, `tabs`, `tab`, `sparkline`, and `hints`; `each` on containers; multi-select; `class:NAME`; `theme="auto"`; `@media (theme: …)`; `layout: grid`; scrollbars; `bar: eighths`; the built-in `move-*`, `check-*`, and `switch-to` actions; the mouse; and `when` matching over the focus chain.
- **`version="3"`** is `version="2"` plus `L008`/`L009` and the 0.3 additions, all additive: `row-gap` and `column-gap`; `wrap: truncate-start` and `truncate-middle`; `<column priority="N">`; `<sparkline scale="NAME">`; `{path}` in a `<tab>`'s `label` and `short`; `<scroll stick="bottom">`; `<modal focus="#id">`; and `<keymap when="SEL">`. Several `<keymap>` elements work in every version. See [layout](/guide/layout), [widgets](/guide/widgets), [keymap, actions and focus](/guide/interaction), and the [Go API](/guide/go-api) for each in its usual place, and the [tags](/reference/tags), [CSS properties](/reference/css), and [diagnostics](/reference/diagnostics) reference for the exact rules.

In a `version="1"` document each 0.2 addition is still recognized, and reported with a message that ends in `(requires version="2")`; in a `version="1"` or `version="2"` one, each 0.3 addition ends in `(requires version="3")`. Removing the attribute gives the document the whole language. A `.tcss` file has no version of its own: it is checked against the document that loads it.

## The tags [#the-tags]

The vocabulary is closed: an unknown tag is `V001`, not a new widget. The [tags reference](/reference/tags) lists every attribute each tag takes.

| Role | Tag | What it is |
|---|---|---|
| Document | `tui` | the root |
| | `style` | a stylesheet, from `src` or inline |
| | `keymap`, `bind` | global keys and one key row |
| | `screen` | a terminal surface; one is active |
| Layout | `col`, `row` | vertical and horizontal flex containers |
| | `box` | a generic container, a column by default |
| | `scroll` | a clipping viewport that scrolls its content (`axis="y\|x\|both"`) |
| | `spacer` | an empty item that takes the free space |
| | `tabs`, `tab` | a strip of tab labels over the active tab (version 2) |
| Content | `text` | text, with `{path}` interpolation |
| | `rule` | a one-cell separator line (`axis="x\|y"`) |
| | `sparkline` | a bar chart of a numeric array (version 2) |
| | `hints` | key hints generated from the keymap (version 2) |
| Interactive | `list`, `item` | a focusable list with a cursor; `item` is its row |
| | `input` | a single-line text field |
| | `checkbox` | a toggle with a label (current language) |
| | `select` | one choice from a dropdown list (current language) |
| | `button` | a focusable action |
| | `progress` | a 0–100 bar, or a spinner with `indeterminate` |
| | `table`, `column` | rows of an array in columns, with a cursor (version 2) |
| Overlay | `modal` | a layer above the screen that traps focus |
| Composition | `include`, `fragment` | a file placed into the document, and the root of that file (current language) |
| | `template`, `use` | a reusable piece of markup and the places it is stamped (current language; see below) |

[Widgets](/guide/widgets) shows each interactive tag with a rendered example.

## Common attributes [#common-attributes]

Most tags share these:

| Attribute | Meaning |
|---|---|
| `id` | a unique name (`V004` on a duplicate). Required on focusable widgets: `list`, `input`, `button`, `modal`, and in version 2 `table`, `tabs`, `tab`, and in the current language `checkbox` and `select` (`V012`) |
| `class` | space-separated class names for [TCSS](/guide/tcss) selectors |
| `title` | text painted on the node's top border, when it has one; may interpolate `{path}` |
| `if` | `path` or `!path`: the node exists only while it is truthy |
| `hidden`, `disabled` | `true`, `false`, `path`, or `!path` |
| `focusable` | `true` lets any node take focus |
| `on:click`, `on:focus`, … | the name of an action to fire; see [actions](/guide/interaction#actions-are-names) |
| `class:NAME` | adds class `NAME` while a path is truthy (version 2); see [bindings](/guide/bindings#conditional-classes) |
| `style` | inline TCSS declarations; an escape hatch, prefer classes |
| `width`, `height`, `gap`, `pad`, `border` | presentational shortcuts for the same CSS properties; any stylesheet rule beats them |

An attribute a tag does not take is `V002`.

## Text [#text]

`<text>` holds a template: literal text with any number of `{path}` interpolations.

```tui
<text>{done} of {total} tasks</text>
<text wrap="wrap">{ticket.body}</text>
```

- A `<text>` has no element children (`V013`).
- Each line of the body is trimmed, and blank lines at the start and end are dropped, so indentation in the source never reaches the screen. Interior spaces are kept: `mail  {folder}` keeps its two spaces. For spacing around text, use `gap`, `padding`, or `margin` on a container.
- `wrap` is `nowrap` (the default: the line is cut at the edge), `wrap` (word-wrap to the width), or `truncate` (cut with `…` at the end); in `version="3"`, also `truncate-start` (`…` at the start, keeping the end of the line — useful for a path) and `truncate-middle` (`…` in the middle, keeping both ends). Without a `version`, `pre-wrap` keeps every space and line break and wraps at the width: use it for code and diffs.
- Width is measured in terminal columns by grapheme cluster: CJK and emoji take two columns, combining accents take none, and a cluster is never split.

### Rich text with runs [#rich-text-with-runs]

A `<text>` paints styled pieces of text when it has `runs` instead of a body. The host builds the pieces, from markdown for instance, and the document only styles them:

```tui
<text id="reply" runs="msg.runs" style="wrap: wrap"/>
```

```tcss
.code { color: cyan; bold: true; }
text > span.i { italic: true; }
```

```json
{"msg": {"runs": ["Use ", {"text": "go build", "class": "code"}, " and then check the ",
                  {"text": "go.mod", "class": "code i"}, " file"]}}
```

- `runs` is a path, resolved like `bind` (through `each` and `with` aliases, then the data). Its value is an array of runs, a string (one run), or `null` (no run). A run is a string, or an object with a string `text` and an optional `class`, a string of class names separated by spaces. Other members are ignored.
- Each run is styled as a `span`, a child of its text that only the stylesheet sees: `span`, `.code`, `span.code`, `text > span`, and `#reply > span.code` are all selectors that reach it. A span sets `color`, `background`, `bold`, `dim`, `italic`, `underline`, and `reverse`; whatever a rule does not set on it is the text's own, and any other property is accepted and ignored. You never write `<span>` yourself (`V001`).
- The runs are joined into one text, and `wrap`, `truncate`, `pre-wrap`, and `content-align` act on that text, so a word that crosses two runs stays one word in two styles.
- A run's text is data: `{…}` in it is never read, an escape sequence is removed, each tab becomes four spaces, and any other control character but a line feed is removed. A line feed starts a new line.
- A `<text runs>` takes no body text (`V013`). A missing path is `B003`, and a value of the wrong shape is `B013`, a warning that names the first bad run; `tuimark validate` needs `--data` to check them. `tuimark inspect --at X,Y` on a cell a run painted adds a `run` member with the run's index, text, classes, and style.

`<button>` takes its label from `label` or from its body; `title` and `placeholder` are templates too, and in the current language so is a button's label or body (`label="Save ({plan})"`). See [bindings](/guide/bindings) for how paths resolve.

## Screens and modals [#screens-and-modals]

A document with several screens shows one at a time. Only the active screen and its open modals are laid out; the host switches with `Set("@screen", "id")`, and in version 2 a keymap row can do it with `action="switch-to" to="#id"`.

A modal is open while its `open` attribute is truthy: `true`, `false`, a path, or `!path` (never `{path}`). `bind` on a modal is a shorthand for the same thing; if both are present, `open` wins. An open modal:

- is laid out above its screen, centered, with a single border by default. Unsized, it is as big as its content in the current language, and 80% × 80% of the screen in a document that writes `version`;
- traps focus: `tab` cycles inside it, and a focus request for a node outside it does nothing;
- fires `on:open` when it opens and `on:close` when it closes, and `on:escape` when `esc` is pressed. In the current language `esc` on a modal without `on:escape` closes it;
- gives focus back to where it was when it closes.

## Composing a document from files [#composing-a-document-from-files]

Past a screen or two, one file gets long. In the current language an `<include src="…"/>` places the contents of another file where it stands, so a document can be split by the pieces a person would name: the header, the footer, the keymap.

The included file is a **fragment**: its root is `<fragment>`, and the children of that root are what the include inserts. The `src` is a path relative to the file that writes the include.

```tui tab="app.tui"
<tui>
  <style src="theme.tcss"/>
  <include src="parts/keys.tui"/>
  <screen id="main">
    <include src="parts/header.tui"/>
    <list id="tasks" each="tasks as t" key="t.id" bind="selected">
      <item><text>{t.title}</text></item>
    </list>
    <include src="parts/footer.tui"/>
  </screen>
</tui>
```

```tui tab="parts/header.tui"
<fragment>
  <text id="title" class="title">Tasks</text>
  <rule/>
</fragment>
```

```tui tab="parts/footer.tui"
<fragment>
  <hints/>
</fragment>
```

```tui tab="parts/keys.tui"
<fragment>
  <keymap>
    <bind keys="q,ctrl+c" action="quit" label="quit"/>
    <bind keys="n" action="new_task" label="new"/>
  </keymap>
</fragment>
```

The document then renders as if the fragments' elements were written in place. The styles, the data, and the ids are shared, so `#title` in `header.tui` is the same `#title` a stylesheet or a `to="#title"` sees:

```grid
Tasks
────────────────────────────────────────
write the docs
ship it
q quit  n new
```

- **Where an include can stand.** Under `<tui>` (the `keys.tui` above), in a `<screen>`, inside a container, or inside a `<keymap>` for a fragment of `<bind>` rows. A fragment may hold several elements, and may include other fragments.
- **Nothing is global.** The fragment carries no attributes of its own (any attribute on a `<fragment>`, `version` included, is `V002`), and a `<fragment>` anywhere but the root of an included file is `V001`.
- **A fragment checked on its own is checked alone.** `tuimark validate parts/header.tui` checks that one file, with its own elements and attributes, and prints `ok (fragment checked on its own)`. The `<include>` elements inside it are checked for their attributes, but no file they name is read, so a broken include is reported when you validate the document that places it. A fragment is not a document: `dump` on one reports `V005`, "check it through the document that includes it".

```term
$ tuimark validate parts/header.tui
ok (fragment checked on its own)
```

What can go wrong with an include, all reported where the include stands:

| Code | When |
|---|---|
| `V006` | the include is a cycle, is outside the file system a `LoadFS` host gave the document, or cannot be read |
| `V020` | the file is a stylesheet (use `<style src>`), or its root is not `<fragment>` (a whole `<tui>` document is not a fragment) |
| `V021` | the expansions would pass the limits: 100000 elements, 16 MiB of placed source, or 256 levels of nesting |

An include that fails inserts nothing, and the rest of the document is still checked. A cycle names the files in the loop:

```term
$ tuimark validate app.tui
error V006 b.tui:3:3 /screen/include include src="a.tui" is an include cycle: app.tui → a.tui → b.tui → a.tui
```

The limit is there to stop an include that fans out into a tree of includes from filling the memory: it counts what the includes insert, and the first one that would go over inserts nothing and stops later ones from expanding.

`<template>` and `<use>` go one step further than a file: a `<template id="…">` directly under `<tui>` or `<screen>` holds a piece of markup, and each `<use template="…" with="path as alias"/>` stamps a copy of it, with the data bound to an alias and its ids scoped to the use. A template can also take literal text as props (`props="title"`, `<use title="CPU"/>`, `{$title}`) and one `<slot/>` that receives the content written between `<use>` and `</use>`. That is for a piece that repeats, such as a group of fields or a panel. [Templates and composition](/guide/composition) walks through them, with the scoped ids, the diagnostics, and the tools that show what a copy produced; the attributes are in the [tags reference](/reference/tags#added-by-the-current-language).

## What is not in the language [#what-is-not-in-the-language]

On purpose, and permanently:

- **No expressions.** No arithmetic, comparisons, filters, function calls, or array indexes. Markup has `{path}`, `each`, `if`, and guards; anything computed comes from the host as data.
- **No pixels.** One unit is one terminal cell. `px`, `em`, `vh` and friends are `V003`.
- **No open vocabulary.** Tags and properties are a fixed catalog, so an agent cannot invent a widget that silently does nothing.
- **No dynamic props, no named slots.** A prop is literal text replaced when the document is read, never a path or an expression; a template has one slot, with no name and no fallback content; and a slot cannot stand where content repeats per row. Data goes through `with`.
- **No generated code.** A `.tui` file is interpreted at run time; nothing is compiled to Go.

## Formatting [#formatting]

`tuimark fmt` rewrites a document in one canonical shape: two-space indentation, one element per line, attributes in a fixed order (`id`, `class`, the `class:NAME` guards, then the rest), self-closing empty elements, and comments kept in place.

```sh
tuimark fmt app.tui            # print the formatted document
tuimark fmt app.tui --write    # rewrite the file
tuimark fmt app.tui --check    # exit 2 when it is not formatted
```

It works on any well-formed document, even one with diagnostics, so it is safe to run before anything else.
