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