* Wrap text to fit within a given width, returning array of lines. * ANSI-aware: preserves styling across line breaks. * * @param hard - If true, break words that exceed width (needed when columns * are narrower than the longest word). Default false.
(text: string, width: number, options?: {
hard?: boolean;
})
| 42 | * are narrower than the longest word). Default false. |
| 43 | */ |
| 44 | function wrapText(text: string, width: number, options?: { |
| 45 | hard?: boolean; |
| 46 | }): string[] { |
| 47 | if (width <= 0) return [text]; |
| 48 | // Strip trailing whitespace/newlines before wrapping. |
| 49 | // formatToken() adds EOL to paragraphs and other token types, |
| 50 | // which would otherwise create extra blank lines in table cells. |
| 51 | const trimmedText = text.trimEnd(); |
| 52 | const wrapped = wrapAnsi(trimmedText, width, { |
| 53 | hard: options?.hard ?? false, |
| 54 | trim: false, |
| 55 | wordWrap: true |
| 56 | }); |
| 57 | // Filter out empty lines that result from trailing newlines or |
| 58 | // multiple consecutive newlines in the source content. |
| 59 | const lines = wrapped.split('\n').filter(line => line.length > 0); |
| 60 | // Ensure we always return at least one line (empty string for empty cells) |
| 61 | return lines.length > 0 ? lines : ['']; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Renders a markdown table using Ink's Box layout. |
no outgoing calls
no test coverage detected