A utility that lets you prompt a console user for input in an interactive way. It provides hooks for tab completion, syntax highlighting, history management via the up and down arrows, etc.
        public void RegisterKeyPress(ConsoleKeyInfo key)
        {
            Context.Reset();
            Context.KeyPressed       = key;
            Context.CharacterToWrite = new ConsoleCharacter(Context.KeyPressed.KeyChar);

            IKeyHandler handler = null;

            if (KeyHandlers.TryGetValue(Context.KeyPressed.Key, out handler) == false && RichTextCommandLineReader.IsWriteable(Context.KeyPressed))
            {
                WriteCharacterForPressedKey(Context.KeyPressed);
                DoSyntaxHighlighting(Context);
            }
            else if (handler != null)
            {
                handler.Handle(Context);

                if (Context.Intercept == false && RichTextCommandLineReader.IsWriteable(Context.KeyPressed))
                {
                    WriteCharacterForPressedKey(Context.KeyPressed);
                }

                DoSyntaxHighlighting(Context);
            }
            FireValueChanged();
        }
示例#2
0
 /// <summary>
 /// Creates a new CLI object.
 /// </summary>
 public CliHelper()
 {
     Reader = new RichTextCommandLineReader() { Console = ConsoleProvider.Current };
 }
示例#3
0
        private async Task OnHandleHey(ConsoleKeyInfo keyInfo)
        {
            if (InputBox.IsInputBlocked)
            {
                return;
            }
            OnKeyPress(keyInfo);
            if (keyInfo.Key == ConsoleKey.Enter)
            {
                ConsoleString output = ConsoleString.Empty;
                try
                {
                    var args = Args.Convert(InputBox.Value.ToString());
                    AddHistory(InputBox.Value.ToString());

                    if (def.ExceptionBehavior?.Policy == ArgExceptionPolicy.StandardExceptionHandling)
                    {
                        def.ExceptionBehavior = new ArgExceptionBehavior(ArgExceptionPolicy.DontHandleExceptions);
                    }

                    ArgAction action;
                    ConsoleOutInterceptor.Instance.Attach();
                    try
                    {
                        action = Args.ParseAction(def, args);
                    }
                    finally
                    {
                        ConsoleOutInterceptor.Instance.Detatch();
                    }
                    InputBox.Dispose();
                    output = new ConsoleString(ConsoleOutInterceptor.Instance.ReadAndClear());

                    if (action.Cancelled == false)
                    {
                        var oldDef = Args.GetAmbientDefinition();
                        try
                        {
                            Args.SetAmbientDefinition(def);
                            await Run(action);
                        }
                        finally
                        {
                            Args.SetAmbientDefinition(oldDef);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var inner = ex;
                    if (ex is AggregateException && (ex as AggregateException).InnerExceptions.Count == 1)
                    {
                        inner = ex.InnerException;
                    }

                    if (ex is ArgException == false)
                    {
                        throw;
                    }

                    output = inner.Message.ToRed();
                }
                finally
                {
                    if (IsExpired == false)
                    {
                        HardRefresh(output);
                    }
                }
            }
            else if (keyInfo.Key == ConsoleKey.Tab)
            {
                ConsoleCharacter?prototype = InputBox.Value.Length == 0 ? (ConsoleCharacter?)null : InputBox.Value[InputBox.Value.Length - 1];
                InputBox.RichTextEditor.RegisterKeyPress(keyInfo, prototype);
            }
            else if (keyInfo.Key == ConsoleKey.UpArrow)
            {
                if (HasHistory())
                {
                    InputBox.Value = GetHistoryPrevious();
                    SetOutput(CreateAssistiveText());
                }
            }
            else if (keyInfo.Key == ConsoleKey.DownArrow)
            {
                if (HasHistory())
                {
                    InputBox.Value = GetHistoryNext();
                    SetOutput(CreateAssistiveText());
                }
            }
            else if (RichTextCommandLineReader.IsWriteable(keyInfo))
            {
                SetOutput(CreateAssistiveText());
            }
            AfterKeyPress(keyInfo);
        }