/// <summary>Writes a text with the specified color, optionally adds a newline after text and optionally resets the color after writing.</summary> /// <param name="text">The text to write.</param> /// <param name="color">The color to set before writing the text.</param> /// <param name="resetColor">Resets the console color after writing the text, if set to <see langword="true"/>.</param> /// <param name="newlineAfterText">Adds a newline after the written text, if set to <see langword="true"/>.</param> public static void WriteWithColorAndOptionalNewline(string text, ConsoleColorSet color, bool resetColor = true, bool newlineAfterText = false) { if (newlineAfterText) { WriteLineWithColor(text, color, resetColor); } else { WriteWithColor(text, color, resetColor); } }
public HostUI() { m_ConsoleColors = new Dictionary <int, ConsoleColorSet>(); m_ConsoleColors[DefaultColor] = new ConsoleColorSet { ForegroundColor = ConsoleColor.Gray, BackgroundColor = ConsoleColor.Black }; m_ConsoleColors[ErrorColor] = new ConsoleColorSet { ForegroundColor = ConsoleColor.Red, BackgroundColor = ConsoleColor.Black }; m_ConsoleColors[WarningColor] = new ConsoleColorSet { ForegroundColor = ConsoleColor.Yellow, BackgroundColor = ConsoleColor.Black }; m_ConsoleColors[VerboseColor] = new ConsoleColorSet { ForegroundColor = ConsoleColor.White, BackgroundColor = ConsoleColor.Black }; m_ConsoleColors[InfoColor] = new ConsoleColorSet { BackgroundColor = ConsoleColor.Gray, ForegroundColor = ConsoleColor.Black }; m_ConsoleColors[DebugColor] = new ConsoleColorSet { BackgroundColor = ConsoleColor.DarkGray, ForegroundColor = ConsoleColor.Black }; }
void ILogPlugin.Log(string source, LogLevel level, object message) { if (!Settings.Default.Logger.Active) { return; } if (message is Exception ex) { var sb = new StringBuilder(); GetErrorLogs(sb, ex); message = sb.ToString(); } lock (typeof(Logger)) { DateTime now = DateTime.Now; var log = $"[{now.TimeOfDay:hh\\:mm\\:ss\\.fff}] {message}"; if (Settings.Default.Logger.ConsoleOutput) { var currentColor = new ConsoleColorSet(); switch (level) { case LogLevel.Debug: DebugColor.Apply(); break; case LogLevel.Error: ErrorColor.Apply(); break; case LogLevel.Fatal: FatalColor.Apply(); break; case LogLevel.Info: InfoColor.Apply(); break; case LogLevel.Warning: WarningColor.Apply(); break; } Console.WriteLine(log); currentColor.Apply(); } if (!string.IsNullOrEmpty(Settings.Default.Logger.Path)) { StringBuilder sb = new StringBuilder(source); foreach (char c in GetInvalidFileNameChars()) { sb.Replace(c, '-'); } var path = Combine(Settings.Default.Logger.Path, sb.ToString()); Directory.CreateDirectory(path); path = Combine(path, $"{now:yyyy-MM-dd}.log"); try { File.AppendAllLines(path, new[] { $"[{level}]{log}" }); } catch (IOException) { Console.WriteLine("Error writing the log file: " + path); } } } }
/// <summary>Writes text to the console with a specified color and appends a newline after the specified text.</summary> /// <param name="text">The text to write to the console.</param> /// <param name="color">The color to set before writing the text.</param> /// <param name="resetColor">Resets the console color after writing the text, if set to <see langword="true"/>.</param> public static void WriteLineWithColor(string text, ConsoleColorSet color, bool resetColor = true) => WriteWithColor($"{text}\n", color, resetColor);
/// <summary>Writes text to the console with a specified color.</summary> /// <param name="text">The text to write to the console.</param> /// <param name="color">The color to set before writing the text.</param> /// <param name="resetColor">Resets the console color after writing the text, if set to <see langword="true"/>.</param> public static void WriteWithColor(string text, ConsoleColorSet color, bool resetColor = true) { SetConsoleColor(color); WriteAndOptionallyResetColor(text, resetColor); }
/// <summary>Sets the console colors to a specified <seealso cref="ConsoleColorSet"/>'s colors.</summary> /// <param name="set">The console color set to set.</param> public static void SetConsoleColor(ConsoleColorSet set) { ForegroundColor = set.ForegroundColor; BackgroundColor = set.BackgroundColor; }
/// <summary>Requests input from the console, providing a request message before doing so, with a specified color for both input and output, separately provided.</summary> /// <param name="requestMessage">The message to write before requesting input.</param> /// <param name="outputColor">The color of the message.</param> /// <param name="inputColor">The color of the input.</param> /// <param name="resetColor">Determines whether the color will be reset after performing the operation.</param> /// <param name="newlineAfterMessage">Determines whether a newline should be added after the message.</param> /// <returns>The input string that was given.</returns> public static string RequestInputLine(string requestMessage, ConsoleColorSet outputColor, ConsoleColorSet inputColor, bool resetColor = true, bool newlineAfterMessage = true) { WriteWithColorAndOptionalNewline(requestMessage, outputColor, false, newlineAfterMessage); return(ReadLineWithColor(inputColor, resetColor)); }
/// <summary>Reads a line from the console with a specified color.</summary> /// <param name="color">The color to set before reading the line.</param> /// <param name="resetColor">Resets the console color after reading the line, if set to <see langword="true"/>.</param> /// <returns>The input string that was given.</returns> public static string ReadLineWithColor(ConsoleColorSet color, bool resetColor = true) { SetConsoleColor(color); return(ReadLineAndOptionallyResetColor(resetColor)); }