Skip to content

A Library for Console apps that provides a prettier and easier experience with output and input parsing/validating

License

Notifications You must be signed in to change notification settings

chrisK00/PrettyConsoleHelper

Repository files navigation

PrettyConsoleHelper

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

How to use

Static instances

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.

InputHelper with methods for validation and parsing.

  • 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: ");


input.png

  • Having troubles parsing enums? Use the GetEnumInput() method.var season = PrettyInputHelper.GetEnumInput<Season>(); enumexample.png

Console table - fluent api style

  • 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
  1. 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);
            }
  1. Print table.Write();

    output.png

if you would like to view the current headers in a comma separated string tbl.Headers

PrettyConsole : IPrettyConsole

Options: You can choose default coloring for lots of stuff by passing down PrettyConsoleOptions

var console = new PrettyConsole(new PrettyConsoleOptions(numberColor: ConsoleColor.Red));
  • Write and WriteLine 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()); errorlog.png
  • If you want your prompt with your choosen color to popup simply add true like this ConsolePretty.Write("Whats your name?", true);
    true.png

InputHelper : IInputHelper

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);

example.png

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 Umc-Bz-M096-P.gif

Another alternative that does not use a table MPIn-Mk-Uj5-F.gif

Dependency injection

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();

dipretty.png

About

A Library for Console apps that provides a prettier and easier experience with output and input parsing/validating

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages