-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a console menu and a console app to test it
- Loading branch information
Showing
6 changed files
with
133 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters