Ejemplo n.º 1
0
        /// @short Принимает массив с названиями действий меню.
        /// @returns Выбраный заголовок.
        public static string Select(params string[] choices)
        {
            int        selectedIndex = 0;
            ConsoleKey pressedKey    = ConsoleKey.NoName;

            MyConsole.MoveCursorDown(choices.Length);

            while (pressedKey != ConsoleKey.Enter)
            {
                MyConsole.MoveCursorToBeginingOfLine();
                MyConsole.MoveCursorUp(choices.Length);

                for (int choice = 0; choice < choices.Length; choice++)
                {
                    string choiceTitle = choices[choice];
                    if (selectedIndex == choice)
                    {
                        Console.WriteLine($" > {MyConsole.ANSI_BOLD}{choiceTitle}{MyConsole.ANSI_RESET}");
                    }
                    else
                    {
                        Console.WriteLine($"   {choiceTitle}");
                    }
                }

                pressedKey = Console.ReadKey().Key;

                if (pressedKey == ConsoleKey.DownArrow)
                {
                    if (selectedIndex < choices.Length - 1)
                    {
                        selectedIndex++;
                    }
                    else
                    {
                        selectedIndex = 0;
                    }
                }
                else if (pressedKey == ConsoleKey.UpArrow)
                {
                    if (selectedIndex > 0)
                    {
                        selectedIndex--;
                    }
                    else
                    {
                        selectedIndex = choices.Length - 1;
                    }
                }
            }

            // Перемещаемся выше на 1, потому что, при нажатии Enter каретка сдвигается на 1 линию вниз
            MyConsole.MoveCursorUp(1);
            MyConsole.ClearLinesBeforeThis(choices.Length);
            // Двигаемся как раз к тому месту, где начиналось меню
            MyConsole.MoveCursorUp(choices.Length);

            return(choices[selectedIndex]);
        }
Ejemplo n.º 2
0
        /// @short Принимает массив с названиями действий меню и заголовок меню.
        /// @returns Выбраный заголовок.
        public static string Select(string prompt, params string[] choices)
        {
            Console.WriteLine($" {MyConsole.ANSI_ITALIC}{prompt}{MyConsole.ANSI_RESET}");
            string result = Select(choices);

            // Убираем строку с текстом из prompt
            MyConsole.ClearLinesBeforeThis(1);
            MyConsole.MoveCursorUp(1);
            return(result);
        }