Fasetto.EasyConsole
0.1.5
dotnet add package Fasetto.EasyConsole --version 0.1.5
NuGet\Install-Package Fasetto.EasyConsole -Version 0.1.5
<PackageReference Include="Fasetto.EasyConsole" Version="0.1.5" />
paket add Fasetto.EasyConsole --version 0.1.5
#r "nuget: Fasetto.EasyConsole, 0.1.5"
// Install Fasetto.EasyConsole as a Cake Addin #addin nuget:?package=Fasetto.EasyConsole&version=0.1.5 // Install Fasetto.EasyConsole as a Cake Tool #tool nuget:?package=Fasetto.EasyConsole&version=0.1.5
EasyConsole
EasyConsole is a library to make it easier for developers to build a simple menu interface for a .NET console application.
Features
- Automatically numbered menus
- Fluent creation of menus
- Input/Output helpers
Quick Start
Menu
The base functionality of the library is to provide an easy way to create console menus. A Menu
consists of Options
that will be presented to a user for selection. An option contains a name, that will be displayed to the user, and a callback function to invoke if the user selects the option. Render the menu in the console using the Display()
method.
var menu = new EasyConsole.Menu()
.Add("foo", () => Console.WriteLine("foo selected"))
.Add("bar", () => Console.WriteLine("bar selected"));
menu.Display();
Utilities - Input/Output
EasyConsole also provides input and output utilities to abstract the concept of dealing with the Console.
The Output
class adds helper methods to control the color of text in the console.
Output.WriteLine("default");
Output.WriteLine(ConsoleColor.Red, "red");
Output.WriteLine(ConsoleColor.Green, "green");
Output.WriteLine(ConsoleColor.Blue, "blue");
The Input
class adds helper methods that prompt the user for input. The utility takes care of displaying prompt text and handling parsing logic. For example, non-numeric input will be rejected by ReadInt()
and the user will be re-prompted.
object input = Input.ReadString("Please enter a string:");
Output.WriteLine("You wrote: {0}", input);
input = Input.ReadInt("Please enter an integer (between 1 and 10):", min: 1, max: 10);
Output.WriteLine("You wrote: {0}", input);
Program
All of these features can be put together to create complex programs with nested menus. A console program consists of a main Program
class that contains Pages
. The Program
class is a navigator of pages and will keep a history of pages that a user is navigating through. Think of it as your browser history. To create a program you must subclass the Program
class and add any Pages
in the constructor. Note: Before exiting the constructor, you must set one of the pages as the main page where the program should start.
class DemoProgram : Program
{
public DemoProgram()
: base("EasyConsole Demo", breadcrumbHeader: true)
{
AddPage(new MainPage(this));
AddPage(new Page1(this));
AddPage(new Page1A(this));
AddPage(new Page1B(this));
AddPage(new Page2(this));
SetPage<MainPage>();
}
}
A Page
can display any type of data, but the subclass MenuPage
was created to speed up the creation of pages that display menus. Simply pass in all of the options you want displayed into the options
parameter in the constructor.
class MainPage : MenuPage
{
public MainPage(Program program)
: base("Main Page", program,
new Option("Page 1", () => program.NavigateTo<Page1>()),
new Option("Page 2", () => program.NavigateTo<Page2>()),
new Option("Input", () => program.NavigateTo<InputPage>()))
{
}
}
As you can see, navigation is handled by the Program
class. As you navigate through to different pages, the history is logged. You can then invoke NavigateBack()
if you would like to go back to the previous page.
Example Project
The source code contains an example console demo under the Demo directory. It offers a demo with nested menu options as well as an example of how to prompt the user for input.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.2 is compatible. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Added ability to prompt user for an enumeration type.
- Added an overload to Output.WriteLine method to avoid needing to escape curly braces in format parameter