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

Text Wrapping with Optimized Line Breaks

Build a text wrapping utility that intelligently handles long words by minimizing the total number of line breaks when text must be forcibly split.

Requirements

Your implementation should:

  1. Wrap text to a specified column width
  2. When a word is too long to fit on the current line or on a new line by itself, decide whether to:
    • Start wrapping the word immediately on the current line, OR
    • Move the entire word to the next line before wrapping it
  3. Choose the option that produces fewer total line breaks across all wrapped output
  4. Handle basic word-based wrapping (split on spaces)
  5. Support hard wrapping mode where no line exceeds the specified column width

Example Scenarios

Scenario 1: Word fits better starting on current line

Input: "abc verylongword" with width 10

Option A (start on current line):
"abc verylo"
"ngword"
Total: 2 lines, 1 break in the word

Option B (start on next line):
"abc"
"verylongwo"
"rd"
Total: 3 lines, 1 break in the word

Choose Option A (fewer total lines)

Scenario 2: Word fits better starting on next line

Input: "abcdefgh verylongword" with width 10

Option A (start on current line):
"abcdefgh v"
"erylongwor"
"d"
Total: 3 lines, 2 breaks in the word

Option B (start on next line):
"abcdefgh"
"verylongwo"
"rd"
Total: 3 lines, 1 break in the word

Choose Option B (same lines, fewer breaks in word)

Test Cases

  • Given "hello world" with width 8, wraps to "hello\nworld" @test
  • Given "test verylongwordhere" with width 10, minimizes line breaks by choosing optimal starting position @test
  • Given "short x" where x is a word longer than width, wraps x starting on new line when that produces fewer breaks @test
  • Given text with multiple long words, applies optimization logic to each word independently @test

API

/**
 * Wraps text to the specified column width with optimized line break handling.
 *
 * @param {string} text - The text to wrap
 * @param {number} width - The maximum column width
 * @returns {string} The wrapped text with newlines
 */
function wrapText(text, width) {
  // Implementation here
}

module.exports = { wrapText };

Implementation Notes

@generates

Dependencies { .dependencies }

wrap-ansi { .dependency }

Provides text wrapping functionality with support for optimized hard wrapping algorithm that minimizes line breaks.

@satisfied-by

Version

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