Skip to content

Commit ac2a9bb

Browse files
committed
Abstract ColorService
1 parent ff5a672 commit ac2a9bb

File tree

9 files changed

+75
-43
lines changed

9 files changed

+75
-43
lines changed

ConsoleHero.Test/ColorHelperTests.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ConsoleHero.Helpers;
1+
using ConsoleHero.Interfaces;
2+
using ConsoleHero.Services;
23

34
namespace ConsoleHero.Test;
45

@@ -20,9 +21,10 @@ public void TearDown()
2021
[TestMethod]
2122
public void SetTextColor_WithColor_WritesExpectedAnsiCode()
2223
{
24+
IColorService colorService = new ColorService();
2325
Color color = Color.FromArgb(255, 0, 0);
2426

25-
ColorHelper.SetTextColor(color);
27+
colorService.SetTextColor(color);
2628

2729
const string expectedOutput = "\u001b[38;2;255;0;0m";
2830
Assert.AreEqual(expectedOutput, _consoleOutput.ToString());
@@ -51,7 +53,9 @@ public void SetTextColor_WithConsoleColor_WritesExpectedAnsiCode(
5153
int expectedG,
5254
int expectedB)
5355
{
54-
ColorHelper.SetTextColor(consoleColor);
56+
IColorService colorService = new ColorService();
57+
58+
colorService.SetTextColor(consoleColor);
5559

5660
string expectedOutput = $"\u001b[38;2;{expectedR};{expectedG};{expectedB}m";
5761
Assert.AreEqual(expectedOutput, _consoleOutput.ToString());
@@ -62,17 +66,18 @@ public void ConsoleColorToDrawingColor_ReturnsExpectedColor()
6266
{
6367
const ConsoleColor consoleColor = ConsoleColor.Green;
6468

65-
Color color = ColorHelper.ConsoleColorToDrawingColor(consoleColor);
69+
Color color = IColorService.ConsoleColorToDrawingColor(consoleColor);
6670

6771
Assert.AreEqual(Color.Green, color);
6872
}
6973

7074
[TestMethod]
7175
public void SetToDefault_SetsGlobalDefaultTextColor()
7276
{
77+
IColorService colorService = new ColorService();
7378
GlobalSettings.DefaultTextColor = Color.FromArgb(0, 255, 255);
7479

75-
ColorHelper.SetToDefault();
80+
colorService.SetToDefault();
7681

7782
const string expectedOutput = "\u001b[38;2;0;255;255m";
7883
Assert.AreEqual(expectedOutput, _consoleOutput.ToString());

ConsoleHero/ColorText.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ConsoleHero.Helpers;
1+
using ConsoleHero.Interfaces;
22

33
namespace ConsoleHero;
44

@@ -24,7 +24,7 @@ internal ColorText(string text, Color? color = null)
2424
internal ColorText(string text, ConsoleColor color)
2525
{
2626
Text = text;
27-
_color = ColorHelper.ConsoleColorToDrawingColor(color);
27+
_color = IColorService.ConsoleColorToDrawingColor(color);
2828
}
2929

3030
internal string Text { get; } = string.Empty;

ConsoleHero/ConsoleHero.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
99
<PackageId>ConsoleHero</PackageId>
10-
<Version>0.3.0</Version>
10+
<Version>0.3.1</Version>
1111
<Authors>Derek Gooding</Authors>
1212
<Description>A library for making quick menus in a console application.</Description>
1313
<PackageTags>Console;FluentAPI;Menu;CLI;ConsoleApp;CSharp;UserInterface;MenuNavigation;Scalable;ConsoleMenu;Fluent;</PackageTags>

ConsoleHero/ConsoleService.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

ConsoleHero/GlobalSettings.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using ConsoleHero.Helpers;
2-
using ConsoleHero.Interfaces;
1+
using ConsoleHero.Interfaces;
2+
using ConsoleHero.Services;
33

44
namespace ConsoleHero;
55

@@ -11,7 +11,7 @@ public static class GlobalSettings
1111
/// <summary>
1212
/// This is the default color all text will be. If unset, will be White.
1313
/// </summary>
14-
public static Color DefaultTextColor { get; set; } = ColorHelper.ConsoleColorToDrawingColor(ConsoleColor.White);
14+
public static Color DefaultTextColor { get; set; } = IColorService.ConsoleColorToDrawingColor(ConsoleColor.White);
1515

1616
/// <summary>
1717
/// How many line breaks between menues or paragraphs. Set to 1 by default.
@@ -32,5 +32,16 @@ public static IConsoleService Service
3232
set => _service = value;
3333
}
3434

35+
public static IColorService ColorService
36+
{
37+
get
38+
{
39+
_colorService ??= new ColorService();
40+
return _colorService;
41+
}
42+
set => _colorService = value;
43+
}
44+
3545
private static IConsoleService? _service;
46+
private static IColorService? _colorService;
3647
}

ConsoleHero/Helpers/ColorHelper.cs renamed to ConsoleHero/Interfaces/IColorService.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
namespace ConsoleHero.Helpers;
1+
namespace ConsoleHero.Interfaces;
22

3-
internal static class ColorHelper
3+
public interface IColorService
44
{
5-
internal static void SetTextColor(Color color) => SetRgbTextColor(color.R, color.G, color.B);
6-
7-
internal static void SetTextColor(ConsoleColor consoleColor) => SetTextColor(ConsoleColorToDrawingColor(consoleColor));
8-
9-
internal static Color ConsoleColorToDrawingColor(ConsoleColor consoleColor) => consoleColor switch
5+
public static Color ConsoleColorToDrawingColor(ConsoleColor consoleColor) => consoleColor switch
106
{
117
ConsoleColor.Black => Color.Black,
128
ConsoleColor.DarkBlue => Color.FromArgb(0, 0, 139),
@@ -27,9 +23,11 @@ internal static class ColorHelper
2723
_ => Color.Black,
2824
};
2925

30-
private static void SetRgbTextColor(byte r, byte g, byte b) => GlobalSettings.Service.Write($"\u001b[38;2;{r};{g};{b}m");
26+
public abstract void SetTextColor(Color color);
27+
28+
public abstract void SetTextColor(ConsoleColor consoleColor);
3129

32-
//internal static void ResetColor() => Write("\u001b[0m");
30+
public abstract void SetTextColor(byte r, byte g, byte b);
3331

34-
internal static void SetToDefault() => SetTextColor(GlobalSettings.DefaultTextColor);
32+
public abstract void SetToDefault();
3533
}

ConsoleHero/ListExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ConsoleHero.Helpers;
1+
using ConsoleHero.Services;
22

33
namespace ConsoleHero;
44

@@ -108,7 +108,7 @@ internal static void Print(this IList<ParagraphLine> list, string input = "")
108108
{
109109
foreach (ILineComponent component in line.Components)
110110
{
111-
ColorHelper.SetTextColor(component.Color);
111+
GlobalSettings.ColorService.SetTextColor(component.Color);
112112
if (component is ColorText c)
113113
GlobalSettings.Service.Write(c.Text);
114114
else if (component is InputPlaceholder)
@@ -121,6 +121,6 @@ internal static void Print(this IList<ParagraphLine> list, string input = "")
121121
for (int i = 0; i < GlobalSettings.Spacing; i++)
122122
GlobalSettings.Service.WriteLine();
123123

124-
ColorHelper.SetToDefault();
124+
GlobalSettings.ColorService.SetToDefault();
125125
}
126126
}

ConsoleHero/Services/ColorService.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using ConsoleHero.Interfaces;
2+
3+
namespace ConsoleHero.Services;
4+
5+
internal class ColorService : IColorService
6+
{
7+
void IColorService.SetTextColor(Color color)
8+
=> GlobalSettings.ColorService.SetTextColor(color.R, color.G, color.B);
9+
10+
void IColorService.SetTextColor(ConsoleColor consoleColor)
11+
=> GlobalSettings.ColorService.SetTextColor(IColorService.ConsoleColorToDrawingColor(consoleColor));
12+
13+
void IColorService.SetTextColor(byte r, byte g, byte b)
14+
=> GlobalSettings.Service.Write($"\u001b[38;2;{r};{g};{b}m");
15+
16+
void IColorService.SetToDefault() =>
17+
GlobalSettings.ColorService.SetTextColor(GlobalSettings.DefaultTextColor);
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using ConsoleHero.Interfaces;
2+
using System.Runtime.InteropServices;
3+
4+
namespace ConsoleHero.Services;
5+
6+
internal class ConsoleService : IConsoleService
7+
{
8+
void IConsoleService.Clear() => Console.Clear();
9+
string? IConsoleService.ReadLine() => Console.ReadLine();
10+
void IConsoleService.Write(string? value) => Console.Write(value);
11+
void IConsoleService.WriteLine(string? value) => Console.WriteLine(value);
12+
void IConsoleService.WriteLine() => Console.WriteLine();
13+
ConsoleKeyInfo IConsoleService.ReadKey() => Console.ReadKey();
14+
void IConsoleService.Beep(int frequency, int duration)
15+
{
16+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
17+
Console.Beep(frequency, duration);
18+
}
19+
}

0 commit comments

Comments
 (0)