addons/isl/src/terminalOutput.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318export function processTerminalLines(segments: Array<string>): Array<string> {
b69ab319 const result = [];
b69ab3110 // Normalize output buffering to be newline based.
b69ab3111 // This avoids weirdness with output buffering at \r instead of \n, and makes this logic simpler.
b69ab3112 for (const line of segments.join('').split('\n')) {
b69ab3113 const cr = line.lastIndexOf(
b69ab3114 '\r',
b69ab3115 line.length - 2, // ignore \r at the end, that should be handled as \n
b69ab3116 );
b69ab3117 if (cr !== -1) {
b69ab3118 // if there's one or more carriage returns, take the output after the last one as this line.
b69ab3119 result.push(line.slice(cr + 1).trimEnd());
b69ab3120 continue;
b69ab3121 }
b69ab3122
b69ab3123 result.push(line.trimEnd());
b69ab3124 }
b69ab3125
b69ab3126 while (result.length > 0 && result.at(-1)?.trim() === '') {
b69ab3127 result.pop();
b69ab3128 }
b69ab3129
b69ab3130 return result;
b69ab3131}