private static InteractiveWindowColor? GetColorFromEscape()

in Nodejs/Product/Nodejs/Repl/NodejsReplEvaluator.cs [364:486]


        private static InteractiveWindowColor? GetColorFromEscape(string text, ref int start)
        {
            // http://en.wikipedia.org/wiki/ANSI_escape_code
            // process any ansi color sequences...
            InteractiveWindowColor? color = null;
            var 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 (var j = 0; j < codes.Count; j++)
                        {
                            switch (codes[j])
                            {
                                case 0: color = InteractiveWindowColor.White; break;
                                case 1: // bright/bold
                                    color |= InteractiveWindowColor.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 &= ~InteractiveWindowColor.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, InteractiveWindowColor.Black); break;
                                case 31: color = Change(color, InteractiveWindowColor.DarkRed); break;
                                case 32: color = Change(color, InteractiveWindowColor.DarkGreen); break;
                                case 33: color = Change(color, InteractiveWindowColor.DarkYellow); break;
                                case 34: color = Change(color, InteractiveWindowColor.DarkBlue); break;
                                case 35: color = Change(color, InteractiveWindowColor.DarkMagenta); break;
                                case 36: color = Change(color, InteractiveWindowColor.DarkCyan); break;
                                case 37: color = Change(color, InteractiveWindowColor.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 = InteractiveWindowColor.DarkGray; break;
                                case 91: color = InteractiveWindowColor.Red; break;
                                case 92: color = InteractiveWindowColor.Green; break;
                                case 93: color = InteractiveWindowColor.Yellow; break;
                                case 94: color = InteractiveWindowColor.Blue; break;
                                case 95: color = InteractiveWindowColor.Magenta; break;
                                case 96: color = InteractiveWindowColor.Cyan; break;
                                case 97: color = InteractiveWindowColor.White; break;
                            }
                        }
                    }
                    break;
                }
                else
                {
                    // unknown char, invalid escape
                    break;
                }
                start += 1;
            }
            return color;
        }