Skip to content

Commit

Permalink
Adding a console menu and a console app to test it
Browse files Browse the repository at this point in the history
  • Loading branch information
devedse committed May 27, 2019
1 parent a998dd1 commit 5181503
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 10 deletions.
12 changes: 12 additions & 0 deletions DeveCoolLib.ConsoleApp/DeveCoolLib.ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DeveCoolLib\DeveCoolLib.csproj" />
</ItemGroup>

</Project>
43 changes: 43 additions & 0 deletions DeveCoolLib.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using DeveCoolLib.DeveConsoleMenu;
using System;

namespace DeveCoolLib.ConsoleApp
{
public class Program
{

public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
TestConsoleMenu();
}

private static void TestConsoleMenu()
{
var running = true;
var menu = new ConsoleMenu(ConsoleMenuType.KeyPress);

menu.MenuOptions.Add(new ConsoleMenuOption("Do something", () => Console.WriteLine("This is the first option")));
menu.MenuOptions.Add(new ConsoleMenuOption("Do something else", () => Console.WriteLine("This is the second option")));
menu.MenuOptions.Add(new ConsoleMenuOption("Do something 3", () => Console.WriteLine("This is the third option")));
menu.MenuOptions.Add(new ConsoleMenuOption("Exit", () => running = false));

while (running)
{
menu.RenderMenu();
menu.WaitForResult();
Console.WriteLine();
Console.WriteLine();

if (menu.ConsoleMenuType == ConsoleMenuType.KeyPress)
{
menu.ConsoleMenuType = ConsoleMenuType.StringInput;
}
else
{
menu.ConsoleMenuType = ConsoleMenuType.KeyPress;
}
}
}
}
}
8 changes: 7 additions & 1 deletion DeveCoolLib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ VisualStudioVersion = 16.0.28803.452
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeveCoolLib", "DeveCoolLib\DeveCoolLib.csproj", "{CE935F89-DFCE-41E8-B8F6-F02248B25A82}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeveCoolLib.Tests", "DeveCoolLib.Tests\DeveCoolLib.Tests.csproj", "{8444821B-0C92-4704-B785-F565DC75F1B9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeveCoolLib.Tests", "DeveCoolLib.Tests\DeveCoolLib.Tests.csproj", "{8444821B-0C92-4704-B785-F565DC75F1B9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeveCoolLib.ConsoleApp", "DeveCoolLib.ConsoleApp\DeveCoolLib.ConsoleApp.csproj", "{1E2440B6-4F03-43FB-9C64-88EF274250A2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +23,10 @@ Global
{8444821B-0C92-4704-B785-F565DC75F1B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8444821B-0C92-4704-B785-F565DC75F1B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8444821B-0C92-4704-B785-F565DC75F1B9}.Release|Any CPU.Build.0 = Release|Any CPU
{1E2440B6-4F03-43FB-9C64-88EF274250A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E2440B6-4F03-43FB-9C64-88EF274250A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E2440B6-4F03-43FB-9C64-88EF274250A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E2440B6-4F03-43FB-9C64-88EF274250A2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
60 changes: 58 additions & 2 deletions DeveCoolLib/DeveConsoleMenu/ConsoleMenu.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,69 @@
namespace DeveCoolLib.DeveConsoleMenu
using System;
using System.Collections.Generic;

namespace DeveCoolLib.DeveConsoleMenu
{
public class ConsoleMenu
{
public ConsoleMenuType ConsoleMenuType { get; set; }
public int StartNumber { get; set; }
public List<ConsoleMenuOption> MenuOptions { get; set; } = new List<ConsoleMenuOption>();

public ConsoleMenu(ConsoleMenuType consoleMenuType)
public ConsoleMenu(ConsoleMenuType consoleMenuType, int startNumber = 1)
{
ConsoleMenuType = consoleMenuType;
StartNumber = startNumber;
}

public void RenderMenu()
{
Console.WriteLine("Choose any option:");

int maxLength = MenuOptions.Count.ToString().Length;

for (int i = 0; i < MenuOptions.Count; i++)
{
var option = MenuOptions[i];
Console.WriteLine($" {$"{i + StartNumber}:".PadRight(maxLength, ' ')} {option.Text}");
}
}

public void WaitForResult()
{
while (true)
{
Console.WriteLine();
Console.Write("Choose an option: ");

string input = "";
if (ConsoleMenuType == ConsoleMenuType.KeyPress)
{
input = Console.ReadKey().KeyChar.ToString();
Console.WriteLine();
}
else
{
input = Console.ReadLine();
}

if (int.TryParse(input, out int result))
{
int actualChoice = result - StartNumber;
if (actualChoice >= 0 && actualChoice < MenuOptions.Count)
{
ExecuteOption(actualChoice);
return;
}
}

Console.WriteLine("Input is not valid, please choose any of the provided options");
}
}

private void ExecuteOption(int id)
{
var option = MenuOptions[id];
option.ActionToExecute();
}
}
}
10 changes: 4 additions & 6 deletions DeveCoolLib/DeveConsoleMenu/ConsoleMenuOption.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DeveCoolLib.DeveConsoleMenu
{
public class ConsoleMenuOption
{
private readonly string _text;
private readonly Action _actionToExecute;
public string Text { get; }
public Action ActionToExecute { get; }

public ConsoleMenuOption(string text, Action actionToExecute)
{
_text = text;
_actionToExecute = actionToExecute;
Text = text;
ActionToExecute = actionToExecute;
}
}
}
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ WORKDIR /source
# caches restore result by copying csproj file separately
#COPY /NuGet.config /source/
COPY /DeveCoolLib/*.csproj /source/DeveCoolLib/
COPY /DeveCoolLib.ConsoleApp/*.csproj /source/DeveCoolLib.ConsoleApp/
COPY /DeveCoolLib.Tests/*.csproj /source/DeveCoolLib.Tests/
COPY /DeveCoolLib.sln /source/
RUN ls
Expand All @@ -13,4 +14,11 @@ RUN dotnet restore
# copies the rest of your code
COPY . .
RUN dotnet build --configuration Release
RUN dotnet test --configuration Release ./DeveCoolLib.Tests/DeveCoolLib.Tests.csproj
RUN dotnet test --configuration Release ./DeveCoolLib.Tests/DeveCoolLib.Tests.csproj
RUN dotnet publish ./DeveCoolLib.ConsoleApp/DeveCoolLib.ConsoleApp.csproj --output /app/ --configuration Release

# Stage 2
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-alpine3.9
WORKDIR /app
COPY --from=builder /app .
ENTRYPOINT ["dotnet", "DeveCoolLib.ConsoleApp.dll"]

0 comments on commit 5181503

Please sign in to comment.