diff --git a/addons/xterm-addon-serialize/src/SerializeAddon.api.ts b/addons/xterm-addon-serialize/src/SerializeAddon.api.ts index 79343fbe..6fbf9af2 100644 --- a/addons/xterm-addon-serialize/src/SerializeAddon.api.ts +++ b/addons/xterm-addon-serialize/src/SerializeAddon.api.ts @@ -131,12 +131,14 @@ describe('SerializeAddon', () => { it('serialize all rows of content with color16', async function (): Promise { this.timeout(20000); - const rows = 16; const cols = 10; const color16 = [ 30, 31, 32, 33, 34, 35, 36, 37, // Set foreground color - 40, 41, 42, 43, 44, 45, 46, 47 // Set background color + 90, 91, 92, 93, 94, 95, 96, 97, + 40, 41, 42, 43, 44, 45, 46, 47, // Set background color + 100, 101, 103, 104, 105, 106, 107 ]; + const rows = color16.length; const lines = newArray( (index: number) => digitsString(cols, index, `\x1b[${color16[index % color16.length]}m`), rows diff --git a/addons/xterm-addon-serialize/src/SerializeAddon.ts b/addons/xterm-addon-serialize/src/SerializeAddon.ts index 564bbddc..6143d94f 100644 --- a/addons/xterm-addon-serialize/src/SerializeAddon.ts +++ b/addons/xterm-addon-serialize/src/SerializeAddon.ts @@ -77,6 +77,24 @@ const FG_FM_MASK = FgFlags.FM_MASK; const BG_FM_MASK = BgFlags.FM_MASK; const COLOR_MASK = Attributes.CM_MASK | Attributes.RGB_MASK; +function fgColor256to16(c: number): number { + if (0 <= c && c <= 7) { + return 30 + c; + } else if (8 <= c && c <= 15) { + return 82 + c; + } + return -1; +} + +function bgColor256to16(c: number): number { + if (0 <= c && c <= 7) { + return 40 + c; + } else if (8 <= c && c <= 15) { + return 92 + c; + } + return -1; +} + class StringSerializeHandler extends BaseSerializeHandler { private _rowIndex: number = 0; private _allRows: string[] = new Array(); @@ -130,7 +148,7 @@ class StringSerializeHandler extends BaseSerializeHandler { sgrSeq.push('39'); } else if (cell.isFgPalette()) { switch (cell.getFgColorMode()) { - case Attributes.CM_P16: sgrSeq.push(`${30 + fgColor}`); break; + case Attributes.CM_P16: sgrSeq.push(fgColor256to16(fgColor).toString()); break; case Attributes.CM_P256: sgrSeq.push(`38;5;${fgColor}`); break; } } else if (cell.isFgRGB()) { @@ -165,7 +183,7 @@ class StringSerializeHandler extends BaseSerializeHandler { sgrSeq.push('49'); } else if (cell.isBgPalette()) { switch (cell.getBgColorMode()) { - case Attributes.CM_P16: sgrSeq.push(`${40 + bgColor}`); break; + case Attributes.CM_P16: sgrSeq.push(bgColor256to16(bgColor).toString()); break; case Attributes.CM_P256: sgrSeq.push(`48;5;${bgColor}`); break; } } else if (cell.isFgRGB()) {