Example #1
0
        private string ParsedOutput(string data)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                return(string.Empty);
            }

            var r   = @"\e\[(\d{1,2});(\d{1,2})(?:;(\d{0,2}))?m";
            var end = @"\e\[0m";
            //\e[2J\e[H
            // \e7[r\e[999;999H\e[6n
            var ignored = @"(\e\[H|\e7|\e\[r|\e\[\d{1,};?\d{0,}H|\e\[6n|\e\d{1,}\|)";
            var clear   = @"\e\[2J";

            var sb = new StringBuilder(data);

            sb.Insert(0, (char)0x1B);
            var withEscape = sb.ToString();

            if (withEscape.IsMatch(ignored))
            {
                data = withEscape.REReplace(ignored, "");
            }
            else if (withEscape.IsMatch(clear))
            {
                Console.ForeColor          = Console.DefaultForeColor;
                Console.SelectionBackColor = Console.BackColor;
                Console.ClearOutput();
                return(string.Empty);
            }
            else if (withEscape.IsMatch(end))
            {
                // reset forecolor to default
                Console.ForeColor          = Console.DefaultForeColor;
                Console.SelectionBackColor = Console.BackColor;
                data = withEscape.REReplace(end, "");
            }
            else if (withEscape.IsMatch(r))
            {
                var m = withEscape.Match(r, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);

                var code = m.Groups[2].Value;
                LinuxConsole.ConsoleColor          fcolor = (LinuxConsole.ConsoleColor) int.Parse(m.Groups[2].Value);
                LinuxConsole.ConsoleColorAttribute attrib = (LinuxConsole.ConsoleColorAttribute) int.Parse(m.Groups[1].Value);

                var clr = LinuxConsole.ToColor(fcolor, attrib);
                Console.ForeColor = clr;

                var bcode = m.Groups[3].Value.Or("0");
                if ((LinuxConsole.ConsoleColor) int.Parse(bcode) != LinuxConsole.ConsoleColor.DEFAULT)
                {
                    LinuxConsole.ConsoleColor bcolor = (LinuxConsole.ConsoleColor) int.Parse(m.Groups[2].Value);
                    var bclr = LinuxConsole.ToColor(bcolor, attrib);
                    Console.SelectionBackColor = bclr;
                }

                data = withEscape.REReplace(r, "");
            }

            return(data);
        }
Example #2
0
        /// <summary>
        /// Processes the string.
        /// </summary>
        /// <param name="value">The value.</param>
        private void ProcessString(string value)
        {
            Regex        logcat = new Regex(@"^(?:(?:\#\s+)?(logcat|[WIDE])/)(.*)$", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
            Match        m1     = logcat.Match(value);
            string       text   = string.Empty;
            ConsoleColor clr    = Logger.Color;

            if (m1.Success)
            {
                string level = m1.Groups[1].Value;
                text = m1.Groups[2].Value;
                switch (level)
                {
                case "logcat":
                    return;

                case "W": //warn
                    clr = ConsoleColor.Yellow;
                    break;

                case "I": //info
                    clr = ConsoleColor.Cyan;
                    break;

                case "D": //debug
                    clr = ConsoleColor.Green;
                    break;

                case "E": //error
                    clr = ConsoleColor.Red;
                    break;

                default:
                    clr = Logger.Color;
                    break;
                }
                if (TextBox != null && !TextBox.IsDisposed)
                {
                    if (TextBox.InvokeRequired)
                    {
                        TextBox.Invoke(new SetTextColorDelegate(SetConsoleColor), clr);
                        TextBox.Invoke(new AppendDelegate(TextBox.AppendText), text);
                    }
                    else
                    {
                        SetConsoleColor(clr);
                        TextBox.AppendText(text);
                    }
                }
            }
            else
            {
                Regex colored = new Regex(@"(?:\e\[(\d{1,2};\d{1,2})m)?([^(\e)]*)(?:\e\[0m)?", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
                Match m       = colored.Match(value);
                while (m.Success)
                {
                    string colorCode = m.Groups[1].Value;
                    text = m.Groups[2].Value;
                    LinuxConsole.ConsoleColor          color  = LinuxConsole.GetConsoleColor(colorCode);
                    LinuxConsole.ConsoleColorAttribute attrib = LinuxConsole.GetConsoleColorAttribute(colorCode);
                    if (color != LinuxConsole.ConsoleColor.DEFAULT)
                    {
                        clr = LinuxConsole.ToWindowsConsoleColor(color, attrib);
                    }
                    if (TextBox != null && !TextBox.IsDisposed)
                    {
                        if (TextBox.InvokeRequired)
                        {
                            TextBox.Invoke(new SetTextColorDelegate(SetConsoleColor), clr);
                            TextBox.Invoke(new AppendDelegate(TextBox.AppendText), text);
                        }
                        else
                        {
                            SetConsoleColor(clr);
                            TextBox.AppendText(text);
                        }
                    }
                    m = m.NextMatch( );
                }
            }
        }