in Python/Product/PythonTools/PythonTools/Repl/PythonCommonInteractiveEvaluator.cs [815:918]
private static ConsoleColor? GetColorFromEscape(string text, ref int start) {
// http://en.wikipedia.org/wiki/ANSI_escape_code
// process any ansi color sequences...
ConsoleColor? color = null;
List<int> codes = new List<int>();
int? value = 0;
while (start < text.Length) {
if (text[start] >= '0' && text[start] <= '9') {
// continue parsing the integer...
if (value == null) {
value = 0;
}
value = 10 * value.Value + (text[start] - '0');
} else if (text[start] == ';') {
if (value != null) {
codes.Add(value.Value);
value = null;
} else {
// CSI ; - invalid or CSI ### ;;, both invalid
break;
}
} else if (text[start] == 'm') {
start += 1;
if (value != null) {
codes.Add(value.Value);
}
// parsed a valid code
if (codes.Count == 0) {
// reset
color = null;
} else {
for (int j = 0; j < codes.Count; j++) {
switch (codes[j]) {
case 0: color = ConsoleColor.White; break;
case 1: // bright/bold
color |= ConsoleColor.DarkGray;
break;
case 2: // faint
case 3: // italic
case 4: // single underline
break;
case 5: // blink slow
case 6: // blink fast
break;
case 7: // negative
case 8: // conceal
case 9: // crossed out
case 10: // primary font
case 11: // 11-19, n-th alternate font
break;
case 21: // bright/bold off
color &= ~ConsoleColor.DarkGray;
break;
case 22: // normal intensity
case 24: // underline off
break;
case 25: // blink off
break;
case 27: // image - postive
case 28: // reveal
case 29: // not crossed out
case 30: color = Change(color, ConsoleColor.Black); break;
case 31: color = Change(color, ConsoleColor.DarkRed); break;
case 32: color = Change(color, ConsoleColor.DarkGreen); break;
case 33: color = Change(color, ConsoleColor.DarkYellow); break;
case 34: color = Change(color, ConsoleColor.DarkBlue); break;
case 35: color = Change(color, ConsoleColor.DarkMagenta); break;
case 36: color = Change(color, ConsoleColor.DarkCyan); break;
case 37: color = Change(color, ConsoleColor.Gray); break;
case 38: // xterm 286 background color
case 39: // default text color
color = null;
break;
case 40: // background colors
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47: break;
case 90: color = ConsoleColor.DarkGray; break;
case 91: color = ConsoleColor.Red; break;
case 92: color = ConsoleColor.Green; break;
case 93: color = ConsoleColor.Yellow; break;
case 94: color = ConsoleColor.Blue; break;
case 95: color = ConsoleColor.Magenta; break;
case 96: color = ConsoleColor.Cyan; break;
case 97: color = ConsoleColor.White; break;
}
}
}
break;
} else {
// unknown char, invalid escape
break;
}
start += 1;
}
return color;
}