ColoredConsole rework
This commit is contained in:
parent
052169df1c
commit
613b11e88c
@ -1,86 +1,89 @@
|
|||||||
namespace DTLib.Console;
|
namespace DTLib.Console;
|
||||||
|
|
||||||
//
|
|
||||||
// вывод и ввод цветного текста в консоли
|
|
||||||
// работает медленнее чем хотелось бы
|
|
||||||
//
|
|
||||||
public static class ColoredConsole
|
public static class ColoredConsole
|
||||||
{
|
{
|
||||||
// парсит название цвета в ConsoleColor
|
public static int Width => System.Console.WindowWidth;
|
||||||
public static ConsoleColor ParseColor(string color) => color switch
|
public static int Height => System.Console.WindowHeight;
|
||||||
{
|
|
||||||
//case "magneta":
|
|
||||||
"m" => ConsoleColor.Magenta,
|
|
||||||
//case "green":
|
|
||||||
"g" => ConsoleColor.Green,
|
|
||||||
//case "red":
|
|
||||||
"r" => ConsoleColor.Red,
|
|
||||||
//case "yellow":
|
|
||||||
"y" => ConsoleColor.Yellow,
|
|
||||||
//case "white":
|
|
||||||
"w" => ConsoleColor.White,
|
|
||||||
//case "blue":
|
|
||||||
"b" => ConsoleColor.Blue,
|
|
||||||
//case "cyan":
|
|
||||||
"c" => ConsoleColor.Cyan,
|
|
||||||
//case "h":
|
|
||||||
"h" or "gray" => ConsoleColor.Gray,
|
|
||||||
//case "black":
|
|
||||||
"black" => ConsoleColor.Black,
|
|
||||||
_ => throw new Exception($"ColoredConsole.ParseColor({color}) error: incorrect color"),
|
|
||||||
};
|
|
||||||
|
|
||||||
public static void Write(ConsoleColor color,string msg)
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static void Fg(ConsoleColor color) => System.Console.ForegroundColor = color;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static void Bg(ConsoleColor color) => System.Console.BackgroundColor = color;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static void ResetColor() => System.Console.ResetColor();
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static void Clear()
|
||||||
{
|
{
|
||||||
System.Console.ForegroundColor = color;
|
ResetColor();
|
||||||
System.Console.Write(msg);
|
System.Console.Clear();
|
||||||
System.Console.ForegroundColor = ConsoleColor.Gray;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Write(string msg) => Write(ConsoleColor.Gray, msg);
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static void Write(char c) => System.Console.Write(c);
|
||||||
|
|
||||||
// вывод цветного текста
|
public static void Write(string msg, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
public static void Write(params string[] input)
|
|
||||||
{
|
{
|
||||||
if (input.Length == 1)
|
if(fg != null) Fg(fg.Value);
|
||||||
|
if(bg != null) Bg(bg.Value);
|
||||||
|
#if NETSTANDARD2_0
|
||||||
|
var chars = msg.ToCharArray();
|
||||||
|
#else
|
||||||
|
var chars = msg.AsSpan();
|
||||||
|
#endif
|
||||||
|
for (int i = 0; i < chars.Length; ++i)
|
||||||
{
|
{
|
||||||
Write(input[0]);
|
Write(chars[i]);
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input.Length % 2 != 0)
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
throw new Exception("ColoredConsole.Write() error: every text string must have color string before");
|
public static void WriteLine() => Write('\n');
|
||||||
|
|
||||||
for (ushort i = 0; i < input.Length; i++)
|
public static void WriteLine(string msg, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
{
|
{
|
||||||
System.Console.ForegroundColor = ParseColor(input[i++]);
|
Write(msg, fg, bg);
|
||||||
System.Console.Write(input[i]);
|
|
||||||
}
|
|
||||||
System.Console.ForegroundColor = ConsoleColor.Gray;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void WriteLine() => System.Console.WriteLine();
|
|
||||||
public static void WriteLine(ConsoleColor color,string msg)
|
|
||||||
{
|
|
||||||
System.Console.ForegroundColor = color;
|
|
||||||
System.Console.WriteLine(msg);
|
|
||||||
System.Console.ForegroundColor = ConsoleColor.Gray;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void WriteLine(params string[] input)
|
|
||||||
{
|
|
||||||
Write(input);
|
|
||||||
WriteLine();
|
WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ввод цветного текста
|
public static string? ReadLine(string query, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
public static string? Read(ConsoleColor color)
|
|
||||||
{
|
{
|
||||||
System.Console.ForegroundColor = color;
|
Write(query, fg, bg);
|
||||||
var r = System.Console.ReadLine();
|
Write(':');
|
||||||
System.Console.ForegroundColor = ConsoleColor.Gray;
|
Write(' ');
|
||||||
return r;
|
return System.Console.ReadLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string? Read(string color) => Read(ParseColor(color));
|
public static void WriteHLine(char c, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
|
{
|
||||||
|
WriteLine(c.Multiply(Width - 1), fg, bg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void WriteTitle(string title, char spacing = '-',
|
||||||
|
string left_framing = "[", string right_framing = "]",
|
||||||
|
ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
|
{
|
||||||
|
int both_spacing_length = Width - title.Length - left_framing.Length - right_framing.Length - 1;
|
||||||
|
int left_spacing_length = both_spacing_length / 2;
|
||||||
|
int right_spacing_length = left_spacing_length + both_spacing_length % 2;
|
||||||
|
|
||||||
|
var b = new StringBuilder();
|
||||||
|
if(left_spacing_length > 0)
|
||||||
|
b.Append(spacing.Multiply(left_spacing_length));
|
||||||
|
b.Append(left_framing);
|
||||||
|
b.Append(title);
|
||||||
|
b.Append(right_framing);
|
||||||
|
if(right_spacing_length > 0)
|
||||||
|
b.Append(spacing.Multiply(right_spacing_length));
|
||||||
|
|
||||||
|
WriteLine(b.ToString(), fg, bg);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static void WriteCentred(string title, ConsoleColor? fg = null, ConsoleColor? bg = null)
|
||||||
|
{
|
||||||
|
WriteTitle(title, ' ', "", "", fg, bg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<!--package info-->
|
<!--package info-->
|
||||||
<PackageId>DTLib</PackageId>
|
<PackageId>DTLib</PackageId>
|
||||||
<Version>1.4.3</Version>
|
<Version>1.6.0</Version>
|
||||||
<Authors>Timerix</Authors>
|
<Authors>Timerix</Authors>
|
||||||
<Description>Library for all my C# projects</Description>
|
<Description>Library for all my C# projects</Description>
|
||||||
<RepositoryType>GIT</RepositoryType>
|
<RepositoryType>GIT</RepositoryType>
|
||||||
|
|||||||
@ -27,7 +27,7 @@ public class ConsoleLogger : ILogger
|
|||||||
|
|
||||||
var msg = format.CreateMessage(context, severity, message);
|
var msg = format.CreateMessage(context, severity, message);
|
||||||
lock (consolelocker)
|
lock (consolelocker)
|
||||||
ColoredConsole.WriteLine(ColorFromSeverity(severity),msg);
|
ColoredConsole.WriteLine(msg, fg: ColorFromSeverity(severity));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ConsoleColor ColorFromSeverity(LogSeverity severity)
|
private static ConsoleColor ColorFromSeverity(LogSeverity severity)
|
||||||
|
|||||||
@ -1,13 +1,11 @@
|
|||||||
using DTLib.Logging;
|
namespace DTLib.Logging;
|
||||||
|
|
||||||
namespace launcher_client;
|
public interface ILineWriter : IDisposable
|
||||||
|
|
||||||
public interface IConsoleWrapper : IDisposable
|
|
||||||
{
|
{
|
||||||
public void WriteLine(string msg);
|
public void WriteLine(string msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ConsoleWrapperLogger : ILogger
|
public class LineWriterLogger : ILogger
|
||||||
{
|
{
|
||||||
public bool DebugLogEnabled { get; set; } = false;
|
public bool DebugLogEnabled { get; set; } = false;
|
||||||
public bool InfoLogEnabled { get; set; } = true;
|
public bool InfoLogEnabled { get; set; } = true;
|
||||||
@ -15,16 +13,16 @@ public class ConsoleWrapperLogger : ILogger
|
|||||||
public bool ErrorLogEnabled { get; set; } = true;
|
public bool ErrorLogEnabled { get; set; } = true;
|
||||||
public ILogFormat Format { get; set; }
|
public ILogFormat Format { get; set; }
|
||||||
|
|
||||||
private readonly IConsoleWrapper _consoleWrapper;
|
private readonly ILineWriter _lineWriter;
|
||||||
|
|
||||||
public ConsoleWrapperLogger(IConsoleWrapper consoleWrapper, ILogFormat format)
|
public LineWriterLogger(ILineWriter lineWriter, ILogFormat format)
|
||||||
{
|
{
|
||||||
_consoleWrapper = consoleWrapper;
|
_lineWriter = lineWriter;
|
||||||
Format = format;
|
Format = format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConsoleWrapperLogger(IConsoleWrapper consoleWrapper)
|
public LineWriterLogger(ILineWriter lineWriter)
|
||||||
: this(consoleWrapper, new DefaultLogFormat())
|
: this(lineWriter, new DefaultLogFormat())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
public void Log(string context, LogSeverity severity, object message, ILogFormat format)
|
public void Log(string context, LogSeverity severity, object message, ILogFormat format)
|
||||||
@ -33,14 +31,14 @@ public class ConsoleWrapperLogger : ILogger
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
var msg = format.CreateMessage(context, severity, message);
|
var msg = format.CreateMessage(context, severity, message);
|
||||||
lock (_consoleWrapper)
|
lock (_lineWriter)
|
||||||
_consoleWrapper.WriteLine(msg);
|
_lineWriter.WriteLine(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_consoleWrapper.Dispose();
|
_lineWriter.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
~ConsoleWrapperLogger() => Dispose();
|
~LineWriterLogger() => Dispose();
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user