示例#1
0
        /// <summary>
        /// Clears Cosole Screen and draws menu of provided options
        /// </summary>
        /// <typeparam name="TReturnType"></typeparam>
        /// <param name="menu"></param>
        /// <param name="brush"></param>
        /// <returns></returns>
        public async Task <TReturnType> AskUser <TReturnType>(Menu <TReturnType> menu, PaintBrush brush)
        {
            logger.Trace($"Method {nameof(AskUser)} called");

            Console.Clear();

            brush.RenderMenu(menu);

            ConsoleKey key;

            do
            {
                key = await Task.Run(() => Console.ReadKey(true).Key);

                switch (key)
                {
                case ConsoleKey.UpArrow:
                {
                    int newIndex = menu.Choices.ToList().IndexOf(menu.SelectedChoice) - 1;
                    // overflow validation
                    // todo implement "rotating option for Menu model" (if selected q is 0 and direction is UP select last q and vise versa)
                    if (newIndex < 0)
                    {
                        continue;
                    }

                    brush.DeselectChoice(menu);

                    menu.SelectedChoice = menu.Choices.ToList()[newIndex];

                    brush.SelectChoice(menu);
                    break;
                }

                case ConsoleKey.DownArrow:
                {
                    int newIndex = menu.Choices.ToList().IndexOf(menu.SelectedChoice) + 1;

                    // todo implement "rotating option for Menu model" (if selected q is 0 and direction is UP select last q and vise versa)
                    if (newIndex >= menu.Choices.Count)
                    {
                        continue;
                    }

                    brush.DeselectChoice(menu);

                    menu.SelectedChoice = menu.Choices.ToList()[newIndex];

                    brush.SelectChoice(menu);
                    break;
                }
                }
            } while (key != ConsoleKey.Enter);

            Console.Clear();

            logger.Trace($"Method {nameof(AskUser)} ended");

            return(menu.SelectedChoice.Key);
        }