Esempio n. 1
0
        private async IAsyncEnumerable <string> GetProcessedLinesAsync(ILineReader reader)
        {
            string?line;

            while ((line = await reader.ReadLineAsync()) != null)
            {
                var processResult = _patcher.Process(line);

                if (processResult.LineAction != LineAction.Remove)
                {
                    yield return(processResult.Line);
                }
            }
        }
Esempio n. 2
0
        private async Task CommandLineLoop(CommandDelegate application, CancellationToken loopToken)
        {
            await Task.Yield();

            var consoleTokenSource = new ConsoleCancelEventTokenSource(_console);

            loopToken.Register(consoleTokenSource.Cancel);
            IsRunning = true;

            bool   isFirstCancel = true;
            string commandLine   = null;

            try
            {
                while (!loopToken.IsCancellationRequested)
                {
                    try
                    {
                        _outputLock.Lock();
                        _prompt?.Write(_coloredTextWriter);
                        commandLine = await _lineReader.ReadLineAsync(consoleTokenSource.Token)
                                      .ConfigureAwait(false);
                    }
                    catch (OperationCanceledException ex)
                    {
                        // Do not throw because this is a normal loop ending.
                        if (loopToken.IsCancellationRequested)
                        {
                            break;
                        }

                        consoleTokenSource.Reset();
                        _console.WriteLine();

                        if (ex is ReadingLineCanceledException rex && rex.HadUserInput)
                        {
                            // Do not count this as a first cancellation if there was a user input
                            // and reset the flag if it was not the first one.
                            isFirstCancel = true;
                            continue;
                        }

                        // Exit on double Ctrl+C hotkey.
                        if (!isFirstCancel)
                        {
                            break;
                        }

                        isFirstCancel = false;
                        _console.WriteLine("Press Ctrl+C again to close application");
                        continue;
                    }
                    finally
                    {
                        _outputLock.Unlock();
                    }

                    // Reset the flag after a succesfull reading.
                    isFirstCancel = true;

                    if (String.IsNullOrWhiteSpace(commandLine))
                    {
                        continue;
                    }

                    try
                    {
                        var parsedCommandLine = _parser.Parse(commandLine, consoleTokenSource.Token);

                        if (parsedCommandLine.HasErrors)
                        {
                            PrintParseErrors(parsedCommandLine);
                            continue;
                        }

                        var context = _contextBuilder
                                      .SetCancelToken(consoleTokenSource.Token)
                                      .SetRawCommandLine(commandLine)
                                      .SetParsedCommandLine(parsedCommandLine)
                                      .Build();

                        // End command-line loop.
                        if (loopToken.IsCancellationRequested)
                        {
                            break;
                        }

                        await application(context).ConfigureAwait(false);
                    }
                    catch (OperationCanceledException)
                    {
                        // Do not throw because this is a normal loop ending.
                        if (loopToken.IsCancellationRequested)
                        {
                            break;
                        }

                        consoleTokenSource.Reset();
                        _console.WriteLine();
                    }
                    catch (Exception ex)
                    {
                        PrintErrorMessage(ex, verbose: false);
                    }
                }
            }
            finally
            {
                consoleTokenSource.Dispose();
                IsRunning = false;
            }
        }
Esempio n. 3
0
 public Task <string?> ReadLineAsync()
 {
     return(_innerReader.ReadLineAsync());
 }