From 1a3846161a6fc58f45cb5b13aa93bc73d6112341 Mon Sep 17 00:00:00 2001 From: Paris Date: Mon, 30 May 2016 17:11:31 +0300 Subject: [PATCH] Proper rendering of inverse data in default colors Fix #57 --- src/xterm.js | 59 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/src/xterm.js b/src/xterm.js index 6bec2030..608f88b9 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -968,6 +968,17 @@ }; + /** + * Flags used to render terminal text properly + */ + Terminal.flags = { + BOLD: 0b00001, + UNDERLINE: 0b00010, + BLINK: 0b00100, + INVERSE: 0b01000, + INVISIBLE: 0b10000 + } + /* * Rendering Engine * @@ -1035,8 +1046,7 @@ fg = (data >> 9) & 0x1ff; flags = data >> 18; - // bold - if (flags & 1) { + if (flags & Terminal.flags.BOLD) { if (!Terminal.brokenBold) { out += ' xterm-bold '; } @@ -1044,35 +1054,50 @@ if (fg < 8) fg += 8; } - // underline - if (flags & 2) { + if (flags & Terminal.flags.UNDERLINE) { out += ' xterm-underline '; } - // blink - if (flags & 4) { + if (flags & Terminal.flags.BLINK) { out += ' xterm-blink '; } - // inverse - if (flags & 8) { - bg = (data >> 9) & 0x1ff; - fg = data & 0x1ff; - // Should inverse just be before the - // above boldColors effect instead? - if ((flags & 1) && fg < 8) fg += 8; + /** + * If inverse flag is on, then swap the foreground and background variables. + */ + if (flags & Terminal.flags.INVERSE) { + /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */ + bg = [fg, fg = bg][0]; + // Should inverse just be before the + // above boldColors effect instead? + if ((flags & 1) && fg < 8) fg += 8; } - // invisible - if (flags & 16) { + if (flags & Terminal.flags.INVISIBLE) { out += ' xterm-hidden '; } - if (bg !== 256) { + /** + * Weird situation: Invert flag used black foreground and white background results + * in invalid background color, positioned at the 256 index of the 256 terminal + * color map. Pin the colors manually in such a case. + * + * Source: https://github.com/sourcelair/xterm.js/issues/57 + */ + if (flags & Terminal.flags.INVERSE) { + if (bg == 257) { + bg = 15; + } + if (fg == 256) { + fg = 0; + } + } + + if (bg < 256) { out += ' xterm-bg-color-' + bg + ' '; } - if (fg !== 257) { + if (fg < 256) { out += ' xterm-color-' + fg + ' '; }