A small and quickly made Library for Console apps that provides a prettier and easier experience with output and input parsing/validating while helping beginners avoid tedious tasks. It will also wrap the Console class in an interface in order to make ReadLines and ReadKeys testable. The source code is nowhere near structured/optimal as the purpose of this project is to improve the console developing experience. This does not follow semantic versioning or any sort of best practice.
Includes a InputHelper, ConsoleTable and ConsoleOutput
https://www.nuget.org/packages/PrettyConsoleHelper/
Install-Package PrettyConsoleHelper
I have placed static instances of the PrettyConsole and InputHelper inside their intefaces so you can directly make use of them without setting anything up. You can still change all options if necessary.
- Use the Validate method in order to use attributes which you have probably seen before as data annotations. Theres also a generic overload in case you would like to parse in to a specific type. The input message is optional.
var email = PrettyInputHelper.Validate(new EmailAddressAttribute(), "Enter email: ");
- Having troubles parsing enums? Use the GetEnumInput() method.
var season = PrettyInputHelper.GetEnumInput<Season>();
- Options: HeaderColor (default orange), Column Separator (default " | ")
- Expect any values being null? No worries! You can even add headers and rows with null values if you wanted to test the table format
- Different ways of creating a table
We have a list of person
var people = new List<Person>
{
new Person { Age = 50, Name = "Chris" },
new Person { Age = 15, Name = "NotChris" }
};
Fastest approach: Adds rows, headers, writes to console without storing the table in a variable
new PrettyTable()
.AddRowsWithDefaultHeaders(people)
.Write();
I recommend storing it in a variable because then you can reuse it with the ResetTable method.
Fastest Approach:
var tbl = new PrettyTable()
.AddRowsWithDefaultHeaders(people);
Fast approach:
var table = new PrettyTable()
.AddDefaultHeaders<Person>()
.AddRows(people)
Another fast Approach:
var tbl = new PrettyTable()
.AddHeaders("Name", "Age")
.AddRows(people);
Traditional approach
var table = new PrettyTable("Id", "Name", "Age");
foreach (var person in people)
{
table.AddRow(person.Id, person.Name, person);
}
if you would like to view the current headers in a comma separated string tbl.Headers
Options: You can choose default coloring for lots of stuff by passing down PrettyConsoleOptions
var console = new PrettyConsole(new PrettyConsoleOptions(numberColor: ConsoleColor.Red));
Write
andWriteLine
methods with overloads for printing a char multiple times, printing any type and color output aswell. Default values are provided- You can also log an Error or Warning which will print the time, message and exception (if provied)
ConsolePretty.LogError("Bad input", new ArgumentException());
- If you want your prompt with your choosen color to popup simply add
true
like thisConsolePretty.Write("Whats your name?", true);
Options: This can take in a PrettyConsole so that you are able to control the coloring and prompting!
var console = new PrettyConsole(new PrettyConsoleOptions(numberColor: ConsoleColor.Red));
var inputHelper = new InputHelper(console);
- Contains methods for taking in int input, confirming, enums, datetime input, validating input
Parse multiple arguments directly from the console using ParseOptions()
//If we have a Todo class we can do this:
var inputHelper = new InputHelper();
string[] options = { "-title", "-description" };
string[] inputs = Console.ReadLine().Split(' ');
var optionsValues = inputHelper.ParseOptions(options, inputs, "-");
optionsValues.TryGetValue(nameof(Todo.Title), out string title);
optionsValues.TryGetValue(nameof(Todo.Description), out string description);
We might want the user to pick an item from a list of items, this is done by utilizing the .Select() method that has 2 overloads
Another alternative that does not use a table
Options: You can specify all the normal PrettyConsoleOptions
- InputHelper and IPrettyConsole can be dependency injected
class Menu
{
private readonly IPrettyConsole _console;
private readonly InputHelper _input;
public Menu(IPrettyConsole console, InputHelper input)
{
_console = console;
_input = input;
}
public void Run()
{
_console.Write("y/n", true);
_console.WriteLine("It works!");
var num = _input.GetIntInput("Enter a num from 5 to 10", 5, 10);
}
}
class Program
{
static IServiceCollection ConfigureServices()
{
var services = new ServiceCollection();
services.AddPrettyConsoleHelper(opt =>
{
opt.PromptColor = ConsoleColor.DarkGreen;
opt.Prompt = " >";
});
services.AddSingleton<Menu>();
return services;
}
static void Main()
{
var serviceProvider = ConfigureServices().BuildServiceProvider();
serviceProvider.GetRequiredService<Menu>().Run();