| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | export function processTerminalLines(segments: Array<string>): Array<string> { |
| 9 | const result = []; |
| 10 | // Normalize output buffering to be newline based. |
| 11 | // This avoids weirdness with output buffering at \r instead of \n, and makes this logic simpler. |
| 12 | for (const line of segments.join('').split('\n')) { |
| 13 | const cr = line.lastIndexOf( |
| 14 | '\r', |
| 15 | line.length - 2, // ignore \r at the end, that should be handled as \n |
| 16 | ); |
| 17 | if (cr !== -1) { |
| 18 | // if there's one or more carriage returns, take the output after the last one as this line. |
| 19 | result.push(line.slice(cr + 1).trimEnd()); |
| 20 | continue; |
| 21 | } |
| 22 | |
| 23 | result.push(line.trimEnd()); |
| 24 | } |
| 25 | |
| 26 | while (result.length > 0 && result.at(-1)?.trim() === '') { |
| 27 | result.pop(); |
| 28 | } |
| 29 | |
| 30 | return result; |
| 31 | } |
| 32 | |