Dec 22, 2015 05:17 PM
(This post was last modified: Apr 11, 2016 11:36 AM by Odus.)
After seeing N3w8i3H4ck3r's console menu, I was going to post some suggestions for improvement but got carried away and ended up writing this handy tool kit for making the console more user friendly with interactive menus, easy validation and tables.
Demonstration: (Click to View)
Interactive Menu
Looks a bit more pleasant than prompting the user to enter a choice corresponding to a printed menu.
All you have to to is set your options and console highlight color. Originally the options were going to be passed through a ParamArray in the constructor but I ended up making them properties so it wouldn't need to be re-instantiated if you had branches or multiple menus.
There's also some recursion in there which lets the function itself handle (what appears to be) the scrolling... hnnngggg
Usage:
Data Table
For easy data formatting, a table builder is also included specifically for custom data models. Our test case model looks like this:
It fetches all public properties' names and values used to populate the table headers & rows. There are also 2 args passed in the constructor for dimensions: width and indent. The first sets the span across the console, the second is for spacing between the console window border and the bounds of the grid - purely aesthetic.
Usage:
Validator
Writing a bunch of while loops and constant checking can get very boring, so it's wrapped up nicely here for you. To keep things neat, a colon and space is automatically appended to your prompt. You can get input, which simply prompts for input and attempts to convert it to the type you specified. If it can't, it allowed an error message to show and reattempts input.
Alternatively, you can validate, which works similar but supports a lambda expression to check for validation. This could be handy for anything, such as string length, int range, etc. As long as your expression returns a bool, you're good to go.
Usage:
Downloads:
https://github.com/oduslabs/ConsoleKit (ConsoleKit.cs)
Demonstration: (Click to View)
Interactive Menu
Looks a bit more pleasant than prompting the user to enter a choice corresponding to a printed menu.
All you have to to is set your options and console highlight color. Originally the options were going to be passed through a ParamArray in the constructor but I ended up making them properties so it wouldn't need to be re-instantiated if you had branches or multiple menus.
There's also some recursion in there which lets the function itself handle (what appears to be) the scrolling... hnnngggg
Usage:
Code
var menu = new Menu()
{
Options = new string[] {"trvp", "is", "beautiful"},
HighlightColor = ConsoleColor.Red
};
var selection = menu.AwaitInput();Data Table
For easy data formatting, a table builder is also included specifically for custom data models. Our test case model looks like this:
Code
public struct User
{
public string Name { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
}It fetches all public properties' names and values used to populate the table headers & rows. There are also 2 args passed in the constructor for dimensions: width and indent. The first sets the span across the console, the second is for spacing between the console window border and the bounds of the grid - purely aesthetic.
Usage:
Code
var users = new List<User> { }; //assume populated
var table = new Table(50, 2);
table.BuildTable(users);Validator
Writing a bunch of while loops and constant checking can get very boring, so it's wrapped up nicely here for you. To keep things neat, a colon and space is automatically appended to your prompt. You can get input, which simply prompts for input and attempts to convert it to the type you specified. If it can't, it allowed an error message to show and reattempts input.
Alternatively, you can validate, which works similar but supports a lambda expression to check for validation. This could be handy for anything, such as string length, int range, etc. As long as your expression returns a bool, you're good to go.
Usage:
Code
var age = Validator.ValidateInput<int>("Enter your age", "Input must be a number", (i) => i > 18);Downloads:
https://github.com/oduslabs/ConsoleKit (ConsoleKit.cs)



![[Image: Xiuymkc.png]](https://i.imgur.com/Xiuymkc.png)


![[Image: 1zL3pDG.jpg]](https://i.imgur.com/1zL3pDG.jpg)