in libraries/node-core-library/src/Terminal/Terminal.ts [190:387]
private _serializeFormattableTextSegments(
segments: (string | IColorableSequence)[],
withColor: boolean
): string[] {
const lines: string[] = [];
let segmentsToJoin: string[] = [];
let lastSegmentWasEol: boolean = false;
for (let i: number = 0; i < segments.length; i++) {
const segment: IColorableSequence = Colors._normalizeStringOrColorableSequence(segments[i]);
lastSegmentWasEol = !!segment.isEol;
if (lastSegmentWasEol) {
lines.push(segmentsToJoin.join(''));
segmentsToJoin = [];
} else {
if (withColor) {
const startColorCodes: number[] = [];
const endColorCodes: number[] = [];
switch (segment.foregroundColor) {
case ColorValue.Black: {
startColorCodes.push(ConsoleColorCodes.BlackForeground);
endColorCodes.push(ConsoleColorCodes.DefaultForeground);
break;
}
case ColorValue.Red: {
startColorCodes.push(ConsoleColorCodes.RedForeground);
endColorCodes.push(ConsoleColorCodes.DefaultForeground);
break;
}
case ColorValue.Green: {
startColorCodes.push(ConsoleColorCodes.GreenForeground);
endColorCodes.push(ConsoleColorCodes.DefaultForeground);
break;
}
case ColorValue.Yellow: {
startColorCodes.push(ConsoleColorCodes.YellowForeground);
endColorCodes.push(ConsoleColorCodes.DefaultForeground);
break;
}
case ColorValue.Blue: {
startColorCodes.push(ConsoleColorCodes.BlueForeground);
endColorCodes.push(ConsoleColorCodes.DefaultForeground);
break;
}
case ColorValue.Magenta: {
startColorCodes.push(ConsoleColorCodes.MagentaForeground);
endColorCodes.push(ConsoleColorCodes.DefaultForeground);
break;
}
case ColorValue.Cyan: {
startColorCodes.push(ConsoleColorCodes.CyanForeground);
endColorCodes.push(ConsoleColorCodes.DefaultForeground);
break;
}
case ColorValue.White: {
startColorCodes.push(ConsoleColorCodes.WhiteForeground);
endColorCodes.push(ConsoleColorCodes.DefaultForeground);
break;
}
case ColorValue.Gray: {
startColorCodes.push(ConsoleColorCodes.GrayForeground);
endColorCodes.push(ConsoleColorCodes.DefaultForeground);
break;
}
}
switch (segment.backgroundColor) {
case ColorValue.Black: {
startColorCodes.push(ConsoleColorCodes.BlackBackground);
endColorCodes.push(ConsoleColorCodes.DefaultBackground);
break;
}
case ColorValue.Red: {
startColorCodes.push(ConsoleColorCodes.RedBackground);
endColorCodes.push(ConsoleColorCodes.DefaultBackground);
break;
}
case ColorValue.Green: {
startColorCodes.push(ConsoleColorCodes.GreenBackground);
endColorCodes.push(ConsoleColorCodes.DefaultBackground);
break;
}
case ColorValue.Yellow: {
startColorCodes.push(ConsoleColorCodes.YellowBackground);
endColorCodes.push(ConsoleColorCodes.DefaultBackground);
break;
}
case ColorValue.Blue: {
startColorCodes.push(ConsoleColorCodes.BlueBackground);
endColorCodes.push(ConsoleColorCodes.DefaultBackground);
break;
}
case ColorValue.Magenta: {
startColorCodes.push(ConsoleColorCodes.MagentaBackground);
endColorCodes.push(ConsoleColorCodes.DefaultBackground);
break;
}
case ColorValue.Cyan: {
startColorCodes.push(ConsoleColorCodes.CyanBackground);
endColorCodes.push(ConsoleColorCodes.DefaultBackground);
break;
}
case ColorValue.White: {
startColorCodes.push(ConsoleColorCodes.WhiteBackground);
endColorCodes.push(ConsoleColorCodes.DefaultBackground);
break;
}
case ColorValue.Gray: {
startColorCodes.push(ConsoleColorCodes.GrayBackground);
endColorCodes.push(49);
break;
}
}
if (segment.textAttributes) {
for (const textAttribute of segment.textAttributes) {
switch (textAttribute) {
case TextAttribute.Bold: {
startColorCodes.push(ConsoleColorCodes.Bold);
endColorCodes.push(ConsoleColorCodes.NormalColorOrIntensity);
break;
}
case TextAttribute.Dim: {
startColorCodes.push(ConsoleColorCodes.Dim);
endColorCodes.push(ConsoleColorCodes.NormalColorOrIntensity);
break;
}
case TextAttribute.Underline: {
startColorCodes.push(ConsoleColorCodes.Underline);
endColorCodes.push(ConsoleColorCodes.UnderlineOff);
break;
}
case TextAttribute.Blink: {
startColorCodes.push(ConsoleColorCodes.Blink);
endColorCodes.push(ConsoleColorCodes.BlinkOff);
break;
}
case TextAttribute.InvertColor: {
startColorCodes.push(ConsoleColorCodes.InvertColor);
endColorCodes.push(ConsoleColorCodes.InvertColorOff);
break;
}
case TextAttribute.Hidden: {
startColorCodes.push(ConsoleColorCodes.Hidden);
endColorCodes.push(ConsoleColorCodes.HiddenOff);
break;
}
}
}
}
for (let j: number = 0; j < startColorCodes.length; j++) {
const code: number = startColorCodes[j];
segmentsToJoin.push(...['\u001b[', code.toString(), 'm']);
}
segmentsToJoin.push(segment.text);
for (let j: number = endColorCodes.length - 1; j >= 0; j--) {
const code: number = endColorCodes[j];
segmentsToJoin.push(...['\u001b[', code.toString(), 'm']);
}
} else {
segmentsToJoin.push(segment.text);
}
}
}
if (segmentsToJoin.length > 0) {
lines.push(segmentsToJoin.join(''));
}
if (lastSegmentWasEol) {
lines.push('');
}
return lines;
}