* Count how many visual terminal rows a string occupies, accounting for * line wrapping. Each `\n` is one row, and content wider than the terminal * wraps to additional rows.
(text: string)
| 94 | * wraps to additional rows. |
| 95 | */ |
| 96 | function countVisualLines(text: string): number { |
| 97 | // eslint-disable-next-line custom-rules/prefer-use-terminal-size |
| 98 | const cols = process.stdout.columns || 80 // non-React CLI context |
| 99 | let count = 0 |
| 100 | // Split on newlines to get logical lines |
| 101 | for (const logical of text.split('\n')) { |
| 102 | if (logical.length === 0) { |
| 103 | // Empty segment between consecutive \n — counts as 1 row |
| 104 | count++ |
| 105 | continue |
| 106 | } |
| 107 | const width = stringWidth(logical) |
| 108 | count += Math.max(1, Math.ceil(width / cols)) |
| 109 | } |
| 110 | // The trailing \n in "line\n" produces an empty last element — don't count it |
| 111 | // because the cursor sits at the start of the next line, not a new visual row. |
| 112 | if (text.endsWith('\n')) { |
| 113 | count-- |
| 114 | } |
| 115 | return count |
| 116 | } |
| 117 | |
| 118 | /** Write a status line and track its visual line count. */ |
| 119 | function writeStatus(text: string): void { |