or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-wrap-ansi

Wordwrap a string with ANSI escape codes

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/wrap-ansi@9.0.x

To install, run

npx @tessl/cli install tessl/npm-wrap-ansi@9.0.0

index.mddocs/

Wrap ANSI

Wrap 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.

Package Information

  • Package Name: wrap-ansi
  • Package Type: npm
  • Language: TypeScript/JavaScript (ES Module)
  • Installation: npm install wrap-ansi
  • Node.js Version: >=18

Core Imports

import wrapAnsi from 'wrap-ansi';

For TypeScript:

import wrapAnsi, { type Options } from 'wrap-ansi';

For CommonJS:

const wrapAnsi = require('wrap-ansi');

Basic Usage

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 styling

Capabilities

Text Wrapping with ANSI Support

Wraps 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);

Options Configuration

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;
}

Option Details

hard: Controls word breaking behavior

  • false (default): Soft wrap - long words extend past column width
  • true: Hard wrap - long words are broken at column boundaries

wordWrap: Controls space-based word splitting

  • true (default): Attempts to split at spaces, respecting word boundaries
  • false: Fills columns completely, splitting words as necessary

trim: Controls whitespace handling

  • true (default): Removes whitespace from line beginnings and ends
  • false: Preserves all whitespace as-is

ANSI Support Features

  • Color codes: Preserves foreground and background colors (including 256-color and RGB support)
  • Text styling: Maintains bold, italic, underline, strikethrough formatting
  • Hyperlinks: Handles ANSI hyperlink escape sequences (OSC 8 hyperlinks)
  • Complex sequences: Supports nested and combined ANSI codes with proper state management
  • Unicode support: Correctly measures character width including full-width characters and emoji
  • Escape sequence restoration: Re-applies styling after line breaks to maintain visual continuity
  • String normalization: Automatically converts \r\n to \n and normalizes Unicode characters

Error Handling

The function handles various edge cases gracefully:

  • Empty strings return empty strings (unless trim: false with whitespace-only input)
  • Invalid column numbers are handled by the underlying logic
  • Malformed ANSI sequences are processed without throwing errors
  • Newline normalization converts \r\n to \n automatically