Ejemplo n.º 1
0
        public int OnPromptForChoice(PromptForChoiceEventArgs e)
        {
            EventHandler <PromptForChoiceEventArgs> handler = PromptForChoice;

            if (handler != null)
            {
                handler(this, e);
                return(e.SelectedIndex);
            }

            // Write the caption and message strings in Blue.
            Write(ConsoleColor.Blue, ConsoleColor.Black, e.Caption + "\n" + e.Message + "\n");

            // Convert the choice collection into something that's a little easier to work with
            // See the BuildHotkeysAndPlainLabels method for details.
            var promptData = BuildHotkeysAndPlainLabels(e.Choices, true);


            // Loop reading prompts until a match is made, the default is
            // chosen or the loop is interrupted with ctrl-C.
            while (true)
            {
                // Format the overall choice prompt string to display...
                for (var element = 0; element < promptData.GetLength(1); element++)
                {
                    if (element == e.SelectedIndex)
                    {
                        Write(ConsoleBrushes.VerboseForeground, ConsoleBrushes.VerboseBackground,
                              $"[{promptData[0, element]}] {promptData[1, element]}  ");
                    }
                    else
                    {
                        Write(null, null, $"[{promptData[0, element]}] {promptData[1, element]}  ");
                    }
                }
                Write(null, null, $"(default is \"{promptData[0, e.SelectedIndex]}\"):");

                string data = ReadLine().Trim().ToUpper();

                // If the choice string was empty, use the default selection.
                if (data.Length == 0)
                {
                    return(e.SelectedIndex);
                }

                // See if the selection matched and return the
                // corresponding index if it did...
                for (var i = 0; i < e.Choices.Count; i++)
                {
                    if (promptData[0, i][0] == data[0])
                    {
                        return(i);
                    }
                }

                // If they picked the very last thing in the list, they want help
                if (promptData.GetLength(1) > e.Choices.Count && promptData[0, e.Choices.Count] == data)
                {
                    // Show help
                    foreach (var choice in e.Choices)
                    {
                        Write($"{choice.Label.Replace("&", "")} - {choice.HelpMessage}\n");
                    }
                }
                else
                {
                    Write(ConsoleBrushes.ErrorForeground, ConsoleBrushes.ErrorBackground, "Invalid choice: " + data + "\n");
                }
            }
        }
Ejemplo n.º 2
0
        private void Console_PromptForChoice(object sender, PromptForChoiceEventArgs e)
        {
            // Ensure this is invoked synchronously on the UI thread
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.Invoke(DispatcherPriority.Render, (Action)(() =>
                {
                    Console_PromptForChoice(sender, e);
                }));
                return;
            }

            // Disable the console ...
            PoshConsole.CommandBox.IsEnabled = false;

            #region PromptForChoiceWindowCouldBeInXaml
            // Create a window with a stack panel inside
            var content = new Grid {
                Margin = new Thickness(6)
            };

            content.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(50)
            });
            content.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });

            var buttons = new StackPanel {
                Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right
            };
            content.Children.Add(buttons);
            buttons.SetValue(Grid.RowProperty, 1);

            var dialog = new Window
            {
                SizeToContent = SizeToContent.WidthAndHeight,
                HorizontalContentAlignment = HorizontalAlignment.Stretch,
                VerticalContentAlignment   = VerticalAlignment.Stretch,
                Title                 = e.Caption,
                Content               = content,
                MinHeight             = 100,
                MinWidth              = 300,
                WindowStartupLocation = WindowStartupLocation.CenterOwner,
                Owner                 = this,
                Tag = -1 // We must initialize the tag
            };

            // Make buttons for each choice
            var index = 0;
            foreach (var choice in e.Choices)
            {
                var item = new Button
                {
                    Content   = choice.Label.Replace('&', '_'),
                    ToolTip   = choice.HelpMessage,
                    IsDefault = e.SelectedIndex == index,
                    Padding   = new Thickness(10, 4, 10, 4),
                    Margin    = new Thickness(4),
                    Tag       = index // set the button Tag to it's index
                };

                // when the button is clicked, set the window tag to the button's index, and close the window.
                item.Click += (o, args) =>
                {
                    dialog.Tag = (args.OriginalSource as FrameworkElement)?.Tag;
                    dialog.Close();
                };
                buttons.Children.Add(item);
                index++;
            }

            // Handle the Caption and Message
            if (string.IsNullOrWhiteSpace(e.Caption))
            {
                e.Caption = e.Message;
            }
            if (!string.IsNullOrWhiteSpace(e.Message))
            {
                content.Children.Insert(0, new TextBlock
                {
                    Text       = e.Message,
                    FontSize   = 16,
                    FontWeight = FontWeight.FromOpenTypeWeight(700),
                    Effect     = new System.Windows.Media.Effects.DropShadowEffect
                    {
                        Color       = Colors.CadetBlue,
                        Direction   = 0,
                        ShadowDepth = 0,
                        BlurRadius  = 5
                    }
                });
            }
            #endregion CouldBeInXaml

            dialog.ShowDialog();
            e.SelectedIndex = (int)dialog.Tag;

            // Reenable the console
            PoshConsole.CommandBox.IsEnabled = true;
        }