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