# Terminal

> A read-only terminal view with ANSI colors. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/terminal/

:::tip[In plain words]
Shows the text a program prints, with its colors, like a terminal window.
:::

A read-only view that understands ANSI colors and stays scrolled to the end. Feed it the output of a process you run (for example with `subprocess` in a thread and `pz.post`). It also accepts lines of `(text, color)` segments; `ui.ansi(text)` converts ANSI text into those segments.

`ui.terminal` · Category: [Code and tools](https://albakore.github.io/pyzed/library/#code-and-tools) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#terminal)

**Code:**

```python
out = ("$ pytest -q\n\x1b[32m........................\x1b[0m\n\x1b[32m24 passed\x1b[0m in 1.21s\n"
       "$ maturin develop --release\n\x1b[2m   Compiling pyzed v0.4.0\x1b[0m\n"
       "\x1b[32m✓\x1b[0m Installed pyzed-0.4.0\n$ ")
pz.container(ui.terminal(out), width=560, height=190)
```

## When to use

- To show build output, test runs, server logs or any command's output.

## Usage

```python
import pyzed as pz
from pyzed import ui

out = ("$ pytest -q\n\x1b[32m........................\x1b[0m\n\x1b[32m24 passed\x1b[0m in 1.21s\n"
       "$ maturin develop --release\n\x1b[2m   Compiling pyzed v0.4.0\x1b[0m\n"
       "\x1b[32m✓\x1b[0m Installed pyzed-0.4.0\n$ ")
pz.container(ui.terminal(out), width=560, height=190)
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in One Dark or One Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Content

```python
pz.container(ui.terminal("$ ls\nmain.py  notes.md  pyproject.toml\n$ "), width=380, height=80)
```

```python
pz.container(ui.terminal("\x1b[32m✓\x1b[0m 24 passed  \x1b[33m!\x1b[0m 1 warning  \x1b[31m✗\x1b[0m 0 failed"),
             width=380, height=50)
```

```python
pz.container(ui.terminal("$ python main.py\nHello", font_size=16), width=380, height=70)
```

```python
pz.container(ui.terminal([[("ok ", "#a1c181"), ("build", None)], [("err ", "#d07277"), ("lint", None)]]),
             width=380, height=60)
```

## Events

Terminal has no events of its own: it shows what you give it. Like every widget it also has the **18 common events** (`click`, `double_click`, `right_click`, `mouse_enter`, `drag`, `key_down`, `resize`…), attached with `.on(...)`: see [Events](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

## Properties and API

### `ui.terminal`

```python
terminal(lines, *, font_size=13, id=None, theme=None)
```

A read-only terminal view with ANSI colors. It stays scrolled to the end.

| Property | Default | Description |
| --- | --- | --- |
| `lines` | *required* | Text with ANSI escapes, or a list of lines of `(text, color)` segments. |
| `font_size` | `13` | Font size. |
| `id` | `None` | Stable identifier used to focus or scroll it with a `Command`. |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#terminal)

### `ui.ansi`

```python
ansi(text)
```

Converts text with ANSI colors into lines of `(text, color_name)` segments.

| Property | Default | Description |
| --- | --- | --- |
| `text` | *required* | Text with ANSI escapes. |

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#ansi)

## Related

- [Console](https://albakore.github.io/pyzed/library/console/): A dev-tools style log with levels, filter, copy and clear.
- [Editor](https://albakore.github.io/pyzed/library/editor/): A full code editor: highlighting, folding, minimap, git markers.
