Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Console Kit | Interactive Menu, Tables, Input Validation

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:
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)
10/10 would use. I need to make a Console App now o.o
[Image: Xiuymkc.png]
Well that is much easier to do, thank u for taking a look at my code and linking my thread!
Ahh, recursion. Recursion was almost the death of me when I took AP Comp sci last year.
Anyways, looks very nice. Good job odoose.
Cute release. Looking over it now.

[Image: 1zL3pDG.jpg]

I give it a perfect 5/7, Trvp is beautiful. Also, nice release.
Posting here so i have a higher possibility to find this when i actually need it.
Thank you guys. Enjoy!

(Dec 23, 2015 09:01 PM)poopGod Wrote:  I give it a perfect 5/7, Trvp is beautiful. Also, nice release.

I see. What would give it that 8/8 r8?
I do not know Mr. Trvp but considering his user title and this post I can assume he is a majestic human being.

Jokes aside this seems nice, Odus. Maybe add like a progressbar property to it? Just have █ character appended to a string which is displayed until it reaches a certain length (like a 100 or whatever the user specifies). And have it so people can set it to a certain value or something. Seems like a cool idea.
Little suggestion @ Odus :
However about adding the options for a custom amount of submenus for every menu entry?
Something like ListViewItems & ListViewSubItems?
[C#/C++/Asm Coder]
Reverse Engineering / .Net security
(Dec 25, 2015 10:36 AM)GhostCode Wrote:  Little suggestion @ Odus :
However about adding the options for a custom amount of submenus for every menu entry?
Something like ListViewItems & ListViewSubItems?

That was actually another reason behind including the menu options as a property because it caters only to the options and the result. Accounting for multi dimensional arrays (i.e. the menu tree) would add complexity to the class. Everything from going through the branches to returning the result.
This is EPIC! Def. gonna come in handy ;) Thanks for writing this up ;)
This is very nice, indeed! Great job man. I could see myself failing miserably at making something like this, I don't know why.
Being great is half the battle. If you're great and nobody knows who you are, then what's the point of being great?
can't get it to work : Console.WriteLine($"{i + 1}.\t{Options[i]}"); vs does not recognize the $ symbole, .net 4.6 vs community
Assert failure(PID 10704 [0x000029d0], Thread: 10960 [0x2ad0]): pBuffer != NULL
(Dec 28, 2015 02:40 PM)Aristocles Wrote:  can't get it to work : Console.WriteLine($"{i + 1}.\t{Options[i]}"); vs does not recognize the $ symbole, .net 4.6 vs community

You must not be using C# 6.0 is my guess. Edited back to the old string interpolation, try again.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by joshuagress12345Mar 12, 2024 08:15 AM 4 323 Mar 12, 2024 08:15 AM
Last Post: Hackers
[Python] by MalveuxJul 12, 2020 10:22 PM 1 70 Jul 12, 2020 10:22 PM
Last Post: Flash7475
[C] by radaropticalJul 7, 2020 09:33 AM 3 103 Jul 7, 2020 09:33 AM
Last Post: tux1