Wordwrap a string with ANSI escape codes
Overall
score
100%
Build a text formatter that wraps styled terminal text while providing precise control over whitespace handling.
Your formatter should wrap text to fit within a specified column width. The key requirement is to provide users with control over how leading and trailing whitespace is handled on wrapped lines.
Implement two whitespace handling modes:
The formatter must correctly handle text containing terminal styling codes (ANSI escape sequences) - these should not affect width calculations or be removed during wrapping.
When text wraps across multiple lines:
Single-line text (text that fits within the column width without wrapping) should maintain its original formatting regardless of mode.
@generates
/**
* Wraps text to specified column width with configurable whitespace handling
* @param {string} text - The text to wrap (may contain ANSI styling codes)
* @param {number} columns - Maximum column width
* @param {object} options - Configuration options
* @param {boolean} options.preserveWhitespace - If true, preserve all whitespace; if false (default), trim whitespace from wrapped lines
* @returns {string} The wrapped text
*/
function formatText(text, columns, options) {
// Implementation here
}
module.exports = { formatText };Provides text wrapping with ANSI escape code support.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-wrap-ansidocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10