Skip to content

Commit 0911c30

Browse files
committed
Abstracted the consoleService so it can be used for other UI frameworks.
Will need to consider turning all the helpers into services similarly in the near future.
1 parent 7988caf commit 0911c30

16 files changed

+110
-25
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\ConsoleHero\ConsoleHero.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

ConsoleHero.Web/GlobalUsings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global using static System.Console;
2+
global using System.Drawing;
3+
global using ConsoleHero.Model;

ConsoleHero.Web/Program.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ConsoleHero.Web;
2+
3+
internal static class Program
4+
{
5+
static void Main()
6+
{
7+
WriteLine("Hello, World!");
8+
}
9+
}

ConsoleHero.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleHero", "ConsoleHero\
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleHero.Test", "ConsoleHero.Test\ConsoleHero.Test.csproj", "{C450F83F-7827-402D-8F20-D84C0406C87A}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleHero.Web", "ConsoleHero.Web\ConsoleHero.Web.csproj", "{3C987FA4-6121-48AA-873D-C092EB514205}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{C450F83F-7827-402D-8F20-D84C0406C87A}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{C450F83F-7827-402D-8F20-D84C0406C87A}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{C450F83F-7827-402D-8F20-D84C0406C87A}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{3C987FA4-6121-48AA-873D-C092EB514205}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{3C987FA4-6121-48AA-873D-C092EB514205}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{3C987FA4-6121-48AA-873D-C092EB514205}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{3C987FA4-6121-48AA-873D-C092EB514205}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

ConsoleHero/ConsoleHero.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net8.0;net7.0;</TargetFrameworks>
4+
<TargetFrameworks>net9.0;net8.0;net7.0;</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
99
<PackageId>ConsoleHero</PackageId>
10-
<Version>0.2.1</Version>
10+
<Version>0.3.0</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: 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;
5+
6+
internal class ConsoleService : IConsoleService
7+
{
8+
public void Clear() => Console.Clear();
9+
public string? ReadLine() => Console.ReadLine();
10+
public void Write(string? value) => Console.Write(value);
11+
public void WriteLine(string? value) => Console.WriteLine(value);
12+
public void WriteLine() => Console.WriteLine();
13+
public ConsoleKeyInfo ReadKey() => Console.ReadKey();
14+
public void Beep(int frequency, int duration)
15+
{
16+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
17+
Console.Beep(frequency, duration);
18+
}
19+
}

ConsoleHero/GlobalSettings.cs

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

34
namespace ConsoleHero;
45

@@ -16,4 +17,20 @@ public static class GlobalSettings
1617
/// How many line breaks between menues or paragraphs. Set to 1 by default.
1718
/// </summary>
1819
public static int Spacing { get; set; } = 1;
20+
21+
/// <summary>
22+
/// By default will use the standard Console. If you want to adapt this to other output sources,
23+
/// create a new <see cref="IConsoleService"/> and set this GlobalSetting.
24+
/// </summary>
25+
public static IConsoleService Service
26+
{
27+
get
28+
{
29+
_service ??= new ConsoleService();
30+
return _service;
31+
}
32+
set => _service = value;
33+
}
34+
35+
private static IConsoleService? _service;
1936
}

ConsoleHero/GlobalUsings.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
global using static System.Console;
2-
global using System.Drawing;
1+
global using System.Drawing;
32
global using ConsoleHero.Model;

ConsoleHero/Helpers/BeepHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ internal class BeepHelper : IBeepHelper
88

99
public BeepHelper(IPlatformHelper plateformHelper) => _platformHelper = plateformHelper;
1010

11-
void IBeepHelper.Beep() => Write("\a");
11+
void IBeepHelper.Beep() => GlobalSettings.Service.Write("\a");
1212

1313
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "Ensured by _platformHelper")]
1414
void IBeepHelper.Beep(int frequency, int duration)
1515
{
1616
if (_platformHelper.IsWindows)
1717
{
18-
Beep(frequency, duration);
18+
GlobalSettings.Service.Beep(frequency, duration);
1919
}
2020
else if (_platformHelper.IsLinux || _platformHelper.IsOSX)
2121
{
@@ -34,7 +34,7 @@ void IBeepHelper.Beep(int frequency, int duration)
3434
}
3535
catch
3636
{
37-
Write("\a");
37+
GlobalSettings.Service.Write("\a");
3838
}
3939
}
4040
}

ConsoleHero/Helpers/ColorHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal static class ColorHelper
2727
_ => Color.Black,
2828
};
2929

30-
private static void SetRgbTextColor(byte r, byte g, byte b) => Write($"\u001b[38;2;{r};{g};{b}m");
30+
private static void SetRgbTextColor(byte r, byte g, byte b) => GlobalSettings.Service.Write($"\u001b[38;2;{r};{g};{b}m");
3131

3232
//internal static void ResetColor() => Write("\u001b[0m");
3333

0 commit comments

Comments
 (0)