From ee82603f2f6b5c71367d4e480cbef67e27fcc119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Fri, 7 Jan 2022 10:41:27 +0100 Subject: [PATCH] BasicColor fixes --- .../Application.CommandHandlers.cs | 2 +- src/FileTime.ConsoleUI.App/Application.cs | 5 ++++- .../UI/Color/ColoredConsoleRenderer.cs | 2 +- src/FileTime.ConsoleUI.App/UI/IStyles.cs | 2 ++ src/FileTime.ConsoleUI.App/UI/Render.cs | 4 ++-- src/FileTime.ConsoleUI.App/UI/Styles.cs | 17 ++++++++++++++--- src/FileTime.ConsoleUI/Program.cs | 12 +++++++++++- 7 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/FileTime.ConsoleUI.App/Application.CommandHandlers.cs b/src/FileTime.ConsoleUI.App/Application.CommandHandlers.cs index 925b5ce..fca57c2 100644 --- a/src/FileTime.ConsoleUI.App/Application.CommandHandlers.cs +++ b/src/FileTime.ConsoleUI.App/Application.CommandHandlers.cs @@ -169,7 +169,7 @@ namespace FileTime.ConsoleUI.App { if (_selectedPane!.CurrentLocation.IsExists(newPath)) { - _coloredConsoleRenderer.ForegroundColor = AnsiColor.From8bit(1); + _coloredConsoleRenderer.ForegroundColor = _styles.ErrorColor; } else { diff --git a/src/FileTime.ConsoleUI.App/Application.cs b/src/FileTime.ConsoleUI.App/Application.cs index d6d5db0..0f48187 100644 --- a/src/FileTime.ConsoleUI.App/Application.cs +++ b/src/FileTime.ConsoleUI.App/Application.cs @@ -25,6 +25,7 @@ namespace FileTime.ConsoleUI.App private readonly IColoredConsoleRenderer _coloredConsoleRenderer; private readonly CommandExecutor _commandExecutor; private readonly ConsoleReader _consoleReader; + private readonly IStyles _styles; private readonly List _previousKeys = new(); public bool IsRunning { get; private set; } = true; @@ -34,13 +35,15 @@ namespace FileTime.ConsoleUI.App IClipboard clipboard, IColoredConsoleRenderer coloredConsoleRenderer, CommandExecutor commandExecutor, - ConsoleReader consoleReader) + ConsoleReader consoleReader, + IStyles styles) { _serviceProvider = serviceProvider; _clipboard = clipboard; _coloredConsoleRenderer = coloredConsoleRenderer; _commandExecutor = commandExecutor; _consoleReader = consoleReader; + _styles = styles; InitCommandBindings(); } diff --git a/src/FileTime.ConsoleUI.App/UI/Color/ColoredConsoleRenderer.cs b/src/FileTime.ConsoleUI.App/UI/Color/ColoredConsoleRenderer.cs index 66e7fd9..cd5ee37 100644 --- a/src/FileTime.ConsoleUI.App/UI/Color/ColoredConsoleRenderer.cs +++ b/src/FileTime.ConsoleUI.App/UI/Color/ColoredConsoleRenderer.cs @@ -53,7 +53,7 @@ namespace FileTime.ConsoleUI.App.UI.Color } else if (BackgroundColor.GetType() != ForegroundColor.GetType()) { - throw new Exception($"Type of {nameof(BackgroundColor)} and {nameof(ForegroundColor)} must be the same."); + throw new Exception($"Type of {nameof(BackgroundColor)} ({BackgroundColor.GetType()}) and {nameof(ForegroundColor)} ({ForegroundColor.GetType()}) must be the same."); } else { diff --git a/src/FileTime.ConsoleUI.App/UI/IStyles.cs b/src/FileTime.ConsoleUI.App/UI/IStyles.cs index 8f179af..0833745 100644 --- a/src/FileTime.ConsoleUI.App/UI/IStyles.cs +++ b/src/FileTime.ConsoleUI.App/UI/IStyles.cs @@ -14,5 +14,7 @@ namespace FileTime.ConsoleUI.App.UI IConsoleColor? ElementSpecialForeground { get; } IConsoleColor? SelectedItemBackground { get; } IConsoleColor? SelectedItemForeground { get; } + IConsoleColor? ErrorColor { get; } + IConsoleColor? AccentForeground { get; } } } \ No newline at end of file diff --git a/src/FileTime.ConsoleUI.App/UI/Render.cs b/src/FileTime.ConsoleUI.App/UI/Render.cs index 93d20ca..7bc5fc6 100644 --- a/src/FileTime.ConsoleUI.App/UI/Render.cs +++ b/src/FileTime.ConsoleUI.App/UI/Render.cs @@ -133,13 +133,13 @@ namespace FileTime.ConsoleUI.App.UI { Console.SetCursorPosition(0, 0); _coloredRenderer.ResetColor(); - _coloredRenderer.ForegroundColor = AnsiColor.From8bit(2); + _coloredRenderer.ForegroundColor = _appStyle.AccentForeground; _coloredRenderer.Write(Environment.UserName + "@" + Environment.MachineName); _coloredRenderer.ResetColor(); _coloredRenderer.Write(' '); - _coloredRenderer.ForegroundColor = AnsiColor.From8bit(4); + _coloredRenderer.ForegroundColor = _appStyle.ContainerForeground; var path = Pane!.CurrentLocation.FullName + "/"; _coloredRenderer.Write(path); diff --git a/src/FileTime.ConsoleUI.App/UI/Styles.cs b/src/FileTime.ConsoleUI.App/UI/Styles.cs index 98c6f41..2160e36 100644 --- a/src/FileTime.ConsoleUI.App/UI/Styles.cs +++ b/src/FileTime.ConsoleUI.App/UI/Styles.cs @@ -15,6 +15,9 @@ namespace FileTime.ConsoleUI.App.UI public IConsoleColor? SelectedItemBackground { get; } public IConsoleColor? SelectedItemForeground { get; } + public IConsoleColor? ErrorColor { get; } + public IConsoleColor? AccentForeground { get; } + public Styles(bool useAnsiColors) { if (useAnsiColors) @@ -23,17 +26,25 @@ namespace FileTime.ConsoleUI.App.UI ElementForeground = AnsiColor.From8bit(14); ElementSpecialForeground = AnsiColor.From8bit(2); SelectedItemForeground = AnsiColor.From8bit(3); + ElementBackground = AnsiColor.From8bit(0); - DefaultForeground = ElementForeground; - SelectedItemBackground = ElementSpecialBackground = ContainerBackground = DefaultBackground = ElementBackground = AnsiColor.From8bit(0); + ErrorColor = AnsiColor.From8bit(1); + AccentForeground = AnsiColor.From8bit(2); } else { - ContainerBackground = new BasicColor(Console.BackgroundColor); ContainerForeground = new BasicColor(ConsoleColor.Blue); ElementBackground = new BasicColor(Console.BackgroundColor); ElementForeground = new BasicColor(Console.ForegroundColor); + ElementSpecialForeground = new BasicColor(ConsoleColor.DarkGreen); + SelectedItemForeground = new BasicColor(ConsoleColor.DarkCyan); + + ErrorColor = new BasicColor(ConsoleColor.Red); + AccentForeground = new BasicColor(ConsoleColor.Green); } + + DefaultForeground = ElementForeground; + SelectedItemBackground = ElementSpecialBackground = ContainerBackground = DefaultBackground = ElementBackground; } } } \ No newline at end of file diff --git a/src/FileTime.ConsoleUI/Program.cs b/src/FileTime.ConsoleUI/Program.cs index aba956a..b2edccf 100644 --- a/src/FileTime.ConsoleUI/Program.cs +++ b/src/FileTime.ConsoleUI/Program.cs @@ -87,12 +87,22 @@ namespace FileTime.ConsoleUI } } + private static bool IsAnsiColorSupported() + { + Console.CursorLeft = 0; + Console.CursorTop = 0; + + Console.Write("\u001b[0ma"); + + return Console.CursorLeft == 1 && Console.CursorTop == 0; + } + private static ServiceProvider CreateServiceProvider() { return new ServiceCollection() .AddLogging((builder) => builder.AddConsole().AddDebug()) .AddSingleton() - .AddSingleton(new Styles(true)) + .AddSingleton(new Styles(IsAnsiColorSupported())) .AddSingleton() .AddSingleton() .AddSingleton()