コード例 #1
0
        /// <inheritdoc/>
        T IPrompt <T> .Show(IAnsiConsole console)
        {
            if (!console.Profile.Capabilities.Interactive)
            {
                throw new NotSupportedException(
                          "Cannot show selection prompt since the current " +
                          "terminal isn't interactive.");
            }

            if (!console.Profile.Capabilities.Ansi)
            {
                throw new NotSupportedException(
                          "Cannot show selection prompt since the current " +
                          "terminal does not support ANSI escape sequences.");
            }

            return(console.RunExclusive(() =>
            {
                var converter = Converter ?? TypeConverterHelper.ConvertToString;
                var list = new RenderableSelectionList <T>(
                    console, Title, PageSize, Choices,
                    converter, HighlightStyle, MoreChoicesText);

                using (new RenderHookScope(console, list))
                {
                    console.Cursor.Hide();
                    list.Redraw();

                    while (true)
                    {
                        var key = console.Input.ReadKey(true);
                        if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar)
                        {
                            break;
                        }

                        if (list.Update(key.Key))
                        {
                            list.Redraw();
                        }
                    }
                }

                list.Clear();
                console.Cursor.Show();

                return Choices[list.Index];
            }));
        }
コード例 #2
0
        /// <inheritdoc/>
        public List <T> Show(IAnsiConsole console)
        {
            if (console is null)
            {
                throw new ArgumentNullException(nameof(console));
            }

            if (!console.Profile.Capabilities.Interactive)
            {
                throw new NotSupportedException(
                          "Cannot show multi selection prompt since the current " +
                          "terminal isn't interactive.");
            }

            if (!console.Profile.Capabilities.Ansi)
            {
                throw new NotSupportedException(
                          "Cannot show multi selection prompt since the current " +
                          "terminal does not support ANSI escape sequences.");
            }

            return(console.RunExclusive(() =>
            {
                var converter = Converter ?? TypeConverterHelper.ConvertToString;
                var list = new RenderableMultiSelectionList <T>(
                    console, Title, PageSize, Choices,
                    Selected, converter, HighlightStyle,
                    MoreChoicesText, InstructionsText);

                using (new RenderHookScope(console, list))
                {
                    console.Cursor.Hide();
                    list.Redraw();

                    while (true)
                    {
                        var key = console.Input.ReadKey(true);
                        if (key.Key == ConsoleKey.Enter)
                        {
                            if (Required && list.Selections.Count == 0)
                            {
                                continue;
                            }

                            break;
                        }

                        if (key.Key == ConsoleKey.Spacebar)
                        {
                            list.Select();
                            list.Redraw();
                            continue;
                        }

                        if (list.Update(key.Key))
                        {
                            list.Redraw();
                        }
                    }
                }

                list.Clear();
                console.Cursor.Show();

                return list.Selections
                .Select(index => Choices[index])
                .ToList();
            }));
        }