Using Box
A Box is a rectangle of text with a height (rows) and width (cols).
Build layouts by combining boxes, then render the result to a string.
Each operation returns a new box.
Every example below is complete. Copy an example into example.ts after
installing effect-boxes, then run bun example.ts or npx tsx example.ts with Node.
The terminal preview for each example shows the string printed by that example.
Create and render a box
Box.text creates one row per line. The longest line determines the width.
Use Box.renderPlainSync to get text without styling annotations.
Hello
Worldimport * as Box from "effect-boxes/Box";
const layout = Box.text("Hello\nWorld");
// layout.rows is 2; layout.cols is 5.
const output = Box.renderPlainSync(layout);
console.log(output);Box.char creates a box from one character. Box.line removes newlines
rather than creating more rows. Box.emptyBox(rows, cols) reserves blank space.
This row includes a two-column gap between the star and the text:
* HelloWorldimport * as Box from "effect-boxes/Box";
const layout = Box.hcat(
[Box.char("*"), Box.emptyBox(1, 2), Box.line("Hello\nWorld")],
Box.top
);
const output = Box.renderPlainSync(layout);
console.log(output);Wrap text
Box.para(text, alignment, width) wraps words to a column width.
Box.left aligns each wrapped line to the left edge.
Boxes combine text into rectangular
layouts. Join boxes horizontally or
vertically, align the result, and render
it to a terminal. import * as Box from "effect-boxes/Box";
const layout = Box.para(
"Boxes combine text into rectangular layouts. Join boxes horizontally or vertically, align the result, and render it to a terminal.",
Box.left,
40
);
const output = Box.renderPlainSync(layout);
console.log(output);Box.columns divides wrapped text into columns with a fixed width and height.
It returns an array of boxes. Join that array with Box.hsep to add space
between the columns. Here each column is 20 columns wide and 4 rows high:
Boxes combine text vertically, align
into rectangular the result, and
layouts. Join boxes render it to a
horizontally or terminal. import * as Box from "effect-boxes/Box";
const columns = Box.columns(
"Boxes combine text into rectangular layouts. Join boxes horizontally or vertically, align the result, and render it to a terminal.",
Box.left,
20,
4
);
const layout = Box.hsep(columns, 2, Box.top);
const output = Box.renderPlainSync(layout);
console.log(output);Compose boxes
Join boxes horizontally
Box.hcat places boxes beside each other. Its alignment controls where shorter
boxes sit vertically. Box.top lines up their first rows.
Left | Right
second row import * as Box from "effect-boxes/Box";
const layout = Box.hcat(
[Box.text("Left\nsecond row"), Box.text(" | "), Box.text("Right")],
Box.top
);
const output = Box.renderPlainSync(layout);
console.log(output);Use Box.punctuateH to put a separator between items:
Name | Age | Cityimport * as Box from "effect-boxes/Box";
const layout = Box.punctuateH(
[Box.text("Name"), Box.text("Age"), Box.text("City")],
Box.top,
Box.text(" | ")
);
const output = Box.renderPlainSync(layout);
console.log(output);Use Box.hsep for a fixed number of blank columns between items:
A B Cimport * as Box from "effect-boxes/Box";
const layout = Box.hsep(
[Box.text("A"), Box.text("B"), Box.text("C")],
3,
Box.top
);
const output = Box.renderPlainSync(layout);
console.log(output);Join boxes vertically
Box.vcat stacks boxes. Its alignment controls where narrower boxes sit
horizontally. Box.left lines up their left edges.
Top
Middle
Bottomimport * as Box from "effect-boxes/Box";
const layout = Box.vcat(
[Box.text("Top"), Box.text("Middle"), Box.text("Bottom")],
Box.left
);
const output = Box.renderPlainSync(layout);
console.log(output);Box.punctuateV adds a separator box between items:
Header
------
Content
------
Footer import * as Box from "effect-boxes/Box";
const layout = Box.punctuateV(
[Box.text("Header"), Box.text("Content"), Box.text("Footer")],
Box.left,
Box.text("------")
);
const output = Box.renderPlainSync(layout);
console.log(output);Box.vsep adds blank rows between items:
Top
Bottomimport * as Box from "effect-boxes/Box";
const layout = Box.vsep([Box.text("Top"), Box.text("Bottom")], 1, Box.left);
const output = Box.renderPlainSync(layout);
console.log(output);Append two boxes
Box.hAppend joins two boxes at the top edge. Box.vAppend joins them at
the left edge. Both support pipe, so you can build a layout one step at a time.
Hello World
Second row import * as Box from "effect-boxes/Box";
import { pipe } from "effect";
const layout = pipe(
Box.text("Hello "),
Box.hAppend(Box.text("World")),
Box.vAppend(Box.text("Second row"))
);
const output = Box.renderPlainSync(layout);
console.log(output);Align content inside a rectangle
Use Box.left, Box.right, Box.top, or Box.bottom to align an edge.
Box.center1 and Box.center2 both center content. They round the content and
container midpoint separately: center1 rounds up, and center2 rounds down.
When spare space is odd, the extra space depends on both widths. For odd-width
content in an even-width container, center1 puts it after the content and
center2 puts it before. For even-width content in an odd-width container,
the direction reverses.
This comparison centers one character in a six-column rectangle. The borders are part of the example code and make the padding visible:
┌──────┐ ┌──────┐
│ X │ │ X │
└──────┘ └──────┘import * as Box from "effect-boxes/Box";
const first = Box.border(
Box.alignHoriz(Box.text("X"), Box.center1, 6),
"single"
);
const second = Box.border(
Box.alignHoriz(Box.text("X"), Box.center2, 6),
"single"
);
const layout = Box.hsep([first, second], 2, Box.top);
const output = Box.renderPlainSync(layout);
console.log(output);Box.align sets both dimensions. Its arguments are horizontal alignment,
vertical alignment, height, then width. Borders add two rows and two columns
outside the requested dimensions. This interior is 3 rows by 7 columns:
┌───────┐
│ │
│ X │
│ │
└───────┘import * as Box from "effect-boxes/Box";
import { pipe } from "effect";
const layout = pipe(
Box.text("X"),
Box.align(Box.center1, Box.center1, 3, 7),
Box.border("single")
);
const output = Box.renderPlainSync(layout);
console.log(output);Box.alignVert sets only the height. Here Box.bottom places the text on
the last row of a three-row interior:
┌──────┐
│ │
│ │
│Bottom│
└──────┘import * as Box from "effect-boxes/Box";
import { pipe } from "effect";
const layout = pipe(
Box.text("Bottom"),
Box.alignVert(Box.bottom, 3),
Box.border("single")
);
const output = Box.renderPlainSync(layout);
console.log(output);Add space around a box
Movement functions grow the box rather than removing text or changing an
absolute terminal position. moveRight adds columns on the left, and moveDown
adds rows above. The border below shows the resulting rectangle:
┌──────────┐
│ │
│ │
│ Hello│
└──────────┘import * as Box from "effect-boxes/Box";
import { pipe } from "effect";
const layout = pipe(
Box.text("Hello"),
Box.moveRight(5),
Box.moveDown(2),
Box.border("single")
);
const output = Box.renderPlainSync(layout);
console.log(output);moveLeft adds columns on the right, and moveUp adds rows below:
┌──────────┐
│Hello │
│ │
│ │
└──────────┘import * as Box from "effect-boxes/Box";
import { pipe } from "effect";
const layout = pipe(
Box.text("Hello"),
Box.moveLeft(5),
Box.moveUp(2),
Box.border("single")
);
const output = Box.renderPlainSync(layout);
console.log(output);Truncate text
Box.truncate(width, position) limits each line to a width and inserts an
ellipsis where text was removed. The position selects the part to preserve.
A box that already fits is returned unchanged.
Box.left keeps the beginning:
This is a very…import * as Box from "effect-boxes/Box";
const layout = Box.truncate(
Box.text("This is a very long piece of text"),
15,
Box.left
);
const output = Box.renderPlainSync(layout);
console.log(output);Box.right keeps the end:
… piece of textimport * as Box from "effect-boxes/Box";
const layout = Box.truncate(
Box.text("This is a very long piece of text"),
15,
Box.right
);
const output = Box.renderPlainSync(layout);
console.log(output);Box.center1 keeps both ends:
This is…of textimport * as Box from "effect-boxes/Box";
const layout = Box.truncate(
Box.text("This is a very long piece of text"),
15,
Box.center1
);
const output = Box.renderPlainSync(layout);
console.log(output);Attach styling annotations
A Box<A> carries annotations of type A. An annotation is metadata attached
to content. The renderer decides how to interpret that metadata.
Box.annotate attaches a value to the content. Use Box.renderPrettySync to
interpret ANSI colors and terminal commands. This example prints red text:
Error!import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
const layout = Box.annotate(Box.text("Error!"), Ansi.red);
const output = Box.renderPrettySync(layout);
console.log(output);Box.renderPlainSync ignores annotations and returns the same text without
color or terminal control sequences:
Error!import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
const layout = Box.annotate(Box.text("Error!"), Ansi.red);
const output = Box.renderPlainSync(layout);
console.log(output);Use Using Annotation for annotation structure and nesting, Using Ansi for terminal styles, and Using Html for HTML output. The Rendering guide explains renderer layers and frame rendering.