Example #1
0
        public void AddToCommandTrace(Command cmd)
        {
            CommandStringParser parser = new CommandStringParser("");

            bool valid = parser.SplitCommand(cmd.ToString(), out string cmdTag, out string argumentPart);

            int offset = m_txtCommandTrace.TextLength;

            string cmdString = cmd.ToString() + Environment.NewLine;

            // dump command
            m_txtCommandTrace.AppendText(cmdString);

            if (!valid)
            {
                return;
            }

            // color command name red
            m_txtCommandTrace.Select(offset, offset + cmdTag.Length);
            m_txtCommandTrace.SelectionColor = Color.Red;

            // let offset point right behind the command
            offset += cmdTag.Length;

            // color argument names blue and argument values black
            if (!string.IsNullOrEmpty(argumentPart))
            {
                foreach (NameValuePair nameValuePair in parser.GetNameValuePairs(argumentPart))
                {
                    m_txtCommandTrace.Select(offset + nameValuePair.NameFrom, offset + nameValuePair.NameTo);
                    m_txtCommandTrace.SelectionColor = Color.Blue;

                    m_txtCommandTrace.Select(offset + nameValuePair.ValueFrom, offset + nameValuePair.ValueTo);
                    m_txtCommandTrace.SelectionColor = Color.Black;
                }
            }
            m_txtCommandTrace.DeselectAll();
            m_txtCommandTrace.ScrollToCaret();
            return;

            /*
             * int index = 0;
             * while (index < cmdString.Length && cmdString[index] != ' ' && cmdString[index] != ';')
             * {
             *  index++;
             * }
             * this.m_txtCommandTrace.Select(offset, offset + index);
             * this.m_txtCommandTrace.SelectionColor = Color.Red;
             *
             * this.m_txtCommandTrace.Select(offset + index, this.m_txtCommandTrace.TextLength);
             * this.m_txtCommandTrace.SelectionColor = Color.Blue;
             * this.m_txtCommandTrace.DeselectAll();
             * this.m_txtCommandTrace.ScrollToCaret();*/
        }
        public override void CommandTrace(Command cmd)
        {
            string output = cmd.ToString();

            if (!CommandExecuter.Instance.ColorOutput)
            {
                Console.WriteLine(output);
            }
            else
            {
                int index = 0;
                // print command name in red
                Console.ForegroundColor = ConsoleColor.Red;
                while (index < output.Length && output[index] != ' ' && output[index] != ';')
                {
                    Console.Write(output[index]);
                    index++;
                }
                Console.ResetColor();
                // print blank
                Console.Write(output[index++]);

                string argumentPart = cmd.ToString();
                argumentPart = argumentPart.Substring(index, argumentPart.Length - index);

                CommandStringParser parser = new CommandStringParser("");

                foreach (NameValuePair nameValuePair in parser.GetNameValuePairs(argumentPart))
                {
                    // next command starts
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    for (int i = 0; i < nameValuePair.Name.Length; i++)
                    {
                        Console.Write(output[index++]);
                    }
                    Console.ResetColor();
                    Console.Write(output[index++]);
                    for (int i = 0; i < nameValuePair.Value.Length; i++)
                    {
                        Console.Write(output[index++]);
                    }
                    if (index < output.Length)
                    {
                        Console.Write(output[index++]);
                    }
                }
                Console.WriteLine("");
                // restore console color for next output (after end of program)
                Console.ResetColor();
                return;
            }
        }