({
content,
// 1-indexed
startLine,
}: {
content: string
startLine: number
})
| 288 | * Adds cat -n style line numbers to the content. |
| 289 | */ |
| 290 | export function addLineNumbers({ |
| 291 | content, |
| 292 | // 1-indexed |
| 293 | startLine, |
| 294 | }: { |
| 295 | content: string |
| 296 | startLine: number |
| 297 | }): string { |
| 298 | if (!content) { |
| 299 | return '' |
| 300 | } |
| 301 | |
| 302 | const lines = content.split(/\r?\n/) |
| 303 | |
| 304 | if (isCompactLinePrefixEnabled()) { |
| 305 | return lines |
| 306 | .map((line, index) => `${index + startLine}\t${line}`) |
| 307 | .join('\n') |
| 308 | } |
| 309 | |
| 310 | return lines |
| 311 | .map((line, index) => { |
| 312 | const numStr = String(index + startLine) |
| 313 | if (numStr.length >= 6) { |
| 314 | return `${numStr}→${line}` |
| 315 | } |
| 316 | return `${numStr.padStart(6, ' ')}→${line}` |
| 317 | }) |
| 318 | .join('\n') |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Inverse of addLineNumbers — strips the `N→` or `N\t` prefix from a single |
no test coverage detected