Console improvements, info providers

This commit is contained in:
2023-08-09 23:24:30 +02:00
parent 7dcca6363b
commit af140ff6b4
21 changed files with 406 additions and 69 deletions

View File

@@ -49,6 +49,7 @@ public static class DI
.WriteTo.File(
Path.Combine(Program.AppDataRoot, "logs", "appLog.log"),
fileSizeLimitBytes: 10 * 1024 * 1024,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true)
.WriteTo.Sink(serviceProvider.GetRequiredService<CustomLoggerSink>());

View File

@@ -0,0 +1,129 @@
using FileTime.ConsoleUI.App;
using TerminalUI.Color;
using TerminalUI.ConsoleDrivers;
using TerminalUI.Models;
namespace FileTime.ConsoleUI.InfoProviders;
public static class ColorSchema
{
private const int ColorTextMargin = 5;
public static void PrintColorSchema(ITheme theme, IConsoleDriver consoleDriver)
{
consoleDriver.Dispose();
consoleDriver.ResetColor();
PrintThemeColors(theme, consoleDriver);
if (theme is IColorSampleProvider colorSampleProvider)
PrintColorPalette(colorSampleProvider, consoleDriver);
}
private static void PrintThemeColors(ITheme theme, IConsoleDriver consoleDriver)
{
consoleDriver.Write("Theme colors:" + Environment.NewLine);
var colorType = typeof(IColor);
var colorProperties = typeof(ITheme)
.GetProperties()
.Where(p => p.PropertyType.IsAssignableTo(colorType))
.OrderBy(p => p.Name)
.ToList();
if (colorProperties.Count == 0)
{
consoleDriver.Write("No colors properties found");
return;
}
var colorTextStartX = colorProperties.Max(p => p.Name.Length) + ColorTextMargin;
foreach (var colorProperty in colorProperties)
{
var color = colorProperty.GetValue(theme) as IColor;
PrintColor(consoleDriver, colorProperty.Name, color, colorTextStartX);
}
consoleDriver.ResetColor();
consoleDriver.Write(Environment.NewLine);
}
private static void PrintColorPalette(IColorSampleProvider colorSampleProvider, IConsoleDriver consoleDriver)
{
if (colorSampleProvider.ForegroundColors is { } foregroundColors)
{
PrintColorPalette("foreground", foregroundColors, consoleDriver);
}
if (colorSampleProvider.BackgroundColors is { } backgroundColors)
{
PrintColorPalette("background", backgroundColors, consoleDriver);
}
}
private static void PrintColorPalette(string paletteName, Type colorPalette, IConsoleDriver consoleDriver)
{
var colorType = typeof(IColor);
var colorFields = colorPalette
.GetFields()
.Where(f => f.FieldType.IsAssignableTo(colorType) && f.IsStatic)
.ToDictionary(f => f.Name, f => (IColor?) f.GetValue(null));
var colorProperties = colorPalette
.GetProperties()
.Where(p => p.PropertyType.IsAssignableTo(colorType) && (p.GetMethod?.IsStatic ?? false))
.ToDictionary(p => p.Name, p => (IColor?) p.GetValue(null));
var colors = colorFields
.Concat(colorProperties)
.OrderBy(v => v.Key)
.ToDictionary(k => k.Key, v => v.Value);
consoleDriver.Write("Color theme for " + paletteName + Environment.NewLine);
if (colors.Count == 0)
{
consoleDriver.Write("No colors found");
consoleDriver.Write(Environment.NewLine);
return;
}
var colorTextStartX = colors.Max(p => p.Key.Length) + ColorTextMargin;
foreach (var (key, value) in colors)
{
PrintColor(consoleDriver, key, value, colorTextStartX);
}
consoleDriver.ResetColor();
consoleDriver.Write(Environment.NewLine);
}
private static void PrintColor(IConsoleDriver consoleDriver, string name, IColor? color, int colorTextStartX)
{
consoleDriver.ResetColor();
consoleDriver.Write(name + ":");
var y = consoleDriver.GetCursorPosition().Y;
consoleDriver.SetCursorPosition(new Position(colorTextStartX, y));
if (color is null)
{
consoleDriver.Write("<null>");
}
else
{
if (color.Type == ColorType.Foreground)
{
consoleDriver.SetForegroundColor(color);
}
else
{
consoleDriver.SetBackgroundColor(color);
}
consoleDriver.Write("Sample text");
}
consoleDriver.ResetColor();
consoleDriver.Write(Environment.NewLine);
}
}

View File

@@ -2,23 +2,30 @@
using FileTime.App.Core.Configuration;
using FileTime.ConsoleUI.App.Configuration;
namespace FileTime.ConsoleUI;
namespace FileTime.ConsoleUI.InfoProviders;
public static class Help
{
public static void PrintHelp()
public static void PrintHelp(IEnumerable<string> infoProvidersKeys)
{
StringBuilder sb = new();
sb.AppendLine("Options:");
PrintDriverOption(sb);
sb.AppendLine();
sb.AppendLine("Info providers:");
foreach (var infoProviderKey in infoProvidersKeys.Order())
{
sb.AppendLine("\t" + infoProviderKey);
}
Console.Write(sb.ToString());
}
public static void PrintDriverOption(StringBuilder sb)
private static void PrintDriverOption(StringBuilder sb)
{
sb.AppendLine($"--{SectionNames.ApplicationSectionName}.{nameof(ConsoleApplicationConfiguration.ConsoleDriver)}");
foreach (var driver in Startup.Drivers.Keys)
foreach (var driver in Startup.Drivers.Keys.Order())
{
sb.AppendLine("\t" + driver);
}

View File

@@ -3,18 +3,13 @@ using FileTime.App.Core;
using FileTime.App.Core.Configuration;
using FileTime.ConsoleUI;
using FileTime.ConsoleUI.App;
using FileTime.ConsoleUI.InfoProviders;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Debugging;
using TerminalUI.ConsoleDrivers;
if(args.Contains("--help"))
{
Help.PrintHelp();
return;
}
IConsoleDriver? driver = null;
(AppDataRoot, EnvironmentName) = Init.InitDevelopment();
@@ -25,8 +20,11 @@ try
var serviceProvider = DI.Initialize(configuration);
if (HandleInfoProviders(args, serviceProvider)) return;
driver = serviceProvider.GetRequiredService<IConsoleDriver>();
Log.Logger.Debug("Using driver {Driver}", driver.GetType().Name);
driver.SetCursorVisible(false);
var app = serviceProvider.GetRequiredService<IApplication>();
@@ -54,6 +52,7 @@ static void InitLogging()
.WriteTo.File(
Path.Combine(logFolder, "appLog.log"),
fileSizeLimitBytes: 10 * 1024 * 1024,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true)
.CreateBootstrapLogger();
@@ -72,6 +71,32 @@ static IConfigurationRoot CreateConfiguration(string[] strings)
return configurationRoot;
}
static bool HandleInfoProviders(string[] args, IServiceProvider serviceProvider)
{
Dictionary<string, Action> infoProviders = new()
{
{
"--info=colors",
() => ColorSchema.PrintColorSchema(
serviceProvider.GetRequiredService<ITheme>(),
serviceProvider.GetRequiredService<IConsoleDriver>()
)
},
};
infoProviders.Add("--help", () => Help.PrintHelp(infoProviders.Keys));
foreach (var infoProviderKey in infoProviders.Keys)
{
if (args.Contains(infoProviderKey))
{
infoProviders[infoProviderKey]();
return true;
}
}
return false;
}
public partial class Program
{
public static string AppDataRoot { get; private set; }

View File

@@ -32,8 +32,6 @@ public static class Startup
if (driver == null)
{
driver = new XTermDriver();
var asd = driver.GetCursorPosition();
driver.Init();
if (!driver.Init())
{
driver = new DotnetDriver();