Skip to content

layout ​

Build terminal output as a tree of boxes, rows, and columns, then render it to a styled string you can print. Handy for splash screens, summary panels, and side-by-side columns.

ts
import { box, render } from "std::ui/layout"

const panel = box(title: "Hello", padding: 1) as b {
  b.text("Welcome!", bold: true)
  b.text("How are you?")
}
print(render(panel))

Containers (box, row, column) take a width: a number of columns, "50%" of the parent, or "full" for the parent width (the terminal at the root). Content wraps to fit its container; pass wrap: false to raw to preserve exact layout (ASCII art, pre-rendered tables). Unsized containers shrink to fit their content but cap at the available width, wrapping anything longer.

For prompts that read a key or a line, use std::ui/cli.

Types ​

LayoutNode ​

Every layout node has the same shape. Containers carry their content in children; leaves have children: [].

ts
/**
Every layout node has the same shape. Containers carry their
content in `children`; leaves have `children: []`.
*/
export type LayoutNode = {
  type: string;
  attrs: any;
  children: any[]
}

(source)

LayoutBuilder ​

Methods inside a container's trailing as name { ... } block. Each method constructs a leaf or container and pushes it onto the surrounding container's children array.

ts
/**
Methods inside a container's trailing `as name { ... }` block. Each
method constructs a leaf or container and pushes it onto the
surrounding container's `children` array.
*/
export type LayoutBuilder = {
  text: any;
  raw: any;
  space: any;
  hline: any;
  vline: any;
  row: any;
  column: any;
  box: any
}

(source)

Alignment ​

ts
export type Alignment = "start" | "center" | "end"

(source)

BorderStyle ​

ts
export type BorderStyle = "rounded" | "heavy" | "double" | "light" | "none"

(source)

Width ​

A column count, a percentage such as "50%", or "full" for the parent width.

ts
/** A column count, a percentage such as "50%", or "full" for the parent width. */
export type Width = number | "full" | string

(source)

Functions ​

text ​

ts
text(
  content: string,
  fgColor: string = "",
  bgColor: string = "",
  bold: boolean = false,
  italic: boolean = false,
  dim: boolean = false,
  underline: boolean = false,
  align: Alignment = "start",
): LayoutNode

A styled run of text. Newlines split it into multiple lines.

Parameters:

NameTypeDefaultDescription
contentstring
fgColorstring""Foreground color, named ("red") or hex ("#cc7a4a")
bgColorstring""Background color, named or hex
boldbooleanfalse
italicbooleanfalse
dimbooleanfalseUse reduced intensity
underlinebooleanfalse
alignAlignment"start"For multi-line text, how shorter lines align to the longest

Returns: LayoutNode

(source)

raw ​

ts
raw(
  content: string,
  align: Alignment = "start",
  wrap: boolean = true,
): LayoutNode

Build a layout node from text with existing ANSI styling or newlines.

Use wrap: false for ASCII art or tables whose spacing must stay intact. Wrapping preserves ANSI colors without bleeding into surrounding borders.

Parameters:

NameTypeDefaultDescription
contentstring
alignAlignment"start"How shorter lines align to the longest
wrapbooleantrueReflow to the container width; false preserves exact spacing

Returns: LayoutNode

(source)

space ​

ts
space(count: number = 1): LayoutNode

Blank space. Inside a row it adds columns; inside a column, rows.

Parameters:

NameTypeDefaultDescription
countnumber1Number of cells of blank space

Returns: LayoutNode

(source)

hline ​

ts
hline(
  char: string = "─",
  length: number = 0,
  fgColor: string = "",
  bold: boolean = false,
  dim: boolean = false,
): LayoutNode

A horizontal rule. Inside a column, leave length at 0 to span the column's width.

Parameters:

NameTypeDefaultDescription
charstring"─"The character to repeat
lengthnumber0Explicit length (0 lets the parent size it)
fgColorstring""Color of the line
boldbooleanfalse
dimbooleanfalseUse reduced intensity

Returns: LayoutNode

(source)

vline ​

ts
vline(
  char: string = "│",
  length: number = 0,
  fgColor: string = "",
  bold: boolean = false,
  dim: boolean = false,
): LayoutNode

A vertical rule. Inside a row, leave length at 0 to span the row's height.

Parameters:

NameTypeDefaultDescription
charstring"│"The character to repeat
lengthnumber0Explicit length (0 lets the parent size it)
fgColorstring""Color of the line
boldbooleanfalse
dimbooleanfalseUse reduced intensity

Returns: LayoutNode

(source)

row ​

ts
row(
  gap: number = 0,
  align: Alignment = "start",
  width: Width = null,
  children: LayoutNode[] = null,
  block: (LayoutBuilder) -> void = null,
): LayoutNode

A horizontal container. Children render left to right.

Parameters:

NameTypeDefaultDescription
gapnumber0Cells of blank space between children
alignAlignment"start"Vertical alignment of shorter children
widthWidthnullColumns, a percentage of the parent, or "full" for the parent width
childrenLayoutNode[]nullPre-built child nodes
block(LayoutBuilder) => voidnullTrailing builder block, appended after children

Returns: LayoutNode

(source)

column ​

ts
column(
  gap: number = 0,
  align: Alignment = "start",
  width: Width = null,
  children: LayoutNode[] = null,
  block: (LayoutBuilder) -> void = null,
): LayoutNode

A vertical container. Children render top to bottom.

Parameters:

NameTypeDefaultDescription
gapnumber0Blank rows between children
alignAlignment"start"Horizontal alignment of narrower children
widthWidthnullColumns, a percentage of the parent, or "full" for the parent width
childrenLayoutNode[]nullPre-built child nodes
block(LayoutBuilder) => voidnullTrailing builder block, appended after children

Returns: LayoutNode

(source)

box ​

ts
box(
  title: string = "",
  titleColor: string = "",
  borderStyle: BorderStyle = "rounded",
  borderColor: string = "",
  padding: number = 1,
  width: Width = null,
  children: LayoutNode[] = null,
  block: (LayoutBuilder) -> void = null,
): LayoutNode

A bordered panel. Multiple children stack vertically inside it.

Parameters:

NameTypeDefaultDescription
titlestring""Text shown in the top border (empty for none)
titleColorstring""Color of the title text
borderStyleBorderStyle"rounded"The border character style; "none" draws no frame (padding and title still apply, the title as a plain first line)
borderColorstring""Color of the border
paddingnumber1Cells of padding between the border and content
widthWidthnullColumns, a percentage of the parent, or "full" for the parent width
childrenLayoutNode[]nullPre-built child nodes
block(LayoutBuilder) => voidnullTrailing builder block, appended after children

Returns: LayoutNode

(source)

render ​

ts
render(
  node: LayoutNode,
  color: "auto" | boolean = "auto",
  cols: number = 0,
  rows: number = 0,
): string

Render a layout tree to a styled, multi-line string ready to print.

Parameters:

NameTypeDefaultDescription
nodeLayoutNode
color"auto" | boolean"auto""auto" uses terminal color support; true always; false strips styling
colsnumber0Width override in columns (0 auto-detects)
rowsnumber0Height override in rows (0 uses the default)

Returns: string

Needs: terminal

(source)