Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to cancel menu using <Esc> key #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions ConsoleMenu/ConsoleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,35 @@ namespace ConsoleUI
public class ConsoleMenu<T>
{
public ConsoleMenuItem<T>[] MenuItems { get; set; }

private readonly bool enableQuitUsingEscape;
private string Description;
private int selectedItemIndex = 0;
private bool loopComplete = false;


public ConsoleMenu(string description, IEnumerable<ConsoleMenuItem<T>> menuItems)
: this(description, menuItems, false)
{
}

public ConsoleMenu(string description, IEnumerable<ConsoleMenuItem<T>> menuItems, bool enableQuitUsingEscape)
{
MenuItems = menuItems.ToArray();
Description = description;
this.enableQuitUsingEscape = enableQuitUsingEscape;
}

public void RunConsoleMenu()
/// <summary>
/// Runs the console menu, waiting for user to chose the desired item
/// </summary>
/// <returns>True - if user selected an item from the menu, false - if user used Esc key to cancel the menu</returns>
public bool RunConsoleMenu()
{
//this will resise the console if the amount of elements in the list are too big
if ((MenuItems.Count()) > Console.WindowHeight)
{
//TODO: Deal with console pagging...
//TODO: Deal with console paging...
}

if (!string.IsNullOrEmpty(Description))
Expand All @@ -35,17 +48,17 @@ public void RunConsoleMenu()
ConsoleKeyInfo kb;
Console.CursorVisible = false;


bool menuBailedOut = false;
while (!loopComplete)
{
for (int i = 0; i < MenuItems.Length; i++)
{
WriteConsoleItem(i, selectedItemIndex);
WriteConsoleItem(i);
}

bottomOffset = Console.CursorTop;
kb = Console.ReadKey(true);
HandleKeyPress(kb.Key);
menuBailedOut = HandleKeyPress(kb.Key);

//Reset the cursor to the top of the screen
Console.SetCursorPosition(0, topOffset);
Expand All @@ -55,10 +68,16 @@ public void RunConsoleMenu()
Console.SetCursorPosition(0, bottomOffset);
Console.CursorVisible = true;
loopComplete = false;
MenuItems[selectedItemIndex].CallBack.Invoke(MenuItems[selectedItemIndex].UnderlyingObject);

if (!menuBailedOut)
{
MenuItems[selectedItemIndex].CallBack.Invoke(MenuItems[selectedItemIndex].UnderlyingObject);
}

return !menuBailedOut;
}

private void HandleKeyPress(ConsoleKey pressedKey)
private bool HandleKeyPress(ConsoleKey pressedKey)
{
switch (pressedKey)
{
Expand All @@ -73,10 +92,21 @@ private void HandleKeyPress(ConsoleKey pressedKey)
case ConsoleKey.Enter:
loopComplete = true;
break;

case ConsoleKey.Escape:
if (!enableQuitUsingEscape)
{
break;
}

loopComplete = true;
return true;
}

return false;
}

private void WriteConsoleItem(int itemIndex, int selectedItemIndex)
private void WriteConsoleItem(int itemIndex)
{
if (itemIndex == selectedItemIndex)
{
Expand Down
15 changes: 13 additions & 2 deletions ConsoleMenuTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,19 @@ private static void OpenDirectoryBrowserConsole(string rootDirPath)
var dirInfo = new DirectoryInfo(dPath);
return new ConsoleMenuItem<DirectoryInfo>(dirName, DirectoryCallback, dirInfo);
});
var menu = new ConsoleMenu<DirectoryInfo>($"DIR: {Path.GetFileName(rootDirPath)}....", dirs);
menu.RunConsoleMenu();

var menu = new ConsoleMenu<DirectoryInfo>($"DIR: {Path.GetFileName(rootDirPath)}....", dirs, true);
var userPressedEscape = !menu.RunConsoleMenu();
if (userPressedEscape && !IsPathRootDirectory(rootDirPath))
{
Console.Clear();
OpenDirectoryBrowserConsole(Directory.GetParent(rootDirPath).FullName);
}
}

private static bool IsPathRootDirectory(string rootDirPath)
{
return Path.GetPathRoot(rootDirPath).Equals(rootDirPath, StringComparison.CurrentCultureIgnoreCase);
}

private static void DirectoryCallback(DirectoryInfo selectedDirInfo)
Expand Down