(buffer: TermTypes.IBuffer, startIndex: number, endIndex: number)
| 346 | * @returns Array of logical lines (with wrapped lines concatenated) |
| 347 | */ |
| 348 | export function bufferLinesToText(buffer: TermTypes.IBuffer, startIndex: number, endIndex: number): string[] { |
| 349 | const lines: string[] = []; |
| 350 | let currentLine = ""; |
| 351 | let isFirstLine = true; |
| 352 | |
| 353 | // Clamp indices to valid buffer range to avoid out-of-bounds access on the |
| 354 | // underlying circular buffer, which could return stale/wrong data. |
| 355 | const clampedStart = Math.max(0, Math.min(startIndex, buffer.length)); |
| 356 | const clampedEnd = Math.max(0, Math.min(endIndex, buffer.length)); |
| 357 | |
| 358 | for (let i = clampedStart; i < clampedEnd; i++) { |
| 359 | const line = buffer.getLine(i); |
| 360 | if (line) { |
| 361 | const lineText = line.translateToString(true); |
| 362 | // If this line is wrapped (continuation of previous line), concatenate without newline |
| 363 | if (line.isWrapped && !isFirstLine) { |
| 364 | currentLine += lineText; |
| 365 | } else { |
| 366 | // This is a new logical line |
| 367 | if (!isFirstLine) { |
| 368 | lines.push(currentLine); |
| 369 | } |
| 370 | currentLine = lineText; |
| 371 | isFirstLine = false; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // Don't forget the last line |
| 377 | if (!isFirstLine) { |
| 378 | lines.push(currentLine); |
| 379 | } |
| 380 | |
| 381 | // Trim trailing blank lines only when the requested range extends to the |
| 382 | // actual end of the buffer. A terminal allocates a fixed number of rows |
| 383 | // (e.g. 80) but only the first few may contain real content; the rest are |
| 384 | // empty placeholder rows. We strip those so callers don't receive a wall |
| 385 | // of empty strings. |
| 386 | // |
| 387 | // Crucially, if the caller requested a specific sub-range (e.g. lines |
| 388 | // 100-150) and lines 140-150 happen to be blank, those blanks are |
| 389 | // intentional and must NOT be removed. We only trim when the range |
| 390 | // reaches the very end of the buffer. |
| 391 | if (clampedEnd >= buffer.length) { |
| 392 | while (lines.length > 0 && lines[lines.length - 1] === "") { |
| 393 | lines.pop(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return lines; |
| 398 | } |
| 399 | |
| 400 | export function quoteForPosixShell(filePath: string): string { |
| 401 | return "'" + filePath.replace(/'/g, "'\\''") + "'"; |
no test coverage detected