Ejemplo n.º 1
0
        public static Func <bool> GenerateMenuFunction(MenuFunctionGenerationSettings menuFunctionGenerationSettings)
        {
            Dictionary <ConsoleKey, MenuOption> optionsDictionary = menuFunctionGenerationSettings.MenuOptions.ToDictionary(k => k.Key);

            if (menuFunctionGenerationSettings.IncludeX && !optionsDictionary.ContainsKey(ConsoleKey.X))
            {
                optionsDictionary.Add(ConsoleKey.X, new MenuOption(ConsoleKey.X, "Exit"));
            }

            return(() =>
            {
                if (menuFunctionGenerationSettings.ClearConsole)
                {
                    Console.Clear();
                }
                Console.WriteLine(menuFunctionGenerationSettings.PreMenuText);
                foreach (var kvp in optionsDictionary)
                {
                    Console.WriteLine(string.Format(menuFunctionGenerationSettings.OptionFormat, kvp.Value.Option, kvp.Value.OptionText));
                }
                Console.Write($"{Environment.NewLine}{menuFunctionGenerationSettings.Prompt}");

                ConsoleKey chosenKey = Console.ReadKey().Key;
                Console.WriteLine();
                if (optionsDictionary.TryGetValue(chosenKey, out MenuOption chosenOption))
                {
                    chosenOption.Action?.Invoke();

                    return chosenOption.ShowMenu;
                }
                else
                {
                    return true;
                }
            });
        }
Ejemplo n.º 2
0
 public static void ShowMenu(MenuFunctionGenerationSettings menuFunctionGenerationSettings)
 {
     ShowMenu(GenerateMenuFunction(menuFunctionGenerationSettings));
 }