zoukankan      html  css  js  c++  java
  • ConsoleHelper 类

    //Writes colored text to the console and allows to clear previously written lines
    //as long a not line break is present
    
    //Sample - screenshot at http://img299.imageshack.us/img299/3931/consolex.png
    
    C.InfoLine("Non-colored text...");
                
    C.Error("Outch, an error.");
    Thread.CurrentThread.Join(1000);
    C.ClearLine();
    C.Warn("Ok, only a warning.");
    Thread.CurrentThread.Join(1000);
    C.ClearLine();
    C.SuccessLine("OK.");
    
    C.InfoColor = ConsoleColor.Blue;
    C.InfoLine("I'm feeling blue");
    
    
    /* ********************************************************** */
    
        /// <summary>
        /// Console helper class.
        /// </summary>
        public static class C
        {
            /// <summary>
            /// The color that is used to print out errors to the console.
            /// </summary>
            public static ConsoleColor ErrorColor = ConsoleColor.Red;
    
            /// <summary>
            /// The color that is used to print out warnings to the console.
            /// </summary>
            public static ConsoleColor WarningColor = ConsoleColor.Yellow;
    
            /// <summary>
            /// The color that is used to print out infos to the console.
            /// </summary>
            public static ConsoleColor SuccessColor = ConsoleColor.Green;
    
            /// <summary>
            /// The color that is used to print out infos to the console.
            /// If set to null, the current console color is used.
            /// </summary>
            public static ConsoleColor? InfoColor;
    
    
    
            public static void ErrorLine(string msg, params object[] args)
            {
                WriteLine(ErrorColor, msg, args);
            }
    
    
            public static void Error(string msg, params object[] args)
            {
                Write(ErrorColor, msg, args);
            }
    
    
            public static void WarnLine(string msg, params object[] args)
            {
                WriteLine(WarningColor, msg, args);
            }
    
    
            public static void Warn(string msg, params object[] args)
            {
                Write(WarningColor, msg, args);
            }
    
            public static void InfoLine(string msg, params object[] args)
            {
                WriteLine(InfoColor ?? Console.ForegroundColor, msg, args);
            }
    
            public static void Info(string msg, params object[] args)
            {
                Write(InfoColor ?? Console.ForegroundColor, msg, args);
            }
    
            public static void SuccessLine(string msg, params object[] args)
            {
                WriteLine(SuccessColor, msg, args);
            }
    
    
            public static void Success(string msg, params object[] args)
            {
                Write(SuccessColor, msg, args);
            }
    
    
            /// <summary>
            /// Clears the current line.
            /// </summary>
            public static void ClearLine()
            {
                var position = Console.CursorLeft;
                
                //overwrite with white space (backspace doesn't really clear the buffer,
                //would need a hacky combination of  and single whitespace)
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.Write("".PadRight(position));
                Console.SetCursorPosition(0, Console.CursorTop);
            }
    
    
            public static void Write(string msg, params object[] args)
            {
                Console.Write(msg, args);
            }
    
    
            public static void WriteLine(ConsoleColor color, string msg, params object[] args)
            {
                Write(color, msg, args);
                Console.Out.WriteLine();
            }
    
    
            public static void Write(ConsoleColor color, string msg, params object[] args)
            {
                try
                {
                    Console.ForegroundColor = color;
                    Console.Write(msg, args);
                }
                finally
                {
                    Console.ResetColor();
                }
            }
    
    
        }
  • 相关阅读:
    CAST()类型转换函数
    CLR LOH的危险
    保持积极的态度,态度决定一切!
    as 操作符和强行转换的区别
    查内存覆盖从以前的帖子里总结的
    CLR中的范型为什么不支持很多操作符?
    如何做一个好的Team Leader?
    Dispose Pattern总结
    慎用Reflection
    CLR Enum类型内幕
  • 原文地址:https://www.cnblogs.com/itelite/p/4142925.html
Copyright © 2011-2022 走看看