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

Terminal Status Display Formatter

Overview

Build a terminal status display formatter that handles styled terminal output. The formatter should correctly wrap status messages containing colors and styling to fit within specified column widths while maintaining proper visual alignment.

Requirements

Create a module that formats status messages for terminal display with the following capabilities:

1. Status Message Formatting

Implement a function that formats status messages with the following behavior:

  • Accept a status message string that may contain ANSI color codes and styling
  • Accept a maximum column width parameter
  • Wrap the text to fit within the specified width
  • Preserve all visual styling (colors, bold, underline, etc.) in the output
  • Return the formatted string with proper line breaks

2. Width Calculation

The formatter must correctly calculate visual width:

  • ANSI escape codes should not count toward the visual width
  • Only visible characters should be counted when determining line breaks
  • Unicode characters (including fullwidth characters) should be measured by their display width

3. Text Wrapping Behavior

  • Break lines at word boundaries when possible
  • Ensure no line exceeds the specified column width
  • Maintain readability of the wrapped output

Input/Output Specification

Function Signature

formatStatusMessage(message, columnWidth)

Parameters:

  • message (string): Status message that may contain ANSI escape codes
  • columnWidth (number): Maximum number of visible columns per line

Returns:

  • (string): Formatted message with line breaks, preserving all styling

Test Cases { .tests }

Test Case 1: Basic Styled Message @test

File: formatter.test.js

import { formatStatusMessage } from './formatter.js';

// Basic styled message wrapping
const message = '\x1b[32mSuccess:\x1b[0m Operation completed successfully and all systems are operational';
const result = formatStatusMessage(message, 30);

// Should wrap at word boundaries while preserving color codes
// Expected output spans multiple lines, no line exceeds 30 visible characters
console.assert(result.includes('\n'), 'Output should contain line breaks');
console.assert(result.includes('\x1b[32m'), 'Output should preserve color codes');

Test Case 2: Multiple Styles @test

File: formatter.test.js

import { formatStatusMessage } from './formatter.js';

// Message with multiple ANSI codes
const message = '\x1b[1m\x1b[33mWarning:\x1b[0m The \x1b[4mconfiguration file\x1b[0m was not found';
const result = formatStatusMessage(message, 25);

// Should preserve bold, yellow, and underline codes
console.assert(result.includes('\x1b[1m'), 'Output should preserve bold');
console.assert(result.includes('\x1b[33m'), 'Output should preserve yellow');
console.assert(result.includes('\x1b[4m'), 'Output should preserve underline');

Test Case 3: Long Word Handling @test

File: formatter.test.js

import { formatStatusMessage } from './formatter.js';

// Message with a very long word
const message = '\x1b[31mError:\x1b[0m FileNotFoundException in /very/long/path/to/configuration/file.json';
const result = formatStatusMessage(message, 20);

// Should break long words that exceed column width
const lines = result.split('\n');
console.assert(lines.every(line => {
    // Remove ANSI codes for width check
    const visibleText = line.replace(/\x1b\[[0-9;]*m/g, '');
    return visibleText.length <= 20;
}), 'No line should exceed 20 visible characters');

Dependencies { .dependencies }

wrap-ansi { .dependency }

Provides text wrapping with ANSI escape code support.

Implementation Notes

  • Focus on correctly handling the visual width of text that contains ANSI codes
  • The formatter should work with standard ANSI SGR (Select Graphic Rendition) codes
  • Test with various terminal color and style combinations

Version

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