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

@@ -0,0 +1,7 @@
namespace FileTime.ConsoleUI.App;
public interface IColorSampleProvider
{
public Type? ForegroundColors { get; }
public Type? BackgroundColors { get; }
}

View File

@@ -1,5 +1,4 @@
using TerminalUI.Color;
using TerminalUI.Models;
namespace FileTime.ConsoleUI.App;
@@ -10,4 +9,5 @@ public interface ITheme
IColor? ElementColor { get; }
IColor? ContainerColor { get; }
IColor? MarkedItemColor { get; }
IColor? SelectedTabBackgroundColor { get; }
}

View File

@@ -4,5 +4,5 @@ namespace FileTime.ConsoleUI.App.KeyInputHandling;
public interface IKeyInputHandlerService
{
void HandleKeyInput(GeneralKeyEventArgs keyEvent);
void HandleKeyInput(GeneralKeyEventArgs keyEvent, SpecialKeysStatus specialKeysStatus);
}

View File

@@ -50,7 +50,7 @@ public class App : IApplication
((INotifyCollectionChanged) _appState.Tabs).CollectionChanged += (_, _) =>
{
if(_appState.Tabs.Count == 0)
if (_appState.Tabs.Count == 0)
_applicationContext.IsRunning = false;
};
@@ -68,13 +68,21 @@ public class App : IApplication
if (_consoleDriver.CanRead())
{
var key = _consoleDriver.ReadKey();
if (_appKeyService.MapKey(key.Key) is { } mappedKey)
{
SpecialKeysStatus specialKeysStatus = new(
(key.Modifiers & ConsoleModifiers.Alt) != 0,
(key.Modifiers & ConsoleModifiers.Shift) != 0,
(key.Modifiers & ConsoleModifiers.Control) != 0
);
var keyEventArgs = new GeneralKeyEventArgs
{
Key = mappedKey
};
_keyInputHandlerService.HandleKeyInput(keyEventArgs);
_keyInputHandlerService.HandleKeyInput(keyEventArgs, specialKeysStatus);
}
}

View File

@@ -10,9 +10,6 @@ public class KeyInputHandlerService : IKeyInputHandlerService
private readonly IAppState _appState;
private readonly IDefaultModeKeyInputHandler _defaultModeKeyInputHandler;
private readonly IRapidTravelModeKeyInputHandler _rapidTravelModeKeyInputHandler;
bool _isCtrlPressed = false;
bool _isShiftPressed = false;
bool _isAltPressed = false;
public KeyInputHandlerService(
IAppState appState,
@@ -24,9 +21,8 @@ public class KeyInputHandlerService : IKeyInputHandlerService
_rapidTravelModeKeyInputHandler = rapidTravelModeKeyInputHandler;
}
public void HandleKeyInput(GeneralKeyEventArgs keyEvent)
public void HandleKeyInput(GeneralKeyEventArgs keyEvent, SpecialKeysStatus specialKeysStatus)
{
var specialKeysStatus = new SpecialKeysStatus(_isAltPressed, _isShiftPressed, _isCtrlPressed);
if (_appState.ViewMode.Value == ViewMode.Default)
{
Task.Run(async () => await _defaultModeKeyInputHandler.HandleInputKey(keyEvent, specialKeysStatus)).Wait();

View File

@@ -6,6 +6,7 @@ using TerminalUI;
using TerminalUI.Color;
using TerminalUI.Controls;
using TerminalUI.Extensions;
using TerminalUI.Models;
using TerminalUI.ViewExtensions;
using ConsoleColor = TerminalUI.Color.ConsoleColor;
@@ -38,15 +39,23 @@ public class MainWindow
RowDefinitionsObject = "Auto *",
ChildInitializer =
{
new TextBlock<IAppState>()
.Setup(t =>
t.Bind(
t,
appState => appState.SelectedTab.Value.CurrentLocation.Value.FullName.Path,
tb => tb.Text,
value => value
)
),
new Grid<IAppState>
{
ColumnDefinitionsObject = "* Auto",
ChildInitializer =
{
new TextBlock<IAppState>()
.Setup(t =>
t.Bind(
t,
appState => appState.SelectedTab.Value.CurrentLocation.Value.FullName.Path,
tb => tb.Text,
value => value
)
),
TabControl()
}
},
new Grid<IAppState>
{
ColumnDefinitionsObject = "* 4* 4*",
@@ -66,6 +75,44 @@ public class MainWindow
_root = root;
}
private IView<IAppState> TabControl()
{
var tabList = new ListView<IAppState, ITabViewModel>
{
Orientation = Orientation.Horizontal,
Extensions =
{
new GridPositionExtension(1, 0)
},
ItemTemplate = item =>
{
var textBlock = item.CreateChild<TextBlock<ITabViewModel>>();
textBlock.Foreground = _theme.DefaultForegroundColor;
textBlock.Bind(
textBlock,
dc => dc.TabNumber.ToString(),
tb => tb.Text,
fallbackValue: "?");
textBlock.Bind(
textBlock,
dc => dc.IsSelected.Value ? _theme.SelectedTabBackgroundColor : null,
tb => tb.Background,
fallbackValue: null
);
return textBlock;
}
};
tabList.Bind(
tabList,
appState => appState == null ? null : appState.Tabs,
v => v.ItemsSource);
return tabList;
}
private ListView<IAppState, IItemViewModel> SelectedItemsView()
{
var list = new ListView<IAppState, IItemViewModel>

View File

@@ -8,22 +8,32 @@ public record Theme(
IColor? DefaultBackgroundColor,
IColor? ElementColor,
IColor? ContainerColor,
IColor? MarkedItemColor) : ITheme;
IColor? MarkedItemColor,
IColor? SelectedTabBackgroundColor,
Type? ForegroundColors,
Type? BackgroundColors) : ITheme, IColorSampleProvider;
public static class DefaultThemes
{
public static Theme Color256Theme => new(
DefaultForegroundColor: Color256Colors.Foregrounds.Gray,
DefaultBackgroundColor: Color256Colors.Foregrounds.Black,
DefaultBackgroundColor: null,
ElementColor: Color256Colors.Foregrounds.Gray,
ContainerColor: Color256Colors.Foregrounds.Blue,
MarkedItemColor: Color256Colors.Foregrounds.Black
MarkedItemColor: Color256Colors.Foregrounds.Black,
SelectedTabBackgroundColor: Color256Colors.Backgrounds.Green,
ForegroundColors: typeof(Color256Colors.Foregrounds),
BackgroundColors: typeof(Color256Colors.Backgrounds)
);
public static Theme ConsoleColorTheme => new(
DefaultForegroundColor: ConsoleColors.Foregrounds.Gray,
DefaultBackgroundColor: ConsoleColors.Foregrounds.Black,
DefaultBackgroundColor: ConsoleColors.Backgrounds.Black,
ElementColor: ConsoleColors.Foregrounds.Gray,
ContainerColor: ConsoleColors.Foregrounds.Blue,
MarkedItemColor: ConsoleColors.Foregrounds.Black);
MarkedItemColor: ConsoleColors.Foregrounds.Black,
SelectedTabBackgroundColor: ConsoleColors.Backgrounds.Green,
ForegroundColors: typeof(ConsoleColors.Foregrounds),
BackgroundColors: typeof(ConsoleColors.Backgrounds)
);
}

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();