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