public string ReadLine(string prompt)
        {
            var completableReadLine = new CompletableReadLine(rootCommandNode);

            // Reset console line
            ClearCurrentConsoleLine();
            Console.Write(prompt);

            while (completableReadLine.ReadKey(Console.ReadKey()))
            {
                ClearCurrentConsoleLine();
                Console.Write(prompt + completableReadLine.GetReadLine());
            }

            return(completableReadLine.GetReadLine());
        }
Beispiel #2
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);
                }
            }
        }