/// <summary>Writes the specified string value, followed by the current line terminator, to the console.</summary> /// <param name="message">The message to write.</param> /// <param name="foreColor">The foreground colour to write.</param> /// <param name="backgroundColor">The background colour to write.</param> public static void WriteLine(string message, ConsoleColor foreColor, ConsoleColor backgroundColor = ConsoleColor.Black) { var resetConsoleColourAspect = new ResetCursorColourAspect(); resetConsoleColourAspect.OnEntry(); try { Write(message + "\n", foreColor, backgroundColor); } finally { resetConsoleColourAspect.OnExit(); } }
/// <summary> /// Writes the specified string value to the console with the <paramref name="underlineCharacter" /> written /// below each character of the message. /// </summary> /// <param name="message">The message to write.</param> /// <param name="foreColor">The foreground colour to write.</param> /// <param name="backgroundColor">The background colour to write.</param> /// <param name="underlineCharacter">The character used to write under the message.</param> public static void WriteUnderLine(string message, ConsoleColor foreColor, ConsoleColor backgroundColor = ConsoleColor.Black, char underlineCharacter = '-') { var resetConsoleColourAspect = new ResetCursorColourAspect(); resetConsoleColourAspect.OnEntry(); try { WriteLine(message, foreColor, backgroundColor); WriteLine(new string(underlineCharacter, message.Length), foreColor, backgroundColor); } finally { resetConsoleColourAspect.OnExit(); } }
/// <summary>Writes the specified string value to the console then reads a string representation of an Enum value.</summary> /// <typeparam name="T">The enum type to validate the input against.</typeparam> /// <param name="message">The message to write.</param> /// <param name="foreColor">The foreground colour to write.</param> /// <param name="backgroundColor">The background colour to write.</param> /// <param name="padding">Used to set the input position.</param> /// <param name="placeHolder">The default value for the input.</param> /// <returns>T.</returns> /// <exception cref="ArgumentException"><typeparamref name="T"/> must be an Enumerable Type</exception> public static T ReadEnum <T>(string message, ConsoleColor foreColor = ConsoleColor.White, ConsoleColor backgroundColor = ConsoleColor.Black, int padding = 0, string placeHolder = "") where T : struct, IConvertible { var resetConsoleColourAspect = new ResetCursorColourAspect(); resetConsoleColourAspect.OnEntry(); try { if (!typeof(T).IsEnum) { throw new ArgumentException("T must be an Enumerable Type"); } string origPlaceHolder = placeHolder; bool validValue = false; T value; do { var input = ReadInput( message, foreColor, backgroundColor, padding, placeHolder); if (input == placeHolder) { input = origPlaceHolder; } validValue = Enum.TryParse(input, true, out value); if (!validValue) { placeHolder = $"{origPlaceHolder} - {input} is invalid"; } } while (!validValue); return(value); } finally { resetConsoleColourAspect.OnExit(); } }
/// <summary>Writes the specified string value to the console.</summary> /// <param name="message">The message to write.</param> /// <param name="foreColor">The foreground colour to write.</param> /// <param name="backgroundColor">The background colour to write.</param> public static void Write(string message, ConsoleColor foreColor, ConsoleColor backgroundColor = ConsoleColor.Black) { var resetConsoleColourAspect = new ResetCursorColourAspect(); resetConsoleColourAspect.OnEntry(); try { lock (_MessageLock) { Console.ForegroundColor = foreColor; Console.BackgroundColor = backgroundColor; Console.Write(message); } } finally { resetConsoleColourAspect.OnExit(); } }
/// <summary>Reads the int.</summary> /// <param name="message">The message to write.</param> /// <param name="foreColor">The foreground colour to write.</param> /// <param name="backgroundColor">The background colour to write.</param> /// <param name="padding">Used to set the input position.</param> /// <param name="placeHolder">The default value for the input.</param> /// <returns>System.Int32.</returns> public static int ReadInt(string message, ConsoleColor foreColor = ConsoleColor.White, ConsoleColor backgroundColor = ConsoleColor.Black, int padding = 0, string placeHolder = "") { var resetConsoleColourAspect = new ResetCursorColourAspect(); resetConsoleColourAspect.OnEntry(); try { string origPlaceHolder = placeHolder; bool validValue = false; int value; do { var input = ReadInput( message, foreColor, backgroundColor, padding, placeHolder); if (input == placeHolder) { input = origPlaceHolder; } validValue = int.TryParse(input, out value); if (!validValue) { placeHolder = $"{origPlaceHolder} - {input} is invalid"; } } while (!validValue); return(value); } finally { resetConsoleColourAspect.OnExit(); } }
/// <summary>Writes the specified string value to the console then reads a line of characters from the console.</summary> /// <param name="message">The message to write.</param> /// <param name="foreColor">The foreground colour to write.</param> /// <param name="backgroundColor">The background colour to write.</param> /// <param name="padding">Used to set the input position.</param> /// <param name="placeHolder">The default value for the input.</param> /// <returns>System.String.</returns> public static string ReadInput(string message, ConsoleColor foreColor = ConsoleColor.White, ConsoleColor backgroundColor = ConsoleColor.Black, int padding = 0, string placeHolder = "") { var resetConsoleColourAspect = new ResetCursorColourAspect(); resetConsoleColourAspect.OnEntry(); try { lock (_MessageLock) { Console.ForegroundColor = foreColor; Console.BackgroundColor = backgroundColor; Console.Write($"{message.PadRight(padding)}"); if (placeHolder != string.Empty) { Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write(placeHolder); Console.CursorLeft = Console.CursorLeft - placeHolder.Length; } Console.ForegroundColor = ConsoleColor.Gray; } string input = Console.ReadLine(); if (input == string.Empty) { input = placeHolder; } return(input); } finally { resetConsoleColourAspect.OnExit(); } }