Refactor styles

This commit is contained in:
2022-01-07 11:38:00 +01:00
parent e4dde1181c
commit ad75eb5ebb
13 changed files with 42 additions and 2 deletions

View File

@@ -1,25 +0,0 @@
namespace FileTime.ConsoleUI.App.UI.Color
{
public class AnsiColor : IConsoleColor
{
public string? Color { get; }
public string? Prefix { get; }
public string? Postfix { get; }
public AnsiColor() { }
public AnsiColor(string color, string preFix = "5;", string postFix = "m")
{
Color = color;
Prefix = preFix;
Postfix = postFix;
}
public static AnsiColor FromRgb(int r, int g, int b) => new($"{r};{g};{b}", "5;", "m");
public static AnsiColor From8bit(byte color) => new($"{color}", "5;", "m");
public override string? ToString()
{
return Color == null ? null : Prefix + Color + Postfix;
}
}
}

View File

@@ -1,14 +0,0 @@
namespace FileTime.ConsoleUI.App.UI.Color
{
public class BasicColor : IConsoleColor
{
public BasicColor() { }
public BasicColor(ConsoleColor color)
{
Color = color;
}
public ConsoleColor? Color { get; }
}
}

View File

@@ -1,78 +0,0 @@
namespace FileTime.ConsoleUI.App.UI.Color
{
public class ColoredConsoleRenderer : IColoredConsoleRenderer
{
private readonly IStyles _styles;
public IConsoleColor? BackgroundColor { get; set; }
public IConsoleColor? ForegroundColor { get; set; }
public ColoredConsoleRenderer(IStyles styles)
{
_styles = styles;
BackgroundColor = _styles.DefaultBackground;
ForegroundColor = _styles.DefaultForeground;
}
public void Write(string text) => DoWrite(text);
public void Write(char c) => DoWrite(c.ToString());
public void Write(string format, params object[] param) => DoWrite(string.Format(format, param));
private void DoWrite(string text)
{
if (BackgroundColor is AnsiColor ansiBackground && ForegroundColor is AnsiColor ansiForeground)
{
var formatting = "\u001b[0m";
if (ansiBackground.Color != null)
{
formatting += "\u001b[48;" + ansiBackground.ToString();
}
if (ansiForeground.Color != null)
{
formatting += "\u001b[38;" + ansiForeground.ToString();
}
Console.Write(formatting + text);
}
else if (BackgroundColor is BasicColor basicBackground && ForegroundColor is BasicColor basicForeground)
{
Console.BackgroundColor = basicBackground.Color ?? Console.BackgroundColor;
Console.ForegroundColor = basicForeground.Color ?? Console.ForegroundColor;
Console.Write(text);
}
else if (BackgroundColor == null && ForegroundColor == null)
{
Console.Write(text);
}
else if (BackgroundColor == null || ForegroundColor == null)
{
throw new Exception($"Either both of {nameof(BackgroundColor)} and {nameof(ForegroundColor)} must be null or neither of them.");
}
else if (BackgroundColor.GetType() != ForegroundColor.GetType())
{
throw new Exception($"Type of {nameof(BackgroundColor)} ({BackgroundColor.GetType()}) and {nameof(ForegroundColor)} ({ForegroundColor.GetType()}) must be the same.");
}
else
{
throw new Exception($"Unsupported color type: {BackgroundColor.GetType()}");
}
}
public void ResetColor()
{
BackgroundColor = _styles.DefaultBackground;
ForegroundColor = _styles.DefaultForeground;
Console.ResetColor();
}
public void Clear()
{
Console.SetCursorPosition(0, 0);
Write(new string(' ', Console.WindowHeight * Console.WindowWidth));
}
}
}

View File

@@ -1,14 +0,0 @@
namespace FileTime.ConsoleUI.App.UI.Color
{
public interface IColoredConsoleRenderer
{
IConsoleColor? BackgroundColor { get; set; }
IConsoleColor? ForegroundColor { get; set; }
void Clear();
void ResetColor();
void Write(string text);
void Write(char c);
void Write(string format, params object[] param);
}
}

View File

@@ -1,4 +0,0 @@
namespace FileTime.ConsoleUI.App.UI.Color
{
public interface IConsoleColor { }
}

View File

@@ -1,20 +0,0 @@
using FileTime.ConsoleUI.App.UI.Color;
namespace FileTime.ConsoleUI.App.UI
{
public interface IStyles
{
IConsoleColor? DefaultBackground { get; }
IConsoleColor? DefaultForeground { get; }
IConsoleColor? ContainerBackground { get; }
IConsoleColor? ContainerForeground { get; }
IConsoleColor? ElementBackground { get; }
IConsoleColor? ElementForeground { get; }
IConsoleColor? ElementSpecialBackground { get; }
IConsoleColor? ElementSpecialForeground { get; }
IConsoleColor? SelectedItemBackground { get; }
IConsoleColor? SelectedItemForeground { get; }
IConsoleColor? ErrorColor { get; }
IConsoleColor? AccentForeground { get; }
}
}

View File

@@ -232,8 +232,8 @@ namespace FileTime.ConsoleUI.App.UI
}
else
{
_coloredRenderer.BackgroundColor = new BasicColor(ConsoleColor.Red);
_coloredRenderer.ForegroundColor = new BasicColor(ConsoleColor.White);
_coloredRenderer.BackgroundColor = _appStyle.ErrorColor;
_coloredRenderer.ForegroundColor = _appStyle.ErrorInverseColor;
Console.SetCursorPosition(startX, startY + currentY++);
_coloredRenderer.Write($"{{0,-{elementWidth}}}", _paddingLeft + "<empty>" + _paddingRight);

View File

@@ -1,50 +0,0 @@
using FileTime.ConsoleUI.App.UI.Color;
namespace FileTime.ConsoleUI.App.UI
{
public class Styles : IStyles
{
public IConsoleColor? DefaultBackground { get; }
public IConsoleColor? DefaultForeground { get; }
public IConsoleColor? ContainerBackground { get; }
public IConsoleColor? ContainerForeground { get; }
public IConsoleColor? ElementBackground { get; }
public IConsoleColor? ElementForeground { get; }
public IConsoleColor? ElementSpecialBackground { get; }
public IConsoleColor? ElementSpecialForeground { get; }
public IConsoleColor? SelectedItemBackground { get; }
public IConsoleColor? SelectedItemForeground { get; }
public IConsoleColor? ErrorColor { get; }
public IConsoleColor? AccentForeground { get; }
public Styles(bool useAnsiColors)
{
if (useAnsiColors)
{
ContainerForeground = AnsiColor.From8bit(4);
ElementForeground = AnsiColor.From8bit(14);
ElementSpecialForeground = AnsiColor.From8bit(2);
SelectedItemForeground = AnsiColor.From8bit(3);
ElementBackground = AnsiColor.From8bit(0);
ErrorColor = AnsiColor.From8bit(1);
AccentForeground = AnsiColor.From8bit(2);
}
else
{
ContainerForeground = new BasicColor(ConsoleColor.Blue);
ElementBackground = new BasicColor(Console.BackgroundColor);
ElementForeground = new BasicColor(Console.ForegroundColor);
ElementSpecialForeground = new BasicColor(ConsoleColor.DarkGreen);
SelectedItemForeground = new BasicColor(ConsoleColor.DarkYellow);
ErrorColor = new BasicColor(ConsoleColor.Red);
AccentForeground = new BasicColor(ConsoleColor.Green);
}
DefaultForeground = ElementForeground;
SelectedItemBackground = ElementSpecialBackground = ContainerBackground = DefaultBackground = ElementBackground;
}
}
}