From 73979336011c8eabfc23afcf2c5e6ef10eccb629 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 21:19:01 +0600 Subject: [PATCH] added colors to ConsoleLogger --- DTLib.Logging/Loggers/ConsoleLogger.cs | 11 ++++- DTLib.sln | 3 +- DTLib/ColoredConsole.cs | 56 +++++++++++++------------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/DTLib.Logging/Loggers/ConsoleLogger.cs b/DTLib.Logging/Loggers/ConsoleLogger.cs index 6f9e660..1674f7a 100644 --- a/DTLib.Logging/Loggers/ConsoleLogger.cs +++ b/DTLib.Logging/Loggers/ConsoleLogger.cs @@ -17,12 +17,21 @@ public class ConsoleLogger : ILogger { var msg = format.CreateMessage(context, severity, message); lock (consolelocker) - ColoredConsole.Write(msg); + ColoredConsole.Write(ColorFromSeverity(severity),msg); } public void Log(string context, LogSeverity severity, object message) => Log(context, severity, message, Format); + private static ConsoleColor ColorFromSeverity(LogSeverity severity) + => severity switch + { + LogSeverity.Debug => ConsoleColor.Gray, + LogSeverity.Info => ConsoleColor.White, + LogSeverity.Warn => ConsoleColor.Yellow, + LogSeverity.Error => ConsoleColor.Red, + _ => throw new ArgumentOutOfRangeException(nameof(severity), severity, null) + }; public void Dispose() { diff --git a/DTLib.sln b/DTLib.sln index f24c434..15aec6c 100644 --- a/DTLib.sln +++ b/DTLib.sln @@ -9,8 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTLib.Tests", "DTLib.Tests\ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6308F24E-A4FF-46B3-B72F-30E05DDCB1D5}" ProjectSection(SolutionItems) = preProject - .gitignore = .gitignore README.md = README.md + .gitignore = .gitignore + .gitmodules = .gitmodules EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTLib.Dtsod", "DTLib.Dtsod\DTLib.Dtsod.csproj", "{ADE425F5-8645-47F0-9AA8-33FA748D36BE}" diff --git a/DTLib/ColoredConsole.cs b/DTLib/ColoredConsole.cs index b23f61b..44209e6 100644 --- a/DTLib/ColoredConsole.cs +++ b/DTLib/ColoredConsole.cs @@ -30,36 +30,36 @@ public static class ColoredConsole _ => throw new Exception($"ColoredConsole.ParseColor({color}) error: incorrect color"), }; + public static void Write(ConsoleColor color,string msg) + { + Console.ForegroundColor = color; + Console.Write(msg); + Console.ForegroundColor = ConsoleColor.Gray; + } + + public static void Write(string msg) => Write(ConsoleColor.Gray, msg); + // вывод цветного текста public static void Write(params string[] input) { - if (input.Length == 1) + if (input.Length % 2 != 0) + throw new Exception("ColoredConsole.Write() error: every text string must have color string before"); + + for (ushort i = 0; i < input.Length; i++) { - if (Console.ForegroundColor != ConsoleColor.Gray) - Console.ForegroundColor = ConsoleColor.Gray; - Console.Write(input[0]); + Console.ForegroundColor = ParseColor(input[i++]); + Console.Write(input[i]); } - else if (input.Length % 2 == 0) - { - StringBuilder strB = new(); - for (ushort i = 0; i < input.Length; i++) - { - ConsoleColor c = ParseColor(input[i]); - if (Console.ForegroundColor != c) - { - Console.Write(strB.ToString()); - Console.ForegroundColor = c; - strB.Clear(); - } - strB.Append(input[++i]); - } - if (strB.Length > 0) - Console.Write(strB.ToString()); - } - else throw new Exception("ColoredConsole.Write() error: every text string must have color string before"); + Console.ForegroundColor = ConsoleColor.Gray; } public static void WriteLine() => Console.WriteLine(); + public static void WriteLine(ConsoleColor color,string msg) + { + Console.ForegroundColor = color; + Console.WriteLine(msg); + Console.ForegroundColor = ConsoleColor.Gray; + } public static void WriteLine(params string[] input) { @@ -68,11 +68,13 @@ public static class ColoredConsole } // ввод цветного текста - public static string Read(string color) + public static string Read(ConsoleColor color) { - ConsoleColor c = ParseColor(color); - if (Console.ForegroundColor != c) - Console.ForegroundColor = c; - return Console.ReadLine(); + Console.ForegroundColor = color; + var r = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.Gray; + return r; } + + public static string Read(string color) => Read(ParseColor(color)); }