Exemplo n.º 1
0
 public static void WriteLine(string format, params object[] args)
 {
     lock (writeLock) {
         ConsoleUtils.WriteSub(format, args);
         Console.WriteLine();
     }
 }
Exemplo n.º 2
0
 public static void Write(string text)
 {
     lock (writeLock)
         ConsoleUtils.WriteSub(text, Console.ForegroundColor, Console.BackgroundColor);
 }
Exemplo n.º 3
0
        private static void WriteSub(string format, params object[] args)
        {
            int          i = 0; int open = -1; int index = -1; int start = 0;
            ConsoleColor originalForeground = Console.ForegroundColor;
            ConsoleColor originalBackground = Console.BackgroundColor;

            while (i < format.Length)
            {
                char c = format[i];

                if (c == '}')
                {
                    if (i != format.Length - 1 && format[i + 1] == '}')
                    {
                        if (index == -1)
                        {
                            if (i != start)
                            {
                                ConsoleUtils.WriteSub(format.Substring(start, i - start), originalForeground, originalBackground);
                            }
                            Console.Write("}");
                            start = i + 2;
                        }
                        ++i;
                    }
                    else if (index < 0)
                    {
                        throw new FormatException();
                    }
                    else
                    {
                        if (open == -1)
                        {
                            Console.Write((args[index] ?? "").ToString());
                        }
                        else
                        {
                            Console.Write("{0" + format.Substring(open, i - open) + "}", args[index]);
                        }
                        open  = -1;
                        index = -1;
                        start = i + 1;
                    }
                }
                else if (open == -1)
                {
                    if (index == -2)
                    {
                        if (c < '0' || c > '9')
                        {
                            throw new FormatException();
                        }
                        index = (int)(c - '0');
                    }
                    else if (index != -1)
                    {
                        if (c != ' ')
                        {
                            if (c == ',' || c == ':')
                            {
                                open = i;
                            }
                            else
                            {
                                if (c < '0' || c > '9')
                                {
                                    throw new FormatException();
                                }
                                index = index * 10 + (int)(c - '0');
                            }
                        }
                    }
                    else if (c == '{')
                    {
                        if (i != format.Length - 1 && format[i + 1] == '{')
                        {
                            if (index == -1)
                            {
                                if (i != start)
                                {
                                    ConsoleUtils.WriteSub(format.Substring(start, i - start), originalForeground, originalBackground);
                                }
                                Console.Write("{");
                                start = i + 2;
                            }
                            ++i;
                        }
                        else if (open == -1)
                        {
                            if (i != start)
                            {
                                ConsoleUtils.WriteSub(format.Substring(start, i - start), originalForeground, originalBackground);
                            }
                            index = -2;
                        }
                        else
                        {
                            throw new FormatException();
                        }
                    }
                }
                ++i;
            }
            if (index != -1)
            {
                throw new FormatException();
            }
            if (i != start)
            {
                ConsoleUtils.WriteSub(format.Substring(start, i - start), originalForeground, originalBackground);
            }
        }