Skip to content

Commit

Permalink
Invert inverse ANSI colors
Browse files Browse the repository at this point in the history
The "inverse" escape sequence was implemented in jupyter#2186, but not by
actually inverting foreground and background.
  • Loading branch information
mgeier committed Nov 1, 2017
1 parent 837e0b8 commit 18186fc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 47 deletions.
98 changes: 52 additions & 46 deletions notebook/static/base/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,57 @@ define([
"ansi-white-intense",
];

function _pushColoredChunk(chunk, fg, bg, bold, underline, inverse, out) {
if (chunk) {
var classes = [];
var styles = [];

if (bold && typeof fg === "number" && 0 <= fg && fg < 8) {
fg += 8; // Bold text uses "intense" colors
}
if (inverse) {
if (fg.length === 0) { fg = [0, 0, 0]; }
if (bg.length === 0) { bg = [255, 255, 255]; }
[fg, bg] = [bg, fg];
}

if (typeof fg === "number") {
classes.push(_ANSI_COLORS[fg] + "-fg");
} else if (fg.length) {
styles.push("color: rgb(" + fg + ")");
}

if (typeof bg === "number") {
classes.push(_ANSI_COLORS[bg] + "-bg");
} else if (bg.length) {
styles.push("background-color: rgb(" + bg + ")");
}

if (bold) {
classes.push("ansi-bold");
}

if (underline) {
classes.push("ansi-underline");
}

if (classes.length || styles.length) {
out.push("<span");
if (classes.length) {
out.push(' class="' + classes.join(" ") + '"');
}
if (styles.length) {
out.push(' style="' + styles.join("; ") + '"');
}
out.push(">");
out.push(chunk);
out.push("</span>");
} else {
out.push(chunk);
}
}
}

function _getExtendedColors(numbers) {
var r, g, b;
var n = numbers.shift();
Expand Down Expand Up @@ -309,52 +360,7 @@ define([
// Ignored: Not a color code
}
var chunk = str.substring(start, match.index);
if (chunk) {
if (bold && typeof fg === "number" && 0 <= fg && fg < 8) {
fg += 8; // Bold text uses "intense" colors
}
var classes = [];
var styles = [];

if (typeof fg === "number") {
classes.push(_ANSI_COLORS[fg] + "-fg");
} else if (fg.length) {
styles.push("color: rgb(" + fg + ")");
}

if (typeof bg === "number") {
classes.push(_ANSI_COLORS[bg] + "-bg");
} else if (bg.length) {
styles.push("background-color: rgb(" + bg + ")");
}

if (bold) {
classes.push("ansi-bold");
}

if (underline) {
classes.push("ansi-underline");
}

if (inverse) {
classes.push("ansi-inverse");
}

if (classes.length || styles.length) {
out.push("<span");
if (classes.length) {
out.push(' class="' + classes.join(" ") + '"');
}
if (styles.length) {
out.push(' style="' + styles.join("; ") + '"');
}
out.push(">");
out.push(chunk);
out.push("</span>");
} else {
out.push(chunk);
}
}
_pushColoredChunk(chunk, fg, bg, bold, underline, inverse, out);
start = ansi_re.lastIndex;

while (numbers.length) {
Expand Down
2 changes: 1 addition & 1 deletion notebook/static/notebook/less/ansicolors.less
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

.ansi-bold { font-weight: bold; }
.ansi-underline { text-decoration: underline; }
.ansi-inverse { outline: 0.5px dotted; }

/* The following styles are deprecated an will be removed in a future version */

.ansibold {font-weight: bold;}
.ansi-inverse { outline: 0.5px dotted; }

/* use dark versions for foreground, to improve visibility */
.ansiblack {color: black;}
Expand Down

0 comments on commit 18186fc

Please sign in to comment.