Directory tree
Build a directory tree from nested data, then style directory and file names. The finished tree looks like this:
.
├── d src
│ ├── f Box.ts
│ └── d utils
│ └── f format.ts
├── d tests
│ └── f box.test.ts
├── d assets
└── f package.jsonEach step below is a complete example. After
installing effect-boxes, copy a step into tree.ts
and run bun tree.ts or npx tsx tree.ts with Node. Replace the previous example when moving to the next step.
This tutorial uses supplied data; it does not read your filesystem.
Step 1: Render the top-level names
Represent each entry with a name and optional children. An entry with
children: [] is an empty directory; an entry without children is a file.
Use Box.vcat to put one name on each row:
src
tests
assets
package.jsonimport * as Box from "effect-boxes/Box";
type Entry = {
readonly name: string;
readonly children?: ReadonlyArray<Entry>;
};
const filesystem: ReadonlyArray<Entry> = [
{
name: "src",
children: [
{ name: "Box.ts" },
{ name: "utils", children: [{ name: "format.ts" }] },
],
},
{ name: "tests", children: [{ name: "box.test.ts" }] },
{ name: "assets", children: [] },
{ name: "package.json" },
];
const tree = Box.vcat(
filesystem.map((entry) => Box.text(entry.name)),
Box.left
);
const output = Box.renderPlainSync(tree);
console.log(output);The output lists only the four top-level entries. It preserves their order
in filesystem; this renderer does not sort entries.
Step 2: Add connectors
Use ├── when another sibling follows and └── for the last sibling.
Join the connector and name with Box.hsep and a one-column gap.
Add . on its own row to represent the root:
.
├── src
├── tests
├── assets
└── package.jsonimport * as Box from "effect-boxes/Box";
type Entry = {
readonly name: string;
readonly children?: ReadonlyArray<Entry>;
};
const filesystem: ReadonlyArray<Entry> = [
{
name: "src",
children: [
{ name: "Box.ts" },
{ name: "utils", children: [{ name: "format.ts" }] },
],
},
{ name: "tests", children: [{ name: "box.test.ts" }] },
{ name: "assets", children: [] },
{ name: "package.json" },
];
const renderEntries = (entries: ReadonlyArray<Entry>): Box.Box<never> =>
Box.vcat(
entries.map((entry, index) => {
const isLast = index === entries.length - 1;
const connector = Box.text(isLast ? "└──" : "├──");
return Box.hsep([connector, Box.text(entry.name)], 1, Box.top);
}),
Box.left
);
const tree = Box.vcat([Box.text("."), renderEntries(filesystem)], Box.left);
const output = Box.renderPlainSync(tree);
console.log(output);You should see └── package.json on the final row. Children are still hidden.
Step 3: Render child entries
Pass a prefix box into each recursive call. │ continues a parent branch;
a three-column blank box ends it. Keep the prefix separate from the label
so each level adds exactly four columns, including the one-column gap:
.
├── src
│ ├── Box.ts
│ └── utils
│ └── format.ts
├── tests
│ └── box.test.ts
├── assets
└── package.json import * as Box from "effect-boxes/Box";
type Entry = {
readonly name: string;
readonly children?: ReadonlyArray<Entry>;
};
const filesystem: ReadonlyArray<Entry> = [
{
name: "src",
children: [
{ name: "Box.ts" },
{ name: "utils", children: [{ name: "format.ts" }] },
],
},
{ name: "tests", children: [{ name: "box.test.ts" }] },
{ name: "assets", children: [] },
{ name: "package.json" },
];
const renderEntries = (
entries: ReadonlyArray<Entry>,
prefix: Box.Box<never> = Box.nullBox
): Box.Box<never> => {
if (entries.length === 0) return Box.nullBox;
return Box.vcat(
entries.flatMap((entry, index) => {
const isLast = index === entries.length - 1;
const connector = Box.text(isLast ? "└──" : "├──");
const continuation = isLast ? Box.emptyBox(1, 3) : Box.text("│ ");
const childPrefix =
prefix.cols > 0
? Box.hsep([prefix, continuation], 1, Box.top)
: continuation;
const parts =
prefix.cols > 0
? [prefix, connector, Box.text(entry.name)]
: [connector, Box.text(entry.name)];
const label = Box.hsep(parts, 1, Box.top);
const children =
entry.children === undefined
? Box.nullBox
: renderEntries(entry.children, childPrefix);
return children.rows > 0 ? [label, children] : [label];
}),
Box.left
);
};
const tree = Box.vcat([Box.text("."), renderEntries(filesystem)], Box.left);
const output = Box.renderPlainSync(tree);
console.log(output);The vertical line under src continues alongside utils/format.ts because
more top-level entries follow src. Under the last child, spaces replace
the child's vertical line. The empty assets directory adds no child rows.
Step 4: Style names independently
Add d for directories and f for files. These one-column markers keep
branch spacing predictable. Annotate only the name box with Ansi.bold
or Ansi.dim, then use Box.renderPrettySync to include those styles:
.
├── d src
│ ├── f Box.ts
│ └── d utils
│ └── f format.ts
├── d tests
│ └── f box.test.ts
├── d assets
└── f package.jsonimport * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
type Entry = {
readonly name: string;
readonly children?: ReadonlyArray<Entry>;
};
const filesystem: ReadonlyArray<Entry> = [
{
name: "src",
children: [
{ name: "Box.ts" },
{ name: "utils", children: [{ name: "format.ts" }] },
],
},
{ name: "tests", children: [{ name: "box.test.ts" }] },
{ name: "assets", children: [] },
{ name: "package.json" },
];
const renderEntries = (
entries: ReadonlyArray<Entry>,
prefix: Box.Box<never> = Box.nullBox
): Box.Box<Ansi.AnsiStyle> => {
if (entries.length === 0) return Box.nullBox;
return Box.vcat(
entries.flatMap((entry, index) => {
const isLast = index === entries.length - 1;
const connector = Box.text(isLast ? "└──" : "├──");
const continuation = isLast ? Box.emptyBox(1, 3) : Box.text("│ ");
const childPrefix =
prefix.cols > 0
? Box.hsep([prefix, continuation], 1, Box.top)
: continuation;
const isDirectory = entry.children !== undefined;
const marker = Box.text(isDirectory ? "d" : "f");
const name = Box.text(entry.name).pipe(
Box.annotate(isDirectory ? Ansi.bold : Ansi.dim)
);
const parts =
prefix.cols > 0
? [prefix, connector, marker, name]
: [connector, marker, name];
const label = Box.hsep(parts, 1, Box.top);
const children =
entry.children === undefined
? Box.nullBox
: renderEntries(entry.children, childPrefix);
return children.rows > 0 ? [label, children] : [label];
}),
Box.left
);
};
const tree = Box.vcat([Box.text("."), renderEntries(filesystem)], Box.left);
const output = Box.renderPrettySync(tree);
console.log(output);Directory names are bold and file names are dim. The connectors and markers keep their default style because they are separate boxes.
To use your own data, replace filesystem in the final example. Keep names
on one line so each label occupies one row. For entries from a real filesystem,
read the directories separately, then pass that nested data to renderEntries.
See Using Box for composition operations and
Using Ansi for other name styles.