From 8e5355ffb489056f7afa07848090cc7865302f9d Mon Sep 17 00:00:00 2001 From: ramduq Date: Wed, 26 Feb 2020 14:00:24 -0500 Subject: [PATCH 1/3] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 49c3016..41104c6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +forked from splttingatms/EasyConsole + + # EasyConsole [![NuGet](https://img.shields.io/nuget/v/EasyConsole.svg)](https://www.nuget.org/packages/EasyConsole/) EasyConsole is a library to make it easier for developers to build a simple menu interface for a .NET console application. From 72e3f7792c7a554f593e8c68746773c063bb26d7 Mon Sep 17 00:00:00 2001 From: ramduq Date: Wed, 26 Feb 2020 22:28:37 -0500 Subject: [PATCH 2/3] Added input methods and support for colors Added Input methods to read other types: DateTime, double, boolean. Added a Settings class in which colors can be set for the title and for user input. --- Demo/Pages/InputPage.cs | 10 +++- Demo/Runner.cs | 9 +++- EasyConsole/EasyConsole.csproj | 5 +- EasyConsole/Input.cs | 90 ++++++++++++++++++++++++++++++++-- EasyConsole/Menu.cs | 2 + EasyConsole/Page.cs | 7 ++- EasyConsole/Settings.cs | 13 +++++ 7 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 EasyConsole/Settings.cs diff --git a/Demo/Pages/InputPage.cs b/Demo/Pages/InputPage.cs index f23174f..32fa5ac 100644 --- a/Demo/Pages/InputPage.cs +++ b/Demo/Pages/InputPage.cs @@ -14,8 +14,14 @@ public override void Display() { base.Display(); - Fruit input = Input.ReadEnum("Select a fruit"); - Output.WriteLine(ConsoleColor.Green, "You selected {0}", input); + //var intInput = Input.ReadInt("Enter integer:", 0, 1000000); + var strInput = Input.ReadString("Enter text:"); + var floatInput = Input.ReadDouble("Enter numeric value:"); + var floatInput2 = Input.ReadDouble("Enter numeric value:", -20, 34.7f); + var enumInput2 = Input.ReadEnum("Select a fruit"); + var boolInput = Input.ReadBool("Are you mad?"); + var dateInput = Input.ReadDate("Enter date:"); + var dateInput2 = Input.ReadDate("Enter date:", DateTime.Parse("2020-01-01"), DateTime.Now); Input.ReadString("Press [Enter] to navigate home"); Program.NavigateHome(); diff --git a/Demo/Runner.cs b/Demo/Runner.cs index c7ddcf6..32d7691 100644 --- a/Demo/Runner.cs +++ b/Demo/Runner.cs @@ -1,9 +1,16 @@ -namespace Demo +using EasyConsole; +using System; + +namespace Demo { class Runner { static void Main(string[] args) { + Settings.TitleColor = ConsoleColor.Blue; + Settings.UserInputColor = ConsoleColor.Green; + Settings.ShowSelectedEnumText = true; + new DemoProgram().Run(); } } diff --git a/EasyConsole/EasyConsole.csproj b/EasyConsole/EasyConsole.csproj index 3fa2fc7..c926b90 100644 --- a/EasyConsole/EasyConsole.csproj +++ b/EasyConsole/EasyConsole.csproj @@ -50,6 +50,7 @@ + @@ -66,7 +67,7 @@ - - + + \ No newline at end of file diff --git a/EasyConsole/Input.cs b/EasyConsole/Input.cs index 2112e22..cb366a6 100644 --- a/EasyConsole/Input.cs +++ b/EasyConsole/Input.cs @@ -4,6 +4,22 @@ namespace EasyConsole { public static class Input { + + enum YesNoEnum + { + Yes, + No + } + + public static string ReadLine() + { + Console.ForegroundColor = Settings.UserInputColor; + var value = Console.ReadLine(); + Console.ResetColor(); + return value; + } + + public static int ReadInt(string prompt, int min, int max) { Output.DisplayPrompt(prompt); @@ -19,28 +35,28 @@ public static int ReadInt(int min, int max) Output.DisplayPrompt("Please enter an integer between {0} and {1} (inclusive)", min, max); value = ReadInt(); } - return value; } public static int ReadInt() { - string input = Console.ReadLine(); + string input = ReadLine(); int value; while (!int.TryParse(input, out value)) { Output.DisplayPrompt("Please enter an integer"); - input = Console.ReadLine(); + input = ReadLine(); } - return value; } + public static string ReadString(string prompt) { Output.DisplayPrompt(prompt); - return Console.ReadLine(); + var input = ReadLine(); + return input; } public static TEnum ReadEnum(string prompt) where TEnum : struct, IConvertible, IComparable, IFormattable @@ -58,7 +74,71 @@ public static TEnum ReadEnum(string prompt) where TEnum : struct, IConver menu.Add(Enum.GetName(type, value), () => { choice = (TEnum)value; }); menu.Display(); + if (Settings.ShowSelectedEnumText) + { + //go to previous line after "Choose an option: [N]" + int cursorPosition = 18 + menu.SelectedIndex.ToString().Length; + Console.SetCursorPosition(cursorPosition, Console.CursorTop - 1); + Output.WriteLine(Settings.UserInputColor, $" ({choice})"); + } + return choice; } + + public static double ReadDouble(string prompt) + { + Output.DisplayPrompt(prompt); + string input = ReadLine(); + double value; + + while (!double.TryParse(input, out value)) + { + Output.DisplayPrompt("Please enter a numeric value"); + input = ReadLine(); + } + return value; + } + public static double ReadDouble(string prompt, float min, float max) + { + double value = ReadDouble(prompt); + while (value < min || value > max) + { + Output.DisplayPrompt("Please enter a numeric value between {0} and {1} (inclusive)", min, max); + value = ReadDouble(prompt); + } + return value; + } + + + public static DateTime ReadDate(string prompt) + { + Output.DisplayPrompt(prompt); + string input = ReadLine(); + DateTime value; + while (!DateTime.TryParse(input, out value)) + { + Output.DisplayPrompt("Please enter a valid date-time"); + input = ReadLine(); + } + return value; + } + public static DateTime ReadDate(string prompt, DateTime min, DateTime max) + { + DateTime value = ReadDate(prompt); + while (value < min || value > max) + { + Output.DisplayPrompt("Please enter a date between {0} and {1} (inclusive)", min, max); + value = ReadDate(prompt); + } + return value; + } + + public static bool ReadBool(string prompt) + { + var input = Input.ReadEnum(prompt); + return (input == YesNoEnum.Yes); + } + + } } diff --git a/EasyConsole/Menu.cs b/EasyConsole/Menu.cs index 7899c42..cd4fc98 100644 --- a/EasyConsole/Menu.cs +++ b/EasyConsole/Menu.cs @@ -7,6 +7,7 @@ namespace EasyConsole public class Menu { private IList