Controls, Startup&Driver improvements

This commit is contained in:
2023-08-09 11:54:32 +02:00
parent 2528487ff6
commit d549733b71
49 changed files with 875 additions and 120 deletions

View File

@@ -18,10 +18,10 @@ public static class DI
{
public static IServiceProvider ServiceProvider { get; private set; } = null!;
public static void Initialize(IConfigurationRoot configuration, IServiceCollection serviceCollection)
public static IServiceProvider Initialize(IConfigurationRoot configuration)
=> ServiceProvider = DependencyInjection
.RegisterDefaultServices(configuration: configuration, serviceCollection: serviceCollection)
.AddConsoleServices()
.RegisterDefaultServices(configuration: configuration)
.AddConsoleServices(configuration)
.AddLocalProviderServices()
.AddServerCoreServices()
.AddFrequencyNavigation()
@@ -31,6 +31,8 @@ public static class DI
.AddCompression()
.SetupLogging()
.AddLogging(loggingBuilder => loggingBuilder.AddSerilog())
.AddConsoleDriver()
.AddTheme()
.BuildServiceProvider();

View File

@@ -23,6 +23,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />

View File

@@ -0,0 +1,26 @@
using System.Text;
using FileTime.App.Core.Configuration;
using FileTime.ConsoleUI.App.Configuration;
namespace FileTime.ConsoleUI;
public static class Help
{
public static void PrintHelp()
{
StringBuilder sb = new();
sb.AppendLine("Options:");
PrintDriverOption(sb);
Console.Write(sb.ToString());
}
public static void PrintDriverOption(StringBuilder sb)
{
sb.AppendLine($"--{SectionNames.ApplicationSectionName}.{nameof(ConsoleApplicationConfiguration.ConsoleDriver)}");
foreach (var driver in Startup.Drivers.Keys)
{
sb.AppendLine("\t" + driver);
}
}
}

View File

@@ -1,52 +1,75 @@
using FileTime.App.Core;
using System.Diagnostics;
using FileTime.App.Core;
using FileTime.App.Core.Configuration;
using FileTime.ConsoleUI;
using FileTime.ConsoleUI.App;
using FileTime.ConsoleUI.Styles;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Serilog;
using Serilog.Debugging;
using TerminalUI.ConsoleDrivers;
IConsoleDriver driver = new WindowsDriver();
driver.Init();
ITheme theme;
if (driver.GetCursorPosition() is not {PosX: 0, PosY: 0})
if(args.Contains("--help"))
{
driver = new DotnetDriver();
driver.Init();
theme = DefaultThemes.ConsoleColorTheme;
}
else
{
theme = DefaultThemes.Color256Theme;
Help.PrintHelp();
return;
}
driver.SetCursorVisible(false);
IConsoleDriver? driver = null;
(AppDataRoot, EnvironmentName) = Init.InitDevelopment();
InitLogging();
try
{
(AppDataRoot, EnvironmentName) = Init.InitDevelopment();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(MainConfiguration.Configuration)
#if DEBUG
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
#endif
.Build();
var configuration = CreateConfiguration(args);
var serviceCollection = new ServiceCollection();
serviceCollection.TryAddSingleton<IConsoleDriver>(driver);
serviceCollection.TryAddSingleton<ITheme>(theme);
var serviceProvider = DI.Initialize(configuration);
DI.Initialize(configuration, serviceCollection);
driver = serviceProvider.GetRequiredService<IConsoleDriver>();
Log.Logger.Debug("Using driver {Driver}", driver.GetType().Name);
driver.SetCursorVisible(false);
var app = DI.ServiceProvider.GetRequiredService<IApplication>();
var app = serviceProvider.GetRequiredService<IApplication>();
app.Run();
}
finally
{
driver.SetCursorVisible(true);
driver.Dispose();
driver?.SetCursorVisible(true);
driver?.Dispose();
}
static void InitLogging()
{
SelfLog.Enable(l => Debug.WriteLine(l));
var logFolder = Path.Combine(AppDataRoot, "logs", "bootstrap");
if (!Directory.Exists(logFolder)) Directory.CreateDirectory(logFolder);
Log.Logger = new LoggerConfiguration()
#if DEBUG || VERBOSE_LOGGING
.MinimumLevel.Verbose()
#endif
.Enrich.FromLogContext()
.WriteTo.File(
Path.Combine(logFolder, "appLog.log"),
fileSizeLimitBytes: 10 * 1024 * 1024,
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true)
.CreateBootstrapLogger();
}
static IConfigurationRoot CreateConfiguration(string[] strings)
{
var configurationRoot = new ConfigurationBuilder()
.AddInMemoryCollection(MainConfiguration.Configuration)
.AddInMemoryCollection(MainConsoleConfiguration.Configuration)
#if DEBUG
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
#endif
.AddCommandLine(strings)
.Build();
return configurationRoot;
}
public partial class Program

View File

@@ -0,0 +1,66 @@
using FileTime.ConsoleUI.App;
using FileTime.ConsoleUI.App.Configuration;
using FileTime.ConsoleUI.Styles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using TerminalUI.ConsoleDrivers;
namespace FileTime.ConsoleUI;
public static class Startup
{
public static readonly Dictionary<string, Func<IConsoleDriver>> Drivers = new()
{
["windows"] = () => new XTermDriver(),
["dotnet"] = () => new DotnetDriver()
};
public static IServiceCollection AddConsoleDriver(this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton<IConsoleDriver>(sp =>
{
var appConfig = sp.GetRequiredService<IOptions<ConsoleApplicationConfiguration>>();
IConsoleDriver? driver = null;
if (appConfig.Value.ConsoleDriver is { } consoleDriver
&& Drivers.TryGetValue(consoleDriver, out var driverFactory))
{
driver = driverFactory();
driver.Init();
}
if (driver == null)
{
driver = new XTermDriver();
var asd = driver.GetCursorPosition();
driver.Init();
if (!driver.Init())
{
driver = new DotnetDriver();
driver.Init();
}
}
return driver;
});
return serviceCollection;
}
public static IServiceCollection AddTheme(this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton<ITheme>(sp =>
{
var driver = sp.GetRequiredService<IConsoleDriver>();
return driver switch
{
XTermDriver _ => DefaultThemes.Color256Theme,
DotnetDriver _ => DefaultThemes.ConsoleColorTheme,
_ => throw new ArgumentOutOfRangeException(nameof(driver))
};
});
return serviceCollection;
}
}