Rendering
Render a box to a string before writing it to a terminal, file, or web page. Rendering does not write the string. The examples below are complete unless marked as continuations.
Render synchronously
Use renderPlainSync for plain text. It strips annotations and preserves
trailing spaces, including spaces added by layout alignment.
Helloimport * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
const box = Box.text("Hello").pipe(Box.annotate(Ansi.bold));
const output = Box.renderPlainSync(box); // "Hello"Use renderPrettySync for an ANSI terminal. It interprets AnsiStyle
annotations and removes trailing whitespace from each rendered line.
Helloimport * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
const box = Box.text("Hello").pipe(Box.annotate(Ansi.bold));
const output = Box.renderPrettySync(box); // "\u001b[1mHello\u001b[0m"Neither function appends a newline. console.log(output) adds a newline when
it writes the string. For cursor-controlled output, use a writer that preserves
the exact string, as shown in Using Cmd.
This complete example makes the whitespace difference visible:
import * as Box from "effect-boxes/Box";
const box = Box.text("Hi").pipe(Box.alignHoriz(Box.left, 5));
const plain = Box.renderPlainSync(box); // "Hi "
const pretty = Box.renderPrettySync(box); // "Hi"
console.log(JSON.stringify({ plain, pretty }));
// {"plain":"Hi ","pretty":"Hi"}Render within an Effect program
renderPlain and renderPretty return an Effect with a built-in renderer.
They have the same whitespace behavior as their synchronous counterparts.
The Effect must run to produce the string.
import { Effect } from "effect";
import * as Box from "effect-boxes/Box";
const box = Box.text("Hello");
const plain: Effect.Effect<string> = Box.renderPlain(box);
const pretty: Effect.Effect<string> = Box.renderPretty(box);
const plainOutput = Effect.runSync(plain); // "Hello"
const prettyOutput = Effect.runSync(pretty); // "Hello"Configure a renderer layer
Use Box.render when the application supplies a renderer. Its default
preserveWhitespace setting is false. Set it explicitly when spaces form
part of the required output.
import { Effect, pipe } from "effect";
import * as Box from "effect-boxes/Box";
import * as Renderer from "effect-boxes/Renderer";
const box = Box.text("Hello").pipe(Box.alignHoriz(Box.left, 8));
const program = pipe(
Box.render(box, { preserveWhitespace: true }),
Effect.provide(Renderer.PlainRendererLive)
);
const output = Effect.runSync(program); // "Hello "Box.printBox renders and writes through Effect's console service. It adds a
newline, so use it for line-oriented output rather than terminal commands.
This complete example prints one greeting:
import { Effect } from "effect";
import * as Box from "effect-boxes/Box";
import * as Renderer from "effect-boxes/Renderer";
const print = Box.printBox(Box.text("Hello")).pipe(
Effect.provide(Renderer.PlainRendererLive)
);
Effect.runPromise(print);| Layer | Annotation interpretation | Output |
|---|---|---|
PlainRendererLive | Strips annotations | Plain text |
AnsiRendererLive | AnsiStyle | ANSI terminal text |
HtmlRendererLive | HtmlAnnotationData | HTML markup |
HtmlPrettyRendererLive | HtmlAnnotationData | Indented HTML markup |
HTML layers have their own whitespace defaults. See Using Html for serialization examples. Changing renderer layers does not translate ANSI styles into HTML annotations.
Track positions or render overlapping layers
Use Renderer.tracked for ordinary box trees when you need rendered text and
reactive positions together. See Using Reactive for
queries and updates. The reported coordinates are relative to the box's origin,
not the terminal cursor's position.
Use Renderer.frame for overlapping Layout.Stack layers. It resolves one
viewport, then encodes that snapshot through FrameEncoder. Plain and ANSI
renderer layers provide both ordinary rendering and frame encoding. Frame
rendering rejects terminal commands and HTML annotations. Write terminal
commands separately from the frame's output.
This complete example produces a three-column frame:
import { Effect } from "effect";
import * as Box from "effect-boxes/Box";
import * as Renderer from "effect-boxes/Renderer";
const frame = Effect.runSync(
Renderer.frame(Box.text("Hi").pipe(Box.alignHoriz(Box.left, 3)), {
preserveWhitespace: true,
}).pipe(Effect.provide(Renderer.PlainRendererLive))
);
const output = frame.output; // "Hi "
// frame.lines retains full-width lines, regardless of output whitespace trimming.For a popup example, see Render overlapping boxes.