uv library Ultraviolet

Ultraviolet (UV): High-performance terminal rendering and input.

The UV subsystem provides core rendering primitives, a diffing terminal renderer, structured cell buffers, and fast input decoders to build responsive, visually rich terminal UIs.

Key Components

  • Terminal: Lifecycle, I/O, and orchestration for UV apps.
  • Buffer: A 2D grid of Cells representing screen state.
  • Cell: A single glyph with UvStyle and optional Link.
  • UvTerminalRenderer: Efficient diff-based rendering to the terminal.
  • EventDecoder: Fast ANSI/kitty input decoder for keys and mouse.
  • Screen: High-level convenient API over buffers and rendering.
  • Canvas: Immediate-mode drawing utilities on top of buffers.

Quick Start

import 'package:artisanal/uv.dart';

void main() async {
  final terminal = Terminal();
  await terminal.start();

  // Draw a red "H" at (0, 0).
  terminal.setCell(0, 0, Cell(
    content: 'H',
    style: UvStyle(fg: UvColor.rgb(255, 0, 0)),
  ));

  terminal.draw();
  await terminal.stop();
}

Concepts

UV models the terminal as layers of drawable cells. A Buffer holds the current state, a UvTerminalRenderer diffs and flushes changes, and Terminal manages lifecycle and device capabilities. Use Canvas for immediate-mode drawing, or Screen for a higher-level facade.

Rendering

The UvTerminalRenderer computes minimal diffs between the previous and next Buffer frames and emits optimized ANSI/OSC sequences to the terminal. Combine it with TerminalCapabilities to adapt to device features (kitty, sixel, hyperlinks, etc.). For text drawing, prefer StyledString and the style ops to avoid per-cell overhead.

Example (pseudo-flow):

final screen = Screen(size: Rectangle(0, 0, 80, 24));
final renderer = UvTerminalRenderer();
// mutate screen/buffers
renderer.render(screen.buffer);

Input and Events

Input is decoded by EventDecoder into typed events:

Access mouse modes via MouseMode and buttons via MouseButton.

Performance Tips

  • Batch mutations on buffers to reduce diff churn.
  • Prefer region updates; avoid full-screen rewrites.
  • Use StyledString runs instead of per-cell style objects when possible.
  • Cache geometry and avoid repeated allocations in hot paths.
  • Detect capabilities once; gate feature use via TerminalCapabilities.

Compatibility

UV targets modern terminals with ANSI + extended capabilities (kitty/sixel). Behavior can vary across emulators; query TerminalCapabilities and gracefully degrade. Hyperlinks (Link, LinkState) and RGB (UvRgb) may be unavailable on legacy terminals; fall back to UvBasic16 or UvIndexed256 palettes.

UV models the terminal as layers of drawable cells. A Buffer holds the current state, a UvTerminalRenderer diffs and flushes changes, and Terminal manages lifecycle and device capabilities. Use Canvas for immediate-mode drawing, or Screen for a higher-level facade.

The UvTerminalRenderer computes minimal diffs between the previous and next Buffer frames and emits optimized ANSI/OSC sequences to the terminal. Combine it with TerminalCapabilities to adapt to device features (kitty, sixel, hyperlinks, etc.). For text drawing, prefer StyledString and the style ops to avoid per-cell overhead.

Example (pseudo-flow):

final screen = Screen(size: Rectangle(0, 0, 80, 24));
final renderer = UvTerminalRenderer();
// mutate screen/buffers
renderer.render(screen.buffer);

Input is decoded by EventDecoder into typed events:

Access mouse modes via MouseMode and buttons via MouseButton.

  • Batch mutations on buffers to reduce diff churn.
  • Prefer region updates; avoid full-screen rewrites.
  • Use StyledString runs instead of per-cell style objects when possible.
  • Cache geometry and avoid repeated allocations in hot paths.
  • Detect capabilities once; gate feature use via TerminalCapabilities.

UV targets modern terminals with ANSI + extended capabilities (kitty/sixel). Behavior can vary across emulators; query TerminalCapabilities and gracefully degrade. Hyperlinks (Link, LinkState) and RGB (UvRgb) may be unavailable on legacy terminals; fall back to UvBasic16 or UvIndexed256 palettes.

Classes

Buffer
A 2D buffer of Lines representing a terminal screen or a portion of it.
Canvas
Canvas is a cell-buffer that can be used to compose and draw Drawables.
Cell
A single cell in a terminal Buffer.
Compositor
Compositor manages layer composition, drawing and hit testing.
Event
Base type for UV-style input events.
EventDecoder
A high-performance ANSI input decoder.
Fixed
A constraint that represents a fixed size.
FocusEvent
Terminal focus gained event.
KeyEvent
Base type for key press/release events.
KeyPressEvent
Key press event.
KittyGraphicsEvent
Kitty graphics payload (APC G ... ST).
Layer
Layer represents a visual layer with content and positioning.
LayerHit
LegacyKeyEncoding
Legacy key encoding behavior flags.
Line
A line is a fixed-width list of cells.
LineData
Metadata for a touched line.
Upstream: third_party/ultraviolet/cell.go (Link). Terminal hyperlink metadata (OSC 8).
LinkState
MouseButton
Mouse button codes (X11-style).
MouseClickEvent
Mouse click event.
MouseEvent
Base type for mouse click/release/wheel/motion events.
MouseMotionEvent
Mouse motion (move/drag) event.
PasteEvent
Bracketed paste content.
Percent
A constraint that represents a percentage of the available size.
Position
Upstream: third_party/ultraviolet/buffer.go (Position, Rectangle). A 2D integer coordinate in terminal cell space.
PrimaryDeviceAttributesEvent
Primary device attributes (DA1) report.
Rectangle
Rectangle with inclusive-exclusive bounds: [min, max).
RenderMetrics
Tracks render performance metrics including FPS, frame times, and render durations.
Screen
Screen is a 2D surface of cells.
StyledString
StyledString is a string that can be decomposed into a series of styled lines and cells.
StyleState
Terminal
Terminal represents a terminal screen that can be manipulated and drawn to.
TerminalCapabilities
Terminal capabilities discovered via ANSI queries.
UvBasic16
16-color palette index (optionally bright).
UvBorder
A drawable border composed of Sides and corner glyphs.
UvColor
Color representation sufficient for Ultraviolet parity tests.
UvIndexed256
256-color indexed palette entry.
UvRgb
24-bit RGBA color.
UvStyle
Upstream: third_party/ultraviolet/cell.go (UvStyle). Style attributes for a terminal Cell.
UvTerminalRenderer
Low-level terminal renderer for the Ultraviolet engine.
WindowSizeEvent
Reports window size in cells and optional pixels.

Enums

MouseMode
Mouse mode.

Functions

newLayer(Object content, [List<Layer> layers = const []]) Layer
Creates a new Layer from a String or Drawable.
newStyledString(String str) StyledString
readStyle(List<_SgrParam> params, StyleState out) → void
rect(int x, int y, int width, int height) Rectangle
Creates a rectangle from origin (x, y) and size (width, height).
splitHorizontal(Rectangle area, Constraint c) → ({Rectangle left, Rectangle right})
Splits area horizontally into left/right.
splitVertical(Rectangle area, Constraint c) → ({Rectangle bottom, Rectangle top})
Splits area vertically into top/bottom.