Console Styling

This commit is contained in:
2023-08-21 12:27:02 +02:00
parent 793f653db0
commit fff07e5da7
26 changed files with 572 additions and 160 deletions

View File

@@ -19,7 +19,6 @@
<ProjectReference Include="..\..\Library\TerminalUI.DependencyInjection\TerminalUI.DependencyInjection.csproj" />
<ProjectReference Include="..\..\Tools\FileTime.Tools.Compression\FileTime.Tools.Compression.csproj" />
<ProjectReference Include="..\FileTime.ConsoleUI.App\FileTime.ConsoleUI.App.csproj" />
<ProjectReference Include="..\FileTime.ConsoleUI.Styles\FileTime.ConsoleUI.Styles.csproj" />
</ItemGroup>
<ItemGroup>
@@ -39,4 +38,8 @@
<Content Include="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<Content Include="appsettings.Local.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>

View File

@@ -10,14 +10,15 @@ public static class ColorSchema
{
private const int ColorTextMargin = 5;
public static void PrintColorSchema(ITheme theme, IConsoleDriver consoleDriver)
public static void PrintColorSchema(IThemeProvider themeProvider, IColorProvider colorProvider, IConsoleDriver consoleDriver)
{
var theme = themeProvider.CurrentTheme;
consoleDriver.Dispose();
consoleDriver.ResetStyle();
PrintThemeColors(theme, consoleDriver);
if (theme is IColorSampleProvider colorSampleProvider)
PrintColorPalette(colorSampleProvider, consoleDriver);
PrintColorPalette(colorProvider, consoleDriver);
}
private static void PrintThemeColors(ITheme theme, IConsoleDriver consoleDriver)
@@ -50,37 +51,26 @@ public static class ColorSchema
consoleDriver.Write(Environment.NewLine);
}
private static void PrintColorPalette(IColorSampleProvider colorSampleProvider, IConsoleDriver consoleDriver)
private static void PrintColorPalette(IColorProvider colorSampleProvider, IConsoleDriver consoleDriver)
{
if (colorSampleProvider.ForegroundColors is { } foregroundColors)
{
PrintColorPalette("foreground", foregroundColors, consoleDriver);
}
var colorPalette = colorSampleProvider.GetType();
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));
.Where(f => f.FieldType.IsAssignableTo(colorType) && !f.IsStatic)
.ToDictionary(f => f.Name, f => (IColor?) f.GetValue(colorSampleProvider));
var colorProperties = colorPalette
.GetProperties()
.Where(p => p.PropertyType.IsAssignableTo(colorType) && (p.GetMethod?.IsStatic ?? false))
.ToDictionary(p => p.Name, p => (IColor?) p.GetValue(null));
.Where(p => p.PropertyType.IsAssignableTo(colorType) && p.GetMethod is {IsStatic: false})
.ToDictionary(p => p.Name, p => (IColor?) p.GetValue(colorSampleProvider));
var colors = colorFields
.Concat(colorProperties)
.OrderBy(v => v.Key)
.ToDictionary(k => k.Key, v => v.Value);
consoleDriver.Write("Color palette for " + paletteName + Environment.NewLine);
consoleDriver.Write("Color palette for " + colorPalette.Name + Environment.NewLine);
if (colors.Count == 0)
{
@@ -94,7 +84,7 @@ public static class ColorSchema
{
PrintColor(consoleDriver, key, value, colorTextStartX);
}
consoleDriver.ResetStyle();
consoleDriver.Write(Environment.NewLine);
}

View File

@@ -3,12 +3,14 @@ using FileTime.App.Core;
using FileTime.App.Core.Configuration;
using FileTime.ConsoleUI;
using FileTime.ConsoleUI.App;
using FileTime.ConsoleUI.App.Styling;
using FileTime.ConsoleUI.InfoProviders;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Debugging;
using TerminalUI;
using TerminalUI.Color;
using TerminalUI.ConsoleDrivers;
using ITheme = FileTime.ConsoleUI.App.Styling.ITheme;
@@ -37,9 +39,9 @@ try
driver.SetCursorVisible(false);
var applicationContext = serviceProvider.GetRequiredService<IApplicationContext>();
var theme = serviceProvider.GetRequiredService<ITheme>();
var defaultTheme = serviceProvider.GetRequiredService<ITheme>();
applicationContext.Theme = theme.ConsoleTheme;
applicationContext.Theme = defaultTheme.ConsoleTheme;
var app = serviceProvider.GetRequiredService<IApplication>();
app.Run();
@@ -79,6 +81,7 @@ static IConfigurationRoot CreateConfiguration(string[] strings)
.AddInMemoryCollection(MainConsoleConfiguration.Configuration)
#if DEBUG
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile("appsettings.Local.json", optional: true, reloadOnChange: true)
#endif
;
@@ -103,7 +106,8 @@ static bool HandleInfoProviders(string[] args, IServiceProvider serviceProvider)
{
"--info=colors",
() => ColorSchema.PrintColorSchema(
serviceProvider.GetRequiredService<ITheme>(),
serviceProvider.GetRequiredService<IThemeProvider>(),
serviceProvider.GetRequiredService<IColorProvider>(),
serviceProvider.GetRequiredService<IConsoleDriver>()
)
},

View File

@@ -1,10 +1,15 @@
using FileTime.ConsoleUI.App.Configuration;
using FileTime.ConsoleUI.App.Styling;
using FileTime.ConsoleUI.Styles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using TerminalUI.Color;
using TerminalUI.ConsoleDrivers;
using TerminalUI.Styling;
using TerminalUI.Styling.Controls;
using IConsoleTheme = TerminalUI.Styling.ITheme;
using ConsoleTheme = TerminalUI.Styling.Theme;
using ITheme = FileTime.ConsoleUI.App.Styling.ITheme;
using Theme = FileTime.ConsoleUI.App.Styling.Theme;
namespace FileTime.ConsoleUI;
@@ -48,15 +53,54 @@ public static class Startup
public static IServiceCollection AddTheme(this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton<ITheme>(sp =>
{
var colorProvider = sp.GetRequiredService<IColorProvider>();
return new Theme(
DefaultForegroundColor: null,
DefaultForegroundAccentColor: colorProvider.RedForeground,
DefaultBackgroundColor: null,
ElementColor: colorProvider.GrayForeground,
ContainerColor: colorProvider.BlueForeground,
MarkedItemForegroundColor: colorProvider.YellowForeground,
MarkedItemBackgroundColor: null,
MarkedSelectedItemForegroundColor: colorProvider.BlackForeground,
MarkedSelectedItemBackgroundColor: colorProvider.YellowForeground,
SelectedItemColor: colorProvider.BlackForeground,
SelectedTabBackgroundColor: colorProvider.GreenBackground,
WarningForegroundColor: colorProvider.YellowForeground,
ErrorForegroundColor: colorProvider.RedForeground,
ListViewItemTheme: new(
SelectedBackgroundColor: colorProvider.GrayBackground,
SelectedForegroundColor: colorProvider.BlackForeground
),
ConsoleTheme: new ConsoleTheme
{
ControlThemes = new ControlThemes
{
ProgressBar = new ProgressBarTheme
{
ForegroundColor = colorProvider.BlueForeground,
BackgroundColor = colorProvider.GrayBackground,
UnfilledForeground = colorProvider.GrayForeground,
UnfilledBackground = colorProvider.GrayBackground,
}
}
}
);
});
serviceCollection.TryAddSingleton<IColorProvider>(sp =>
{
var driver = sp.GetRequiredService<IConsoleDriver>();
return driver switch
{
XTermDriver _ => DefaultThemes.Color256Theme,
DotnetDriver _ => DefaultThemes.ConsoleColorTheme,
XTermDriver _ => new AnsiColorProvider(),
DotnetDriver _ => new ConsoleColorProvider(),
_ => throw new ArgumentOutOfRangeException(nameof(driver))
};
});
return serviceCollection;