Example #1
0
        /// <summary>
        /// Render option list
        /// </summary>
        /// <param name="selected">selected items</param>
        /// <param name="startAt">position to start show</param>
        /// <param name="row">current position</param>
        private void RenderOptions(IEnumerable <int> selected, int startAt = 0, int row = 0)
        {
            ConsoleBuffer.ClearBufferFrom(BUFFER_NAME);

            int renderUntil = CalculateLastOptionToRender(startAt);

            for (var i = startAt; i < renderUntil; i++)
            {
                var caret      = GetCursorForPosition(i - startAt, row);
                var isSelected = selected.Contains(i) ? "(*)" : "( )";

                Console.WriteLine($"{caret}{isSelected} {_options[i].Text}");
            }
            Console.WriteLine("\nPress [SPACE] to select, [ENTER] to accept selection");
        }
        private static async void StartDemo()
        {
            const string NAME = "#BUFFER_SELECTION123#";

            ConsoleBuffer.MemoriseBufferPosition(NAME);

            Console.WriteLine("Select 1 option");
            var selection = InputSelection.From <TestClass>()
                            .AddOption(new TestClass {
                Name = "option1"
            })
                            .AddOption(new TestClass {
                Name = "option2"
            })
                            .AddOption(new TestClass {
                Name = "option3"
            })
                            .AddOption(new TestClass {
                Name = "option4"
            });

            var result1 = await selection.RequestInput();

            Console.WriteLine($"Selected => {result1.First()}");
            ConsoleI.AwaitContinue();
            ConsoleBuffer.ClearBufferFrom(NAME);

            Console.WriteLine("Select 1 to 5 options");
            var selection2 = InputSelection.From <TestClass>()
                             .SetMaxSelected(5)
                             .AddOption(new TestClass {
                Name = "option1"
            })
                             .AddOption(new TestClass {
                Name = "option2"
            })
                             .AddOption(new TestClass {
                Name = "option3"
            })
                             .AddOption(new TestClass {
                Name = "option4"
            })
                             .AddOption(new TestClass {
                Name = "option5"
            })
                             .AddOption(new TestClass {
                Name = "option6"
            })
                             .AddOption(new TestClass {
                Name = "option7"
            })
                             .AddOption(new TestClass {
                Name = "option8"
            })
                             .AddOption(new TestClass {
                Name = "option9"
            })
                             .AddOption(new TestClass {
                Name = "option10"
            })
                             .AddOption(new TestClass {
                Name = "option11"
            });

            var result2 = await selection2.RequestInput();

            foreach (var r in result2)
            {
                Console.WriteLine($"Selected => {r}");
            }
            ConsoleI.AwaitContinue();
            ConsoleBuffer.ClearBufferFrom(NAME);

            Console.WriteLine("Also works with ENUM");
            var result3 = await InputSelection
                          .FromEnum <EnumTest>()
                          .RequestInput();

            foreach (var r in result3)
            {
                Console.WriteLine($"Selected => {(int)r}");
            }
            ConsoleI.AwaitContinue();
            ConsoleBuffer.ClearBufferFrom(NAME);
        }