Wordwrap a string with ANSI escape codes
npx @tessl/cli install tessl/npm-wrap-ansi@9.0.0Wrap ANSI is a Node.js library that provides intelligent text wrapping for strings containing ANSI escape codes. It preserves terminal styling, colors, and formatting while wrapping text to specified column widths, making it ideal for CLI applications, logging systems, and terminal output formatting.
npm install wrap-ansiimport wrapAnsi from 'wrap-ansi';For TypeScript:
import wrapAnsi, { type Options } from 'wrap-ansi';For CommonJS:
const wrapAnsi = require('wrap-ansi');import chalk from 'chalk';
import wrapAnsi from 'wrap-ansi';
const input = 'The quick brown ' + chalk.red('fox jumped over ') +
'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
console.log(wrapAnsi(input, 20));
// Output: properly wrapped text with preserved ANSI stylingWraps words to the specified column width while preserving ANSI escape codes for styling, colors, and hyperlinks.
/**
* Wrap words to the specified column width.
* @param string - A string with ANSI escape codes, like one styled by chalk. Newline characters will be normalized to \n.
* @param columns - The number of columns to wrap the text to.
* @param options - Optional configuration object for wrapping behavior.
* @returns String with proper line wrapping and preserved ANSI styling.
*/
export default function wrapAnsi(string: string, columns: number, options?: Options): string;Usage Examples:
import wrapAnsi from 'wrap-ansi';
import chalk from 'chalk';
// Basic wrapping
const text = 'This is a long line that needs to be wrapped at a specific width.';
const wrapped = wrapAnsi(text, 20);
console.log(wrapped);
// Wrapping with ANSI colors
const coloredText = chalk.blue('Hello ') + chalk.red('colorful ') + chalk.green('world!');
const wrappedColored = wrapAnsi(coloredText, 10);
console.log(wrappedColored);
// Hard wrap mode - breaks long words
const longWord = 'supercalifragilisticexpialidocious';
const hardWrapped = wrapAnsi(longWord, 10, { hard: true });
console.log(hardWrapped);
// Disable word wrapping - fill columns completely
const noWordWrap = wrapAnsi('word1 word2 word3', 8, { wordWrap: false });
console.log(noWordWrap);
// Preserve whitespace
const preserveWhitespace = wrapAnsi(' spaced text ', 10, { trim: false });
console.log(preserveWhitespace);interface Options {
/**
* By default the wrap is soft, meaning long words may extend past the column width.
* Setting this to true will make it hard wrap at the column width.
* @default false
*/
readonly hard?: boolean;
/**
* By default, an attempt is made to split words at spaces, ensuring that they don't extend past the configured columns.
* If wordWrap is false, each column will instead be completely filled splitting words as necessary.
* @default true
*/
readonly wordWrap?: boolean;
/**
* Whitespace on all lines is removed by default. Set this option to false if you don't want to trim.
* @default true
*/
readonly trim?: boolean;
}hard: Controls word breaking behavior
false (default): Soft wrap - long words extend past column widthtrue: Hard wrap - long words are broken at column boundarieswordWrap: Controls space-based word splitting
true (default): Attempts to split at spaces, respecting word boundariesfalse: Fills columns completely, splitting words as necessarytrim: Controls whitespace handling
true (default): Removes whitespace from line beginnings and endsfalse: Preserves all whitespace as-is\r\n to \n and normalizes Unicode charactersThe function handles various edge cases gracefully:
trim: false with whitespace-only input)\r\n to \n automatically