using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace laba3.Core { /// /// TUI menu; /// public class Menu { private List options; private int selected; private string title; public List GetOptions() { return options; } public string GetTitle() { return title; } /// /// Constructor of menu; /// /// Title of menu public Menu(string title) { this.title = title; this.options = new List { }; } /// /// Add option to menu; /// /// Name of option /// Action, runs if option selected public void AddOption(string name, Action action) { this.options.Add(new MenuOption(name, action)); } /// /// Run menu; /// Console will be clean!; /// public void RunMenu() { selected = 0; this.PrintMenu(); ConsoleKeyInfo keyinfo; do { keyinfo = Console.ReadKey(); if (keyinfo.Key == ConsoleKey.DownArrow) { if (selected + 1 < options.Count) { selected++; this.PrintMenu(); } } if (keyinfo.Key == ConsoleKey.UpArrow) { if (selected > 0) { selected--; this.PrintMenu(); } } if (keyinfo.Key == ConsoleKey.Enter) { Console.Clear(); options[selected].Action.Invoke(); } } while (keyinfo.Key != ConsoleKey.Enter); } private void PrintMenu() { Console.Clear(); Console.WriteLine(title); int optionIndex = 0; foreach (MenuOption option in options) { string pointer = optionIndex == selected ? " ->" : " "; Console.WriteLine($"{pointer}{option.Name}"); optionIndex++; } } public struct MenuOption { public string Name { get; } public Action Action { get; } public MenuOption(string name, Action action) { Name = name; Action = action; } } } }