Terminal
FreeA terminal for logs and for typing. Feed it an append-only `lines` stream (a build, a deploy, an agent's tool run) and it renders the ANSI colour subset — SGR 30–37, 90–97 and bold — as styled spans from the kit's tokens, never as markup; follows new output only while you are at the bottom, pausing with a "jump to latest" button and a count when you scroll up; copies the whole scrollback as plain text; and keeps the newest `maxLines`. Add a command registry and it becomes a shell with history, Tab completion and a real `<input>` prompt — and nothing typed is ever evaluated.
Complexity tier: Interactive The primitive bar, plus: its async states are declared — none is an answer — it has a keyboard model and restores focus, and any destructive action it offers can be undone.
import { SvTerminalComponent } from '@styloviz/code-block';Demo & usage
Basic usage
Usage example — open the gallery for the full interactive demo.
An interactive demo for this component lives in the gallery.
Open live demoMore examples
Stream a log
Append to the array as output arrives; a shorter array is a new run and clears the screen. `info` lines are notes the host adds that no process printed.
readonly log = signal<TerminalLine[]>([{ kind: 'info', text: 'Connected to build 4812' }]); onChunk(text: string) { this.log.update((lines) => [...lines, { kind: 'stdout', text }]); } // <sv-terminal [lines]="log()" [interactive]="false" logLabel="Build log" />
A shell with commands
Handlers are functions you write; input is looked up in the registry by name and never evaluated. Output can carry ANSI colour.
commands: TerminalCommand[] = [ { name: 'status', summary: 'Service health', run: () => ({ lines: out('api \u001b[32m● healthy\u001b[0m') }) }, { name: 'clear', summary: 'Clear the screen', run: () => ({ clear: true }) }, ]; // <sv-terminal [commands]="commands" prompt="ops@acme:~$" (ran)="log($event)" />
The parts, outside the component
import { svParseAnsi, svAnsiPlainText, svIsScrolledToEnd } from '@styloviz/code-block'; svParseAnsi('\u001b[1;31mFAIL\u001b[0m src/cart.spec.ts'); // [{ text: 'FAIL', color: 'red', bright: false, bold: true }, { text: ' src/cart.spec.ts', … }]
When to use
Showing CI, deploy or job output as it streams in, with the colours the tools already print. Showing an AI agent's shell or tool runs live in an ops console. A "try it" shell on a developer landing page, alongside ordinary links. Use `sv-code-block variant="terminal"` for a short, fixed transcript inside documentation.
Compare with
- Code Block — Code Block's `terminal` variant draws a fixed transcript and strips ANSI codes. Terminal is the live one: an append-only log stream with the ANSI colour subset, autoscroll that pauses when you scroll up, a max-lines buffer and an optional prompt with commands.
Async states
None — this component has nothing to load, so it has no loading, empty or error state to drive.
Inputs
| Name | Type | Default | Description |
|---|---|---|---|
| commands | readonly TerminalCommand[] | [] | The command registry. Anything not in here does not run. |
| intro | readonly TerminalLine[] | [] | Lines printed before the visitor's first keystroke. |
| prompt | string | '$' | Prompt string, e.g. `visitor@styloviz:~$`. |
| inputLabel | string | 'Terminal input' | Accessible label for the prompt input. |
| maxHeight | string | '22rem' | Max height of the scrollback. |
| minHeight | string | '0' | Minimum height of the scrollback. For a hero, where the terminal is a design element as much as a control: sized to its content, a five-line intro renders as a small box with the column's remaining height empty beneath it. A floor makes it read as a window that happens to be mostly unused, which is what a real one looks like before you type. |
| maxLines | number | 400 | Scrollback cap, in lines. A cap, not virtualisation. A session someone actually types is a few hundred lines at the outside; carrying a virtual scroller for a case that does not occur costs more than it saves. The oldest lines drop. |
| chrome | boolean | true | Show the window chrome (three dots + title). |
| title | string | '' | Title in the chrome bar. |
| typeIntro | boolean | true | Type the intro out character by character. Ignored under `prefers-reduced-motion`, where the intro prints at once. |
| typeSpeedMs | number | 18 | Milliseconds per character while typing the intro. |
| lines | readonly TerminalLine[] | [] | Output the host owns — a build log, a deploy stream, an agent's tool run. APPEND-ONLY. Lines added to the end are printed as they arrive, after whatever is already on screen. A SHORTER array means a new run: the screen clears and the new array prints from the top. Pass a new array each time (signals do), never a mutated one. While this is non-empty the intro prints at once rather than typing, so a log is never held back behind an animation. |
| interactive | boolean | true | Show the prompt. `false` makes a read-only log viewer: no input, no history, no click-to-focus — `[lines]` and `intro` are all it shows. |
| ansi | boolean | true | Honour the ANSI colour subset in line text: SGR 0 / 1 / 22 / 39, 30–37 and 90–97. Other sequences (cursor moves, erase, OSC titles) are always removed. `stderr` keeps its own colour — an escape code cannot paint an error green — but may still be bold. `false` strips every sequence and colours by `kind` only. |
| autoScroll | boolean | true | Follow new output to the bottom — but only while the reader is already there. Scrolling up pauses it and shows "jump to latest"; `false` never scrolls on its own (submitting a command still does). |
| copyable | boolean | true | Show a button that copies the whole scrollback as plain text, escape codes removed. |
| live | TerminalLive | 'polite' | How the log announces new lines. `polite` (the `role="log"` default) reads each line as it arrives; `off` is for a fast stream a screen reader could never keep up with — the reader moves into the log to read it. |
| logLabel | string | 'Terminal output' | Accessible name of the scrollback region. |
Outputs
| Event | Payload type | Description |
|---|---|---|
| ran | TerminalRanEvent | Fired after every submitted line, matched or not. |
| copied | string | Emits the copied text after a successful clipboard write. |
Customization & theming
Line kinds: `command`, `stdout`, `stderr`, `exit`, `info` — each coloured from the `gray`, `danger`, `warning` and `info` tokens; ANSI colours map onto `danger`, `accent`, `warning`, `primary`, `brand`, `info` and `gray`, one step darker in light mode. `ansi="false"` strips every escape sequence and colours by kind only. `autoScroll`, `copyable`, `chrome`, `title`, `minHeight`, `maxHeight`, `maxLines` and `typeIntro` tune the frame; `interactive="false"` removes the prompt. All visible strings are `codeBlock` labels in 12 language packs. `svParseAnsi`, `svAnsiPlainText` and `svIsScrolledToEnd` are exported for your own log views.
Best practices
Do: Pass a new array as lines arrive (signals do this naturally). The component prints only the tail beyond what it has already shown. Do: Name the log with `logLabel` when there is more than one terminal on a page. Don't: Do not route typed text into `eval` or a server shell from a command handler — the registry exists so that nothing typed is ever executed. Don't: Do not rely on colour for meaning: an error is a `stderr` line (and an escape code cannot recolour one), a note is an `info` line. Tip: Set `live="off"` for a stream faster than a screen reader can speak; readers move into the log to read it. Tip: Raise `maxLines` for long builds; the oldest lines drop first. Copy takes what is in the buffer.
Accessibility
The scrollback is a named `role="log"` with `aria-live="polite"` (or `off`), so new lines are announced once rather than the whole session re-read; it takes focus so a keyboard can scroll a paused log. Autoscroll never moves the reader: scrolling up pauses it, and a labelled "jump to latest" button (with the count of new lines) resumes it. The prompt is a labelled native `<input>`, so dictation, IME composition and assistive technology work; Tab moves on whenever there is nothing to complete. Copy confirms through a status message — and says so when the clipboard is unavailable, instead of claiming a copy that did not happen. Colour is never the only signal: errors are `stderr` lines, notes are `info` lines in italics, and the text of every line is plain text.
Feedback on Terminal
Spotted a gap in this page, or something that didn't work as described? Comments are GitHub Discussions in the public styloviz/community repo, so posting needs a GitHub account.