Simple implementation of console menu without flickering and real-time user input.
Supports:
- Plain string
- Checkboxes
- Disabled items
- Colors
API was made with simplicity and ease of use in mind.
Declare a menu without any extra classes:
Menu menu = new("Test Menu") {
[1] = "Foo",
[2] = ("Bar", false),
[0] = "(Baz)",
[0] = "(Xyz)",
[3] = "Test"
};
Here, "first column" (in [square] brackets) set item ID's. If ID is 0
, then the item is disabled.
(Static analyzers will complain about duplicate indices, but you can ignore them in case of 0
)
Users can not select a disabled item.
Second column is menu item content. If it's just a string, then it will be displayed as a simple item.
Make use of C# tuples to specify a pair of string and boolean to create an item with checkbox. Boolean value will be checkbox initial checked state.
Example above will create this kind of menu:
User then can use arrow buttons to navigate Up and Down:
When Enter key is pressed, menu is closing, returning last selected item ID.
If Enter is pressed when checkbox item is selected, it's checked state is toggled.
Last selected menu item ID is returned by Show
method of a menu instance. It can be used to find MenuItem
itself:
int selected = menu.Show();
Console.WriteLine($"Selected = [{selected}] {menu[selected].Text}");
Checkbox items can be accessed with their ID too:
bool bar = menu[2].IsChecked == true;
Console.WriteLine($"Bar = {(bar ? "true" : "false")}");
Menu inherits current console color at the time of it's construction:
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("test test");
Menu menu = new("Test Menu") {
[1] = "Foo",
[2] = ("Bar", false),
[0] = "(Baz)",
[0] = "(Xyz)",
[3] = "Test"
};
But you can specify colors yourself too:
Console.WriteLine("test test");
Menu menu = new("Test Menu") {
BackColor = ConsoleColor.White,
ForeColor = ConsoleColor.Black,
[1] = "Foo",
[2] = ("Bar", false),
[0] = "(Baz)",
[0] = "(Xyz)",
[3] = "Test"
};
Selected item will have its Background and Foreground colors inverted:
Full documentation included in source code using standard XML-docs.
There really isn't much going on. You will only need a ConsoleMenu.Menu
class.
Download a .nupkg
file from Releases and install it with a NuGet console:
Install-Package xxx\ConsoleMenu.x.x.x.nupkg
Or rip .dll
from inside the .nupkg
file manually. NuGet packages can be opened as a ZIP archive. DLL file and its XML-doc can be found in lib
directory there.