Using Cmd
Use Cmd to move the terminal cursor, clear output, or switch screen buffers.
Commands are zero-size Box<AnsiStyle> values. Constructing a command or
rendering it does not change the terminal. A terminal changes only when you
write the rendered ANSI string to it.
The reference snippets below are continuations that require these imports:
import * as Box from "effect-boxes/Box";
import * as Cmd from "effect-boxes/Cmd";Use Box.renderPrettySync to preserve command sequences. Plain rendering
strips them. Use Terminal.display or another exact string writer to emit
commands. console.log adds a newline, which changes the cursor position.
Position the cursor
Absolute coordinates are zero-indexed columns and rows, relative to the terminal's top-left corner. They are not relative to a box or the current cursor.
Cmd.cursorTo(10, 5); // Column 10, row 5
Cmd.home; // Column 0, row 0Relative movement starts at the current cursor position:
Cmd.cursorMove(5, -2); // Five columns right, two rows up
Cmd.cursorUp(3);
Cmd.cursorDown(2);
Cmd.cursorForward(10);
Cmd.cursorBackward(4);
Cmd.cursorNextLine(1); // Start of next line
Cmd.cursorPrevLine(1); // Start of previous lineSave and restore the cursor when a temporary write must leave the current position unchanged. This continuation constructs the output but does not write it:
const status = Box.combineAll([
Cmd.cursorSavePosition,
Cmd.cursorTo(0, 0),
Box.text("Status: OK"),
Cmd.cursorRestorePosition,
]);
const statusOutput = Box.renderPrettySync(status);Clear the screen or lines
Cmd.clearScreen; // Clear the screen and move home
Cmd.eraseDown; // Clear from cursor to end of screen
Cmd.eraseUp; // Clear from cursor to start of screen
Cmd.eraseScreen; // Clear screen without moving cursor
Cmd.eraseLine; // Clear current line
Cmd.eraseEndLine; // Clear from cursor to end of line
Cmd.eraseStartLine; // Clear from cursor to start of lineeraseLines(n) deletes lines within the scroll region and shifts following
content upward. clearLines(n) erases the current line and the preceding
n - 1 lines in place. It leaves the cursor at column zero on the topmost
cleared line.
Cmd.eraseLines(5);
Cmd.clearLines(3);Before using clearLines(3), the cursor must be on the third line of the previous
frame. An extra newline would put it on the fourth line and clear the wrong area.
Hide the cursor and use an alternate screen
This continuation constructs setup and teardown strings. Neither string takes effect until written:
const setup = Box.renderPrettySync(
Box.combineAll([Cmd.altScreenEnter, Cmd.cursorHide, Cmd.clearScreen])
);
const teardown = Box.renderPrettySync(
Box.combineAll([Cmd.altScreenLeave, Cmd.cursorShow])
);The alternate screen keeps the main screen's content available when you leave. Always register cleanup before writing setup. The complete example below runs cleanup on success, failure, or Effect interruption.
Rewrite a progress display
Install the terminal implementation:
npm install effect-boxes "effect@^4" "@effect/platform-node@^4"Save this complete example as progress.ts. In an interactive ANSI terminal,
run bun run progress.ts with Bun or npx tsx progress.ts with Node.
It writes three lines, then replaces those same lines. Run it in a terminal at least 30 columns wide so the lines do not wrap.
These snapshots show the initial and final frames. They render the same text as
progressFrame(0) and progressFrame(100); the browser preview does not emulate
cursor movement or line clearing.
Downloading...
Progress: 0%
Please waitDownloading...
Progress: 100%
Done!import { Effect, Terminal } from "effect";
import { NodeRuntime, NodeTerminal } from "@effect/platform-node";
import * as Box from "effect-boxes/Box";
import * as Cmd from "effect-boxes/Cmd";
const progressFrame = (progress: number): Box.Box<never> =>
Box.vcat(
[
Box.text("Downloading..."),
Box.text(`Progress: ${progress}%`),
Box.text(progress === 100 ? "Done!" : "Please wait"),
],
Box.left
);
const program = Effect.gen(function* () {
const terminal = yield* Terminal.Terminal;
yield* terminal.display(Box.renderPrettySync(progressFrame(0)));
// The first write adds no trailing newline. The cursor is on frame line three.
yield* Effect.sleep("500 millis");
yield* terminal.display(
Box.renderPrettySync(
Box.combineAll([Cmd.clearLines(3), progressFrame(100)])
)
);
// Only after the final frame, move below it for the next shell prompt.
yield* terminal.display("\n");
});
NodeRuntime.runMain(program.pipe(Effect.provide(NodeTerminal.layer)));The first frame skips clearLines because this program has not written a
previous frame. Every replacement frame has the same three-line height.
If a line wraps or another writer moves the cursor, this relative clearing
scheme no longer describes the terminal's position.
Restore the terminal after a full-screen session
Save this complete example as screen.ts. In an interactive ANSI terminal,
run bun run screen.ts with Bun or npx tsx screen.ts with Node.
It shows a status line briefly, then restores the main screen and cursor visibility.
During the session, the alternate screen contains this line. Leaving the alternate screen restores the main screen's previous contents.
Status: runningimport { Effect, Terminal } from "effect";
import { NodeRuntime, NodeTerminal } from "@effect/platform-node";
import * as Box from "effect-boxes/Box";
import * as Cmd from "effect-boxes/Cmd";
const setup = Box.renderPrettySync(
Box.combineAll([Cmd.altScreenEnter, Cmd.cursorHide, Cmd.clearScreen])
);
const teardown = Box.renderPrettySync(
Box.combineAll([Cmd.altScreenLeave, Cmd.cursorShow])
);
const app = Effect.gen(function* () {
const terminal = yield* Terminal.Terminal;
yield* Effect.acquireUseRelease(
Effect.void,
() =>
Effect.gen(function* () {
// Cleanup is registered before the first terminal mutation.
yield* terminal.display(setup);
yield* terminal.display(
Box.renderPrettySync(
Box.combineAll([Cmd.home, Box.text("Status: running")])
)
);
yield* Effect.sleep("500 millis");
}),
() => terminal.display(teardown)
);
});
NodeRuntime.runMain(app.pipe(Effect.provide(NodeTerminal.layer)));acquireUseRelease runs teardown when the body succeeds, fails, or is interrupted.
A teardown write failure remains an error. NodeRuntime.runMain also connects
process signals to Effect interruption. Forceful process termination cannot run
cleanup. The example does not enable raw input mode or implement an event loop.
For overlapping frame rendering, write these setup commands separately from
Renderer.frame output. Frame resolution rejects terminal commands. See
Rendering and the Cmd API reference.