Using Annotation
Attach metadata to a box to describe its style or application data. A renderer decides how to interpret that metadata. Start with Using Box if you have not composed a layout yet.
Every example below is complete. After installing the dependencies, save one as example.ts and run bun run example.ts with Bun or npx tsx example.ts with Node.
Attach a style
Box.text returns Box<never>. Box.annotate returns a box whose annotation type matches the attached data. Both calls preserve the text and dimensions.
Helloimport * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
const plain = Box.text("Hello");
const styled = Box.annotate(plain, Ansi.red);
const output = Box.renderPrettySync(styled);
console.log(output);Box.renderPrettySync interprets ANSI styles. Box.renderPlainSync produces text without styling. Neither renderer converts arbitrary application metadata into visible text.
Attach and inspect application data
Create an annotation, attach it to a box, and inspect its data. This example explicitly turns the data into a visible label.
Submit
id=submit, enabled=trueimport * as Box from "effect-boxes/Box";
import * as Annotation from "effect-boxes/Annotation";
const metadata = Annotation.createAnnotation({ id: "submit", enabled: true });
const button = Box.annotate(Box.text("Submit"), metadata);
const data = Annotation.getAnnotationData(metadata);
const output = Box.renderPlainSync(
Box.vcat(
[button, Box.text(`id=${data.id}, enabled=${data.enabled}`)],
Box.left
)
);
console.log(output);The attached value is also available through button.annotation. That property is optional on Box, so check that it exists before reading its data. Annotations implement Effect's Equal and Hash interfaces for structural comparison.
Replace an annotation
A box has one annotation at its root. Annotating the same box again replaces that root annotation. Combine styles with Ansi.combine when you want both.
Replaced
Combinedimport * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
const red = Box.annotate(Box.text("Replaced"), Ansi.red);
const replaced = Box.annotate(red, Ansi.green);
const combined = Box.annotate(
Box.text("Combined"),
Ansi.combine(Ansi.bold, Ansi.green)
);
const output = Box.renderPrettySync(Box.vcat([replaced, combined], Box.left));
console.log(output);Nest parent and child styles
Compose a styled child inside a parent, then style the parent. The child inherits the parent's bold style and adds red. The surrounding text remains bold.
Parent Child Parentimport * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
const child = Box.annotate(Box.text("Child"), Ansi.red);
const parent = Box.annotate(
Box.hcat([Box.text("Parent "), child, Box.text(" Parent")], Box.top),
Ansi.bold
);
const output = Box.renderPrettySync(parent);
console.log(output);A child style can override a parent style for the same attribute, such as foreground color. This is actual nesting: the parent annotation encloses the child annotation. Styling several sibling boxes does not create a parent annotation.
Transform annotation data
Box.reAnnotate transforms the root annotation data. It throws if the root has no annotation and does not transform child annotations.
Task
Status: READYimport * as Box from "effect-boxes/Box";
import * as Annotation from "effect-boxes/Annotation";
const tagged = Box.annotate(
Box.text("Task"),
Annotation.createAnnotation("ready")
);
const updated = Box.reAnnotate(tagged, (data) => data.toUpperCase());
const label = updated.annotation?.data ?? "missing";
const output = Box.renderPlainSync(
Box.vcat([updated, Box.text(`Status: ${label}`)], Box.left)
);
console.log(output);Remove annotations
Box.unAnnotate removes annotations from the root and all descendants. The text and layout remain unchanged. Compare the styled first line with the plain second line.
Parent Child
Parent Childimport * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
const nested = Box.annotate(
Box.hcat(
[Box.text("Parent "), Box.annotate(Box.text("Child"), Ansi.red)],
Box.top
),
Ansi.bold
);
const plain = Box.unAnnotate(nested);
const output = Box.renderPrettySync(Box.vcat([nested, plain], Box.left));
console.log(output);Choose an annotation module
Use Ansi for terminal styling, Html for HTML elements, and Reactive for position IDs.
Cmd constructs terminal control commands encoded as ANSI annotations. It uses the ANSI renderer rather than a separate Cmd renderer. Write command output directly to an ANSI terminal.
For annotation value guards, mapping, filtering, and merging, see the Annotation API reference. For renderer selection, see Rendering.