wrap-ansi column wrapping: working versions (tested 2026-10-16)
Wrap long text to a fixed column width with wrap-ansi, exercising hard word breaking, whitespace trimming and ANSI code preservation offline.
passed5 passed · 0 failed2026-10-16 19:55 UTC- Status
- passed
- Last passed
- 2026-10-16 19:55 UTC
- Tests
- 5 / 5
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
wrap-ansi@10.0.2@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38958328921
The code
Exactly the files that passed. Your agent can pull them with get_recipe("wrap-ansi-columns") (install), or see them on GitHub.
package.json
{
"name": "wrap-ansi-columns",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"wrap-ansi": "10.0.2"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/wrap.ts
import wrapAnsi from "wrap-ansi";
export type WrapOptions = {
columns: number;
hard?: boolean;
trim?: boolean;
};
export function wrap(text: string, options: WrapOptions): string {
const { columns, hard = false, trim = true } = options;
return wrapAnsi(text, columns, { hard, trim });
}
export function wrapLines(text: string, options: WrapOptions): string[] {
return wrap(text, options).split("\n");
}
export function visibleWidth(value: string): number {
return value.replace(/\u001B\[[0-9;]*m/g, "").length;
}
test/wrap.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { wrap, wrapLines, visibleWidth } from "../src/wrap.ts";
const SENTENCE = "The quick brown fox jumped over the lazy dog";
const stripAnsi = (value: string): string => value.replace(/\u001B\[[0-9;]*m/g, "");
test("wraps long text to a column width on word boundaries", () => {
const output = wrap(SENTENCE, { columns: 20 });
assert.equal(output, "The quick brown fox\njumped over the lazy\ndog");
for (const line of wrapLines(SENTENCE, { columns: 20 })) {
assert.ok(line.length <= 20, `line exceeds columns: ${line}`);
}
});
test("hard wrap breaks words longer than the column width", () => {
const output = wrap("abcdefghijklm", { columns: 5, hard: true });
assert.equal(output, "abcde\nfghij\nklm");
for (const line of wrapLines("abcdefghijklm", { columns: 5, hard: true })) {
assert.ok(line.length <= 5, `line exceeds columns: ${line}`);
}
});
test("soft wrap keeps long words intact", () => {
const output = wrap("abcdefghijklm", { columns: 5 });
assert.equal(output, "abcdefghijklm");
assert.equal(visibleWidth(output), 13);
});
test("trim removes whitespace on wrapped line boundaries", () => {
const trimmed = wrap(SENTENCE, { columns: 20, trim: true });
const kept = wrap(SENTENCE, { columns: 20, trim: false });
assert.equal(trimmed, "The quick brown fox\njumped over the lazy\ndog");
assert.equal(kept, "The quick brown fox \njumped over the lazy\n dog");
assert.notEqual(trimmed, kept);
});
test("preserves ANSI escape codes while wrapping", () => {
const colored = "\u001B[31mThe quick brown fox\u001B[39m";
const output = wrap(colored, { columns: 10 });
const lines = output.split("\n");
assert.deepEqual(lines.map(stripAnsi), ["The quick", "brown fox"]);
assert.ok(output.includes("\u001B[31m"));
assert.ok(output.includes("\u001B[39m"));
for (const line of lines) {
assert.ok(visibleWidth(line) <= 10, `visible line exceeds columns: ${line}`);
}
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.