public MetaData(ColorMessage.MetaData cmMeta) { m_index = cmMeta.m_index; m_fgColor = cmMeta.m_style.Fg.ToSystemColor(); if (m_fgColor.IsEmpty) { m_fgColor = System.Drawing.Color.FromArgb(160, 160, 160); } m_bgColor = cmMeta.m_style.Bg.ToSystemColor(); if (m_bgColor.IsEmpty) { m_bgColor = System.Drawing.Color.Black; } if ((cmMeta.m_style.Flags & TextStyleFlags.HighIntensity) != 0) { bool bold; int c; c = Ansi.ColorToAnsiColor8(Chiroptera.Base.Color.FromSystemColor(m_fgColor), out bold); if (bold == false) { m_fgColor = Ansi.AnsiColor8ToColor(c, true).ToSystemColor(); } if (!cmMeta.m_style.Bg.IsEmpty) { c = Ansi.ColorToAnsiColor8(Chiroptera.Base.Color.FromSystemColor(m_bgColor), out bold); if (bold == false) { m_bgColor = Ansi.AnsiColor8ToColor(c, true).ToSystemColor(); } } } if ((cmMeta.m_style.Flags & TextStyleFlags.Inverse) != 0) { System.Drawing.Color tmp = m_fgColor; m_fgColor = m_bgColor; m_bgColor = tmp; } }
public static StringBuilder ParseBatControls(string text, out List <ColorMessage.MetaData> metaData) { StringBuilder stringBuilder = new StringBuilder(text.Length); metaData = new List <ColorMessage.MetaData>(); Color currentFgColor = Color.Empty; Color currentBgColor = Color.Empty; int pos = 0; int oldPos = 0; while (pos < text.Length) { if (text[pos] == '\t') { stringBuilder.Append(' ', 4); pos++; continue; } if (text[pos] != ESC) { stringBuilder.Append(text[pos]); pos++; continue; } oldPos = pos; pos++; // skip ESC if (pos >= text.Length) { stringBuilder.Append(text.Substring(oldPos, pos - oldPos)); continue; } if (text[pos] == '<') { pos++; // skip < string codeStr = text.Substring(pos, 2); pos += 2; int code = 0; try { code = Int32.Parse(codeStr); } catch (Exception e) { BatConsole.WriteLine(e.ToString()); } switch (code) { case 20: { string colorStr = text.Substring(pos, 6); pos += 6; int colorNum = Int32.Parse(colorStr, System.Globalization.NumberStyles.HexNumber); colorNum |= unchecked ((int)0xff000000); currentFgColor = Color.FromArgb(colorNum); ColorMessage.MetaData md = new ColorMessage.MetaData(stringBuilder.Length, currentFgColor, currentBgColor); metaData.Add(md); pos += 2; // skip ESC| break; } case 21: { string colorStr = text.Substring(pos, 6); pos += 6; int colorNum = Int32.Parse(colorStr, System.Globalization.NumberStyles.HexNumber); colorNum |= unchecked ((int)0xff000000); currentBgColor = Color.FromArgb(colorNum); ColorMessage.MetaData md = new ColorMessage.MetaData(stringBuilder.Length, currentFgColor, currentBgColor); metaData.Add(md); pos += 2; // skip ESC| break; } default: BatConsole.WriteLine("Unknown control code <{0}", code); break; } } else if (text[pos] == '>') { pos++; // skip > string codeStr = text.Substring(pos, 2); pos += 2; int code = Int32.Parse(codeStr); switch (code) { case 20: { currentFgColor = Color.Empty; ColorMessage.MetaData md = new ColorMessage.MetaData(stringBuilder.Length, currentFgColor, currentBgColor); metaData.Add(md); break; } case 21: { currentBgColor = Color.Empty; ColorMessage.MetaData md = new ColorMessage.MetaData(stringBuilder.Length, currentFgColor, currentBgColor); metaData.Add(md); break; } default: BatConsole.WriteLine("Unknown control code >{0}", code); break; } } else { BatConsole.WriteLine("Unknown esc-control command: {0}", text[pos]); } } return(stringBuilder); }