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-4/

Terminal Output Formatter

Overview

Build a terminal output formatter that wraps styled text to fit within specific column widths while maintaining all visual styling. The formatter should handle text containing color codes and text decorations commonly used in command-line interfaces.

Requirements

Your implementation should:

  1. Accept a string containing terminal styling codes, a target column width, and optional configuration
  2. Wrap the text to fit within the specified column width
  3. Preserve all styling codes and ensure they remain effective after wrapping
  4. Handle text that wraps across multiple lines, ensuring each line displays with the correct styles
  5. Support both soft wrapping (allowing long words to exceed the width) and hard wrapping (breaking long words at the column boundary)

Input Specifications

  • Input string may contain terminal escape sequences for colors and text styling
  • Column width will be a positive integer
  • Configuration options may include:
    • A flag to enable hard wrapping (breaking words at column boundaries)
    • Other wrapping behavior controls as needed

Output Specifications

  • Return a wrapped string with newline characters (\n) separating lines
  • All styling codes from the input must be present and functional in the output
  • When text wraps to a new line, styles should carry over correctly so each line can be displayed independently with proper formatting

Dependencies { .dependencies }

wrap-ansi { .dependency }

Provides text wrapping with terminal styling support.

Test Cases

Test 1: Basic styled text wrapping { .test }

File: formatter.test.js

Input:

  • Text with colored segments: "The quick brown " + [RED]"fox jumped over "[/RED] + "the lazy " + [GREEN]"dog and then ran"[/GREEN] + " away with the unicorn."
  • Width: 20 columns
  • Default wrapping mode

Expected behavior:

  • Text should wrap at approximately 20 columns
  • Red and green styling should be preserved on all wrapped lines
  • Each line should display with correct colors when output to terminal

Test 2: Hard wrapping with styled text { .test }

File: formatter.test.js

Input:

  • Text containing a long styled word: [BLUE]"supercalifragilisticexpialidocious"[/BLUE]
  • Width: 10 columns
  • Hard wrapping enabled

Expected behavior:

  • The long word should break at column 10
  • Blue styling should be maintained across all wrapped segments
  • No line should exceed 10 columns in visible width

Test 3: Multiple styles on wrapped text { .test }

File: formatter.test.js

Input:

  • Text with multiple overlapping styles: [BOLD]"This is bold and "[UNDERLINE]"this is both bold and underlined"[/UNDERLINE]" and back to just bold"[/BOLD]
  • Width: 25 columns
  • Default wrapping mode

Expected behavior:

  • Bold and underline styles should be preserved correctly
  • When bold underlined text wraps, both styles should continue on the next line
  • Styles should end at the correct positions in the wrapped output

Implementation Notes

  • You do NOT need to generate or parse terminal escape sequences manually
  • Focus on using the appropriate library functionality to handle the wrapping while preserving styles
  • Consider how terminal styling state needs to be maintained across line breaks
  • Ensure that the visual width (as it appears in a terminal) matches the specified column count, not the string length

Version

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