Skip to content

Tea-Cup/ConsoleMenu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConsoleMenu

Simple implementation of console menu without flickering and real-time user input.

Supports:

  • Plain string
  • Checkboxes
  • Disabled items
  • Colors

Usage

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:
Menu in console

User then can use arrow buttons to navigate Up and Down:
Menu navigation

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.
Menu interaction

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

Colors

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"
};

Console with changed colors

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"
};

Console with changed colors

Selected item will have its Background and Foreground colors inverted:
Selection has colors inverted

Documentation

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.

Installation

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages