Ejemplo n.º 1
0
        public async Task StartLoop()
        {
            try
            {
                Console.Clear();
            }
            catch (System.IO.IOException)
            {
                // Ignore exception when output is redirected.
                // This is a known issue and workaround.
            }

            while (true)
            {
                ICommand commandToExecute = null;

                // If there are no command in the queue, ask for a new one from user
                if (executionContext.CommandQueue.Count == 0)
                {
                    var completableReadLine = new CompletableReadLine(rootCommandNode);
                    var command             = completableReadLine.ReadLine(Prompt);
                    var commandTokens       = command.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    var closestCommandNode  = rootCommandNode.FindNode(commandTokens);

                    var parseOutput = closestCommandNode.ParseFunction(commandTokens);

                    if (parseOutput.command == null)
                    {
                        outputService.Error(parseOutput.error);
                    }
                    else
                    {
                        commandToExecute = parseOutput.command;
                    }
                }
                else
                {
                    commandToExecute = executionContext.CommandQueue.Dequeue();
                }

                if (commandToExecute != null)
                {
                    await commandToExecute.Execute(outputService, executionContext);
                }
            }
        }
        private IEnumerable <CommandNode> GetValidCompletionTree()
        {
            var lastBlock = ParseLastUserInputLastBlock();

            // Trim last block from completion tree calculation since it is an unfinished block
            var commitedUserInput = lastBlock.prefix.Length > 0 ?
                                    lastUserInput.Remove(lastBlock.index) :
                                    lastUserInput;

            var matchedCommandNode = rootCommandNode.FindNode(
                commitedUserInput.Split(
                    new[] { " " },
                    StringSplitOptions.RemoveEmptyEntries),
                exact: true);

            return(matchedCommandNode?.Children ?? new List <CommandNode>());
        }