From 98fa812abf2682161754811810722653329fda53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Tue, 10 Sep 2024 08:42:44 +0200 Subject: [PATCH] Add default background and foreground color as possible colors --- .editorconfig | 7 +++- src/ConsoleApp/FileTime.ConsoleUI/Startup.cs | 4 +- .../TerminalUI/Color/AnsiColorProvider.cs | 4 +- src/Library/TerminalUI/Color/Color256.cs | 37 ++++++++++++++++--- .../TerminalUI/Color/ColorProviderBase.cs | 4 +- .../TerminalUI/Color/ConsoleColorProvider.cs | 4 +- .../TerminalUI/Color/IColorProvider.cs | 5 ++- 7 files changed, 51 insertions(+), 14 deletions(-) diff --git a/.editorconfig b/.editorconfig index 4f1f229..bd152a2 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,3 +1,8 @@ +[*] +tab_width = 4 +indent_size = 4 +indent_style = space + [*.cs] dotnet_diagnostic.RCS1196.severity = none csharp_style_namespace_declarations = file_scoped:error @@ -5,4 +10,4 @@ dotnet_diagnostic.IDE0161.severity = error csharp_style_var_for_built_in_types = true:warning csharp_style_var_when_type_is_apparent = true:warning csharp_style_var_elsewhere = true:warning -csharp_style_expression_bodied_methods = true:warning \ No newline at end of file +csharp_style_expression_bodied_methods = true:warning diff --git a/src/ConsoleApp/FileTime.ConsoleUI/Startup.cs b/src/ConsoleApp/FileTime.ConsoleUI/Startup.cs index 5a5326a..11b802f 100644 --- a/src/ConsoleApp/FileTime.ConsoleUI/Startup.cs +++ b/src/ConsoleApp/FileTime.ConsoleUI/Startup.cs @@ -60,7 +60,7 @@ public static class Startup DefaultForegroundColor: null, DefaultForegroundAccentColor: colorProvider.RedForeground, DefaultBackgroundColor: null, - ElementColor: colorProvider.GrayForeground, + ElementColor: colorProvider.DefaultForeground, ContainerColor: colorProvider.BlueForeground, MarkedItemForegroundColor: colorProvider.YellowForeground, MarkedItemBackgroundColor: null, @@ -105,4 +105,4 @@ public static class Startup return serviceCollection; } -} \ No newline at end of file +} diff --git a/src/Library/TerminalUI/Color/AnsiColorProvider.cs b/src/Library/TerminalUI/Color/AnsiColorProvider.cs index 7255218..2b7c2b4 100644 --- a/src/Library/TerminalUI/Color/AnsiColorProvider.cs +++ b/src/Library/TerminalUI/Color/AnsiColorProvider.cs @@ -25,6 +25,7 @@ public class AnsiColorProvider : ColorProviderBase public override IColor FromRgb(Rgb rgb, ColorType type) => new ColorRgb(rgb.R, rgb.G, rgb.B, type); + public override IColor DefaultForeground { get; } = Color256.DefaultForeground; public override IColor BlackForeground { get; } = new Color256(0, ColorType.Foreground); public override IColor BlueForeground { get; } = new Color256(12, ColorType.Foreground); public override IColor CyanForeground { get; } = new Color256(14, ColorType.Foreground); @@ -42,6 +43,7 @@ public class AnsiColorProvider : ColorProviderBase public override IColor WhiteForeground { get; } = new Color256(15, ColorType.Foreground); public override IColor YellowForeground { get; } = new Color256(11, ColorType.Foreground); + public override IColor DefaultBackground { get; } = Color256.DefaultBackground; public override IColor BlackBackground { get; } = new Color256(0, ColorType.Background); public override IColor BlueBackground { get; } = new Color256(12, ColorType.Background); public override IColor CyanBackground { get; } = new Color256(14, ColorType.Background); @@ -58,4 +60,4 @@ public class AnsiColorProvider : ColorProviderBase public override IColor RedBackground { get; } = new Color256(9, ColorType.Background); public override IColor WhiteBackground { get; } = new Color256(15, ColorType.Background); public override IColor YellowBackground { get; } = new Color256(11, ColorType.Background); -} \ No newline at end of file +} diff --git a/src/Library/TerminalUI/Color/Color256.cs b/src/Library/TerminalUI/Color/Color256.cs index b56094e..805e112 100644 --- a/src/Library/TerminalUI/Color/Color256.cs +++ b/src/Library/TerminalUI/Color/Color256.cs @@ -2,17 +2,42 @@ namespace TerminalUI.Color; -public readonly record struct Color256(byte Color, ColorType Type) : IColor +public readonly record struct Color256(byte? Color, ColorType Type) : IColor { public string ToConsoleColor() - => Type switch + { + if (Color is null) + { + return string.Empty; + } + return Type switch { ColorType.Foreground => $"\x1b[38;5;{Color}m", ColorType.Background => $"\x1b[48;5;{Color}m", - _ => throw new InvalidEnumArgumentException(nameof(Type), (int) Type, typeof(ColorType)) + _ => throw new InvalidEnumArgumentException(nameof(Type), (int)Type, typeof(ColorType)) }; + } - public IColor AsForeground() => this with {Type = ColorType.Foreground}; + public static Color256 DefaultForeground { get; } = new(null, ColorType.Foreground); + public static Color256 DefaultBackground { get; } = new(null, ColorType.Background); + private static Color256 DefaultForegroundAsBackground { get; } = new(7, ColorType.Background); + private static Color256 DefaultBackgroundAsForeground { get; } = new(0, ColorType.Foreground); - public IColor AsBackground() => this with {Type = ColorType.Background}; -} \ No newline at end of file + private static Dictionary _defaultColorMapping = new() + { + { DefaultForeground, DefaultForegroundAsBackground }, + { DefaultBackground, DefaultBackgroundAsForeground }, + { DefaultForegroundAsBackground, DefaultForeground }, + { DefaultBackgroundAsForeground, DefaultBackground }, + }; + + public IColor AsForeground() + => _defaultColorMapping.TryGetValue(this, out var color) + ? color + : this with { Type = ColorType.Foreground }; + + public IColor AsBackground() + => _defaultColorMapping.TryGetValue(this, out var color) + ? color + : this with { Type = ColorType.Background }; +} diff --git a/src/Library/TerminalUI/Color/ColorProviderBase.cs b/src/Library/TerminalUI/Color/ColorProviderBase.cs index 073f4f5..ea229c2 100644 --- a/src/Library/TerminalUI/Color/ColorProviderBase.cs +++ b/src/Library/TerminalUI/Color/ColorProviderBase.cs @@ -82,6 +82,7 @@ public abstract class ColorProviderBase : IColorProvider _ => null }; + public abstract IColor DefaultForeground { get; } public abstract IColor BlackForeground { get; } public abstract IColor BlueForeground { get; } public abstract IColor CyanForeground { get; } @@ -99,6 +100,7 @@ public abstract class ColorProviderBase : IColorProvider public abstract IColor WhiteForeground { get; } public abstract IColor YellowForeground { get; } + public abstract IColor DefaultBackground { get; } public abstract IColor BlackBackground { get; } public abstract IColor BlueBackground { get; } public abstract IColor CyanBackground { get; } @@ -115,4 +117,4 @@ public abstract class ColorProviderBase : IColorProvider public abstract IColor RedBackground { get; } public abstract IColor WhiteBackground { get; } public abstract IColor YellowBackground { get; } -} \ No newline at end of file +} diff --git a/src/Library/TerminalUI/Color/ConsoleColorProvider.cs b/src/Library/TerminalUI/Color/ConsoleColorProvider.cs index 43ae8ff..1401bfa 100644 --- a/src/Library/TerminalUI/Color/ConsoleColorProvider.cs +++ b/src/Library/TerminalUI/Color/ConsoleColorProvider.cs @@ -73,6 +73,7 @@ public class ConsoleColorProvider : ColorProviderBase protected IColor ApproximateColor(Rgb rgb, IEnumerable<(Rgb, IColor)> colors) => colors.MinBy(c => c.Item1 - rgb).Item2; + public override IColor DefaultForeground => _blackForeground; public override IColor BlackForeground => _blackForeground; public override IColor BlueForeground => _blueForeground; public override IColor CyanForeground => _cyanForeground; @@ -90,6 +91,7 @@ public class ConsoleColorProvider : ColorProviderBase public override IColor WhiteForeground => _whiteForeground; public override IColor YellowForeground => _yellowForeground; + public override IColor DefaultBackground => _blackBackground; public override IColor BlackBackground => _blackBackground; public override IColor BlueBackground => _blueBackground; public override IColor CyanBackground => _cyanBackground; @@ -140,4 +142,4 @@ public class ConsoleColorProvider : ColorProviderBase private static readonly IColor _redBackground = new ConsoleColor(System.ConsoleColor.Red, ColorType.Background); private static readonly IColor _whiteBackground = new ConsoleColor(System.ConsoleColor.White, ColorType.Background); private static readonly IColor _yellowBackground = new ConsoleColor(System.ConsoleColor.Yellow, ColorType.Background); -} \ No newline at end of file +} diff --git a/src/Library/TerminalUI/Color/IColorProvider.cs b/src/Library/TerminalUI/Color/IColorProvider.cs index ed11819..ce7e008 100644 --- a/src/Library/TerminalUI/Color/IColorProvider.cs +++ b/src/Library/TerminalUI/Color/IColorProvider.cs @@ -5,6 +5,7 @@ public interface IColorProvider IColor Parse(string color, ColorType type); IColor FromRgb(Rgb rgb, ColorType type); + IColor DefaultForeground { get; } IColor BlackForeground { get; } IColor BlueForeground { get; } IColor CyanForeground { get; } @@ -21,8 +22,8 @@ public interface IColorProvider IColor RedForeground { get; } IColor WhiteForeground { get; } IColor YellowForeground { get; } - + IColor DefaultBackground { get; } IColor BlackBackground { get; } IColor BlueBackground { get; } IColor CyanBackground { get; } @@ -39,4 +40,4 @@ public interface IColorProvider IColor RedBackground { get; } IColor WhiteBackground { get; } IColor YellowBackground { get; } -} \ No newline at end of file +}