• 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();
                }
            }
    
    
        }
  • 相关阅读:
    自调用匿名函数和js的Module模式
    设置一天中不同时段的倒计时,计算时针和分针的夹角
    移动端web开发中对点透的处理,以及理解fastclick如何做到去除300ms延迟
    使用Fiddler改变线上js文件的引用路径
    Linux下常用设置文件和文件夹读写权限操作
    RESTful API
    mysql之load语句
    Django学习之点赞功能
    Django学习之网站图标
    python学习之pyenv
  • 原文地址:https://www.cnblogs.com/itelite/p/4142925.html
Copyright © 2020-2023  润新知