* Serializes terminal content as plain text (no escape sequences) * @param options Custom options to allow control over what gets serialized.
(options?: { scrollback?: number; trimWhitespace?: boolean })
| 561 | * @param options Custom options to allow control over what gets serialized. |
| 562 | */ |
| 563 | public serializeAsText(options?: { scrollback?: number; trimWhitespace?: boolean }): string { |
| 564 | if (!this._terminal) { |
| 565 | throw new Error("Cannot use addon until it has been loaded") |
| 566 | } |
| 567 | |
| 568 | const buffer = getTerminalBuffers(this._terminal) |
| 569 | |
| 570 | if (!buffer) { |
| 571 | return "" |
| 572 | } |
| 573 | |
| 574 | const activeBuffer = buffer.active ?? buffer.normal |
| 575 | if (!activeBuffer) { |
| 576 | return "" |
| 577 | } |
| 578 | |
| 579 | const maxRows = activeBuffer.length |
| 580 | const scrollback = options?.scrollback |
| 581 | const correctRows = scrollback === undefined ? maxRows : constrain(scrollback + this._terminal.rows, 0, maxRows) |
| 582 | |
| 583 | const startRow = maxRows - correctRows |
| 584 | const endRow = maxRows - 1 |
| 585 | const lines: string[] = [] |
| 586 | |
| 587 | for (let row = startRow; row <= endRow; row++) { |
| 588 | const line = activeBuffer.getLine(row) |
| 589 | if (line) { |
| 590 | const text = line.translateToString(options?.trimWhitespace ?? true) |
| 591 | lines.push(text) |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | // Trim trailing empty lines if requested |
| 596 | if (options?.trimWhitespace) { |
| 597 | while (lines.length > 0 && lines[lines.length - 1] === "") { |
| 598 | lines.pop() |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | return lines.join("\n") |
| 603 | } |
| 604 | |
| 605 | private _serializeBufferByScrollback(buffer: IBuffer, scrollback?: number): string { |
| 606 | const maxRows = buffer.length |
nothing calls this directly
no test coverage detected