PowerArgs.Cli.Dialog.ShowRichTextInput C# (CSharp) Method

ShowRichTextInput() public static method

public static ShowRichTextInput ( ConsoleString message, Action resultCallback, System.Action cancelCallback = null, bool allowEscapeToCancel = true, int maxHeight = 12, TextBox inputBox = null ) : void
message ConsoleString
resultCallback Action
cancelCallback System.Action
allowEscapeToCancel bool
maxHeight int
inputBox TextBox
return void
        public static void ShowRichTextInput(ConsoleString message, Action<ConsoleString> resultCallback, Action cancelCallback = null, bool allowEscapeToCancel = true, int maxHeight = 12, TextBox inputBox = null)
        {
            if (ConsoleApp.Current == null)
            {
                throw new InvalidOperationException("There is no console app running");
            }

            ConsolePanel content = new ConsolePanel();

            content.Width = ConsoleApp.Current.LayoutRoot.Width / 2;
            content.Height = ConsoleApp.Current.LayoutRoot.Height / 2;

            var dialog = new Dialog(content);
            dialog.MaxHeight = maxHeight;
            dialog.AllowEscapeToCancel = allowEscapeToCancel;
            dialog.Cancelled.SubscribeForLifetime(() => { if (cancelCallback != null) cancelCallback(); }, dialog.LifetimeManager);

            Label messageLabel = content.Add(new Label() { Text = message,  X = 2, Y = 2 });
            if (inputBox == null)
            {
                inputBox = new TextBox() { Foreground = ConsoleColor.Black, Background = ConsoleColor.White };
            }

            content.Add(inputBox).CenterHorizontally();
            inputBox.Y = 4;

            content.SynchronizeForLifetime(nameof(Bounds), () => { inputBox.Width = content.Width - 4; }, content.LifetimeManager);

            inputBox.KeyInputReceived.SubscribeForLifetime((k) =>
            {
                if (k.Key == ConsoleKey.Enter)
                {
                    resultCallback(inputBox.Value);
                    ConsoleApp.Current.LayoutRoot.Controls.Remove(dialog);
                }
            }, inputBox.LifetimeManager);

            ConsoleApp.Current.LayoutRoot.Controls.Add(dialog);
            inputBox.TryFocus();
        }

Usage Example

Example #1
0
        private async void SaveAsCommandImpl()
        {
            var input = await Dialog.ShowRichTextInput(new RichTextDialogOptions()
            {
                Message = "Select destination".ToYellow()
            });

            if (input == null)
            {
                return;
            }
            SaveCommon(input.StringValue);
        }
All Usage Examples Of PowerArgs.Cli.Dialog::ShowRichTextInput