CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/npm-wrap-ansi

tessl install tessl/npm-wrap-ansi@9.0.0

Wordwrap a string with ANSI escape codes

Agent Success

Agent success rate when using this tile

100%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.04x

Baseline

Agent success rate without this tile

96%

task.mdevals/scenario-5/

Text Formatter for Terminal Output

Build a text formatting utility that wraps text to fit within a specified width while providing flexible whitespace handling for different display contexts.

Requirements

Your utility should format text blocks to fit within terminal constraints while offering control over how whitespace is handled on wrapped lines.

Core Functionality

Implement a function formatText(text, width, preserveWhitespace) that:

  • Wraps input text to fit within the specified width
  • When preserveWhitespace is false (default), removes leading and trailing spaces from wrapped lines for clean output
  • When preserveWhitespace is true, maintains all whitespace exactly as provided in the input
  • Handles text with ANSI color codes (e.g., colored terminal output) correctly

Whitespace Handling Behavior

The formatter should demonstrate these behaviors:

  1. Standard trimming (default): Leading/trailing spaces on wrapped lines are removed for clean display
  2. Whitespace preservation: When enabled, all spaces are maintained, including those that might appear at line boundaries
  3. ANSI-aware processing: Color codes and styling should not affect the visual width calculations or whitespace handling

Test Cases

  • When wrapping 'The quick brown fox jumps over the lazy dog' at width 10 with trimming enabled, each wrapped line should have no leading/trailing spaces @test
  • When wrapping ' Hello World ' at width 8 with whitespace preservation enabled, leading and trailing spaces should be maintained @test
  • When wrapping ANSI colored text '\u001b[31mRed text\u001b[39m with trailing spaces ' at width 20 with trimming enabled, should remove trailing spaces while preserving color codes @test
  • When wrapping ' This is a longer piece of text that will wrap multiple times ' at width 15 with trimming enabled, each wrapped line should be trimmed of leading/trailing spaces @test

Implementation

@generates

API

/**
 * Formats text to fit within specified width with whitespace control
 *
 * @param {string} text - The text to format
 * @param {number} width - Maximum width in columns
 * @param {boolean} preserveWhitespace - If true, preserve all whitespace; if false, trim wrapped lines
 * @returns {string} Formatted text
 */
function formatText(text, width, preserveWhitespace = false) {
  // Implementation here
}

module.exports = { formatText };

Dependencies { .dependencies }

wrap-ansi { .dependency }

Provides text wrapping with ANSI code support and whitespace control.

@satisfied-by

Version

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