Controls, Startup&Driver improvements
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace FileTime.ConsoleUI.App.Configuration;
|
||||
|
||||
public class ConsoleApplicationConfiguration
|
||||
{
|
||||
public string? ConsoleDriver { get; set; }
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using TerminalUI.Models;
|
||||
using TerminalUI.Color;
|
||||
using TerminalUI.Models;
|
||||
|
||||
namespace FileTime.ConsoleUI.App;
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using FileTime.App.Core.Models;
|
||||
using System.Collections.Specialized;
|
||||
using FileTime.App.Core.Models;
|
||||
using FileTime.App.Core.Services;
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using FileTime.ConsoleUI.App.KeyInputHandling;
|
||||
using TerminalUI;
|
||||
using TerminalUI.ConsoleDrivers;
|
||||
@@ -16,6 +18,7 @@ public class App : IApplication
|
||||
private readonly MainWindow _mainWindow;
|
||||
private readonly IApplicationContext _applicationContext;
|
||||
private readonly IConsoleDriver _consoleDriver;
|
||||
private readonly IAppState _appState;
|
||||
private readonly IKeyInputHandlerService _keyInputHandlerService;
|
||||
private readonly Thread _renderThread;
|
||||
|
||||
@@ -26,7 +29,8 @@ public class App : IApplication
|
||||
IAppKeyService<ConsoleKey> appKeyService,
|
||||
MainWindow mainWindow,
|
||||
IApplicationContext applicationContext,
|
||||
IConsoleDriver consoleDriver)
|
||||
IConsoleDriver consoleDriver,
|
||||
IAppState appState)
|
||||
{
|
||||
_lifecycleService = lifecycleService;
|
||||
_keyInputHandlerService = keyInputHandlerService;
|
||||
@@ -35,6 +39,7 @@ public class App : IApplication
|
||||
_mainWindow = mainWindow;
|
||||
_applicationContext = applicationContext;
|
||||
_consoleDriver = consoleDriver;
|
||||
_appState = appState;
|
||||
|
||||
_renderThread = new Thread(Render);
|
||||
}
|
||||
@@ -43,6 +48,12 @@ public class App : IApplication
|
||||
{
|
||||
Task.Run(async () => await _lifecycleService.InitStartupHandlersAsync()).Wait();
|
||||
|
||||
((INotifyCollectionChanged) _appState.Tabs).CollectionChanged += (_, _) =>
|
||||
{
|
||||
if(_appState.Tabs.Count == 0)
|
||||
_applicationContext.IsRunning = false;
|
||||
};
|
||||
|
||||
_mainWindow.Initialize();
|
||||
foreach (var rootView in _mainWindow.RootViews())
|
||||
{
|
||||
@@ -66,6 +77,7 @@ public class App : IApplication
|
||||
_keyInputHandlerService.HandleKeyInput(keyEventArgs);
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using FileTime.App.Core.ViewModels.Timeline;
|
||||
using PropertyChanged.SourceGenerator;
|
||||
|
||||
namespace FileTime.ConsoleUI.App;
|
||||
|
||||
public class ConsoleAppState : AppStateBase, IConsoleAppState
|
||||
public partial class ConsoleAppState : AppStateBase, IConsoleAppState
|
||||
{
|
||||
[Notify] private string? _errorText;
|
||||
}
|
||||
@@ -12,6 +12,10 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="PropertyChanged.SourceGenerator" Version="1.0.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Serilog" Version="3.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace FileTime.ConsoleUI.App;
|
||||
|
||||
public class MainConsoleConfiguration
|
||||
{
|
||||
public static Dictionary<string, string?> Configuration { get; }
|
||||
static MainConsoleConfiguration()
|
||||
{
|
||||
Configuration = new();
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,12 @@ using DeclarativeProperty;
|
||||
using FileTime.App.Core.Models.Enums;
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using TerminalUI;
|
||||
using TerminalUI.Color;
|
||||
using TerminalUI.Controls;
|
||||
using TerminalUI.Extensions;
|
||||
using TerminalUI.Models;
|
||||
using TerminalUI.ViewExtensions;
|
||||
using ConsoleColor = TerminalUI.Color.ConsoleColor;
|
||||
|
||||
namespace FileTime.ConsoleUI.App;
|
||||
|
||||
@@ -17,6 +20,8 @@ public class MainWindow
|
||||
private readonly ITheme _theme;
|
||||
private ListView<IAppState, IItemViewModel> _selectedItemsView;
|
||||
|
||||
private Grid<object> _grid;
|
||||
|
||||
public MainWindow(
|
||||
IConsoleAppState consoleAppState,
|
||||
IApplicationContext applicationContext,
|
||||
@@ -56,9 +61,69 @@ public class MainWindow
|
||||
_selectedItemsView,
|
||||
appState => appState == null ? null : appState.SelectedTab.Map(t => t == null ? null : t.CurrentItems).Switch(),
|
||||
v => v.ItemsSource);
|
||||
|
||||
TestGrid();
|
||||
}
|
||||
|
||||
public IEnumerable<IView> RootViews() => new IView[] {_selectedItemsView};
|
||||
private void TestGrid()
|
||||
{
|
||||
var grid = new Grid<object>
|
||||
{
|
||||
ApplicationContext = _applicationContext,
|
||||
ColumnDefinitionsObject = "Auto Auto",
|
||||
RowDefinitionsObject = "Auto Auto",
|
||||
ChildInitializer =
|
||||
{
|
||||
new Rectangle<object>
|
||||
{
|
||||
Fill = new ConsoleColor(System.ConsoleColor.Blue, ColorType.Foreground),
|
||||
Extensions =
|
||||
{
|
||||
new GridPositionExtension(0, 0)
|
||||
},
|
||||
Width = 2,
|
||||
Height = 2,
|
||||
},
|
||||
new Rectangle<object>
|
||||
{
|
||||
Fill = new ConsoleColor(System.ConsoleColor.Red, ColorType.Foreground),
|
||||
Extensions =
|
||||
{
|
||||
new GridPositionExtension(0, 1)
|
||||
},
|
||||
Width = 3,
|
||||
Height = 3,
|
||||
},
|
||||
new Rectangle<object>
|
||||
{
|
||||
Fill = new ConsoleColor(System.ConsoleColor.Green, ColorType.Foreground),
|
||||
Extensions =
|
||||
{
|
||||
new GridPositionExtension(1, 0)
|
||||
},
|
||||
Width = 4,
|
||||
Height = 4,
|
||||
},
|
||||
new Rectangle<object>
|
||||
{
|
||||
Fill = new ConsoleColor(System.ConsoleColor.Yellow, ColorType.Foreground),
|
||||
Extensions =
|
||||
{
|
||||
new GridPositionExtension(1, 1)
|
||||
},
|
||||
Width = 5,
|
||||
Height = 5,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
_grid = grid;
|
||||
}
|
||||
|
||||
public IEnumerable<IView> RootViews() => new IView[]
|
||||
{
|
||||
_grid, _selectedItemsView
|
||||
};
|
||||
|
||||
private IColor? ToForegroundColor(ItemViewMode viewMode)
|
||||
=> viewMode switch
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
using FileTime.App.Core.Services;
|
||||
using FileTime.App.Core.Configuration;
|
||||
using FileTime.App.Core.Services;
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using FileTime.ConsoleUI.App.Configuration;
|
||||
using FileTime.ConsoleUI.App.KeyInputHandling;
|
||||
using FileTime.ConsoleUI.App.Services;
|
||||
using FileTime.Core.Interactions;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using TerminalUI;
|
||||
@@ -12,7 +15,7 @@ namespace FileTime.ConsoleUI.App;
|
||||
|
||||
public static class Startup
|
||||
{
|
||||
public static IServiceCollection AddConsoleServices(this IServiceCollection services)
|
||||
public static IServiceCollection AddConsoleServices(this IServiceCollection services, IConfigurationRoot configuration)
|
||||
{
|
||||
services.TryAddSingleton<IApplication, App>();
|
||||
services.TryAddSingleton<MainWindow>();
|
||||
@@ -23,6 +26,9 @@ public static class Startup
|
||||
services.TryAddSingleton<IAppKeyService<ConsoleKey>, ConsoleAppKeyService>();
|
||||
services.TryAddSingleton<ISystemClipboardService, ConsoleSystemClipboardService>();
|
||||
services.AddSingleton<CustomLoggerSink>();
|
||||
services.TryAddSingleton(new ApplicationConfiguration(true));
|
||||
|
||||
services.Configure<ConsoleApplicationConfiguration>(configuration);
|
||||
|
||||
services.TryAddSingleton<IApplicationContext>(sp
|
||||
=> new ApplicationContext
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using FileTime.ConsoleUI.App;
|
||||
using TerminalUI.Color;
|
||||
using TerminalUI.Models;
|
||||
using ConsoleColor = TerminalUI.Models.ConsoleColor;
|
||||
using ConsoleColor = TerminalUI.Color.ConsoleColor;
|
||||
|
||||
namespace FileTime.ConsoleUI.Styles;
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
|
||||
@@ -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" />
|
||||
|
||||
26
src/ConsoleApp/FileTime.ConsoleUI/Help.cs
Normal file
26
src/ConsoleApp/FileTime.ConsoleUI/Help.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
66
src/ConsoleApp/FileTime.ConsoleUI/Startup.cs
Normal file
66
src/ConsoleApp/FileTime.ConsoleUI/Startup.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user