• 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();
                }
            }
    
    
        }
  • 相关阅读:
    Java中如何设置表格处于不可编辑状态
    Android界面实现不成功(无报错)
    Eclipse网页报错
    【蓝桥杯】基础练习 十六进制转八进制 Java语言
    分析算法的复杂度
    Android Studio安装错误及解决办法
    Android程序报错以及解决办法
    Genymotion安装使用(配合Android Studio)
    Eclipse导入本地项目并运行
    蓝桥杯Java——安装软件Eclipse以及JDK
  • 原文地址:https://www.cnblogs.com/itelite/p/4142925.html
Copyright © 2020-2023  润新知