Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Demo/Pages/InputPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ public override void Display()
{
base.Display();

Fruit input = Input.ReadEnum<Fruit>("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<Fruit>("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();
Expand Down
9 changes: 8 additions & 1 deletion Demo/Runner.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
Expand Down
5 changes: 3 additions & 2 deletions EasyConsole/EasyConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Page.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings.cs" />
<Compile Include="StringExtensions.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -66,7 +67,7 @@
<Target Name="AfterBuild">
<!-- Only download a new copy of nuget.exe if we don't have a copy available -->
<WebDownload Condition="!Exists('nuget.exe')" Filename="nuget.exe" FileUri="https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" />

<Exec Command="nuget pack EasyConsole.csproj"></Exec>
<Exec Command="nuget pack EasyConsole.csproj">
</Exec>
</Target>
</Project>
90 changes: 85 additions & 5 deletions EasyConsole/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<TEnum>(string prompt) where TEnum : struct, IConvertible, IComparable, IFormattable
Expand All @@ -58,7 +74,71 @@ public static TEnum ReadEnum<TEnum>(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<YesNoEnum>(prompt);
return (input == YesNoEnum.Yes);
}


}
}
2 changes: 2 additions & 0 deletions EasyConsole/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace EasyConsole
public class Menu
{
private IList<Option> Options { get; set; }
public int SelectedIndex { get; private set; }

public Menu()
{
Expand All @@ -20,6 +21,7 @@ public void Display()
Console.WriteLine("{0}. {1}", i + 1, Options[i].Name);
}
int choice = Input.ReadInt("Choose an option:", min: 1, max: Options.Count);
this.SelectedIndex = choice;

Options[choice - 1].Callback();
}
Expand Down
7 changes: 6 additions & 1 deletion EasyConsole/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,24 @@ public Page(string title, Program program)

public virtual void Display()
{
Console.ForegroundColor = Settings.TitleColor;

if (Program.History.Count > 1 && Program.BreadcrumbHeader)
{
string breadcrumb = null;
foreach (var title in Program.History.Select((page) => page.Title).Reverse())
breadcrumb += title + " > ";
breadcrumb = breadcrumb.Remove(breadcrumb.Length - 3);
Console.WriteLine(breadcrumb);
Console.WriteLine(new string('-', breadcrumb.Length)); //add line
}
else
{
Console.WriteLine(Title);
Console.WriteLine(new string('-', Title.Length)); //add line
}
Console.WriteLine("---");
Console.ResetColor();

}
}
}
13 changes: 13 additions & 0 deletions EasyConsole/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace EasyConsole
{
public static class Settings
{
public static ConsoleColor TitleColor = Console.ForegroundColor; //default value

public static ConsoleColor UserInputColor = Console.ForegroundColor; //default value

public static bool ShowSelectedEnumText = false; //displays the selected enum option as text
}
}