void Initialize(ExpressionEvaluationCommandDelegate evalCommandDelegate = null)
        {
            if (evalCommandDelegate == null && CommandLineProcessor != null)
            {
                _evalCommandDelegate = CommandLineProcessor.Eval;
            }
            ViewSizeChanged += (o, e) =>
            {
                if (_inputReaderThread != null)
                {
                    lock (ConsoleLock)
                    {
                        Out.Echo(_prompt);
                        _beginOfLineCurPos = Out.CursorPos;
                        Out.ConsolePrint(_inputReaderStringBuilder.ToString());
                    }
                }
            };
            WorkAreaScrolled += (o, e) =>
            {
                if (_inputReaderThread != null)
                {
                    lock (ConsoleLock)
                    {
                        _beginOfLineCurPos.X += e.DeltaX;
                        _beginOfLineCurPos.Y += e.DeltaY;
                        var p = Out.CursorPos;
                        var(id, left, top, width, height) = ActualWorkArea();
                        var txt = _inputReaderStringBuilder.ToString();
                        if (!string.IsNullOrWhiteSpace(txt))
                        {
                            var index  = Out.GetIndexInWorkAreaConstraintedString(txt, _beginOfLineCurPos, p);
                            var slines = Out.GetWorkAreaStringSplits(txt, _beginOfLineCurPos).Splits;

                            if (Out.CursorTop == slines.Min(o => o.Y))
                            {
                                Out.CursorLeft = left;
                                Out.Echo(_prompt);
                            }
                            var enableConstraintConsolePrintInsideWorkArea = EnableConstraintConsolePrintInsideWorkArea;
                            EnableConstraintConsolePrintInsideWorkArea = false;
                            foreach (var sline in slines)
                            {
                                if (sline.Y >= top && sline.Y <= height)
                                {
                                    Out.SetCursorPos(sline.X, sline.Y);
                                    Out.ConsolePrint("".PadLeft(width - sline.X, ' '));
                                    Out.SetCursorPos(sline.X, sline.Y);
                                    Out.ConsolePrint(sline.Text);
                                }
                            }
                            EnableConstraintConsolePrintInsideWorkArea = enableConstraintConsolePrintInsideWorkArea;
                            Out.SetCursorPos(p);
                        }
                    }
                }
            };
        }
Exemplo n.º 2
0
 public CommandLineReader(
     CommandLineProcessor commandLineProcessor = null,
     string prompt = null,
     ExpressionEvaluationCommandDelegate evalCommandDelegate = null)
 {
     CommandLineProcessor = commandLineProcessor;
     if (CommandLineProcessor != null && CommandLineProcessor != null)
     {
         CommandLineProcessor.CommandLineReader = this;
     }
     _defaultPrompt = prompt ?? $"> ";
     Initialize(evalCommandDelegate);
 }
Exemplo n.º 3
0
        public void ProcessCommandLine(
            string commandLine,
            ExpressionEvaluationCommandDelegate evalCommandDelegate,
            bool outputStartNextLine    = false,
            bool enableHistory          = false,
            bool enablePrePostComOutput = true)
        {
            if (commandLine != null)
            {
                if (outputStartNextLine)
                {
                    Out.LineBreak();
                }
                ExpressionEvaluationResult expressionEvaluationResult = null;

                try
                {
                    sc.CancelKeyPress += CancelKeyPress;
                    CommandLineProcessor.CancellationTokenSource = new CancellationTokenSource();
                    Out.IsModified = false;
                    Err.IsModified = false;

                    var task = Task.Run <ExpressionEvaluationResult>(
                        () => evalCommandDelegate(
                            CommandLineProcessor.CommandEvaluationContext,
                            commandLine,
                            _prompt == null ? 0 : Out.GetPrint(_prompt).Length,            // TODO has no sens with multi line prompt !!!
                            (enablePrePostComOutput && CommandLineProcessor != null) ?
                            CommandLineProcessor.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPreAnalysisOutput) : ""),
                        CommandLineProcessor.CancellationTokenSource.Token
                        );

                    try
                    {
                        try
                        {
                            task.Wait(CommandLineProcessor.CancellationTokenSource.Token);
                        }
                        catch (ThreadInterruptedException)
                        {
                            // get interrupted after send input
                        }
                        expressionEvaluationResult = task.Result;
                    }
                    catch (OperationCanceledException)
                    {
                        //var res = task.Result;
                        expressionEvaluationResult = task.Result;
                        Out.Warningln($"command canceled: {commandLine}");
                    }
                    finally { }
                }
                catch (Exception ex)
                {
                    LogError(ex);
                }
                finally
                {
                    if (enablePrePostComOutput && CommandLineProcessor != null)
                    {
                        if (Out.IsModified || Err.IsModified)
                        {
                            if (!(Out.CursorLeft == 0 && Out.CursorTop == 0))
                            {
                                Out.Echo(CommandLineProcessor.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPostExecOutModifiedOutput));
                            }
                        }
                        Out.Echo(CommandLineProcessor.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPostExecOutput));
                    }

                    CommandLineProcessor.CancellationTokenSource.Dispose();
                    CommandLineProcessor.CancellationTokenSource = null;
                    sc.CancelKeyPress -= CancelKeyPress;
                }
            }

            if (enableHistory && !string.IsNullOrWhiteSpace(commandLine))
            {
                CommandLineProcessor.CmdsHistory.HistoryAppend(commandLine);
            }
        }
        public void ProcessCommandLine(
            string commandLine,
            ExpressionEvaluationCommandDelegate evalCommandDelegate,
            bool outputStartNextLine = false,
            bool enableHistory       = false)
        {
            if (commandLine != null)
            {
                if (outputStartNextLine)
                {
                    Out.LineBreak();
                }

                ExpressionEvaluationResult expressionEvaluationResult = null;

                try
                {
                    sc.CancelKeyPress += CancelKeyPress;
                    CommandLineProcessor.CancellationTokenSource = new CancellationTokenSource();
                    var task = Task.Run <ExpressionEvaluationResult>(
                        () => evalCommandDelegate(CommandLineProcessor.CommandEvaluationContext, commandLine, _prompt == null ? 0 : Out.GetPrint(_prompt).Length),
                        CommandLineProcessor.CancellationTokenSource.Token
                        );

                    try
                    {
                        try
                        {
                            task.Wait(CommandLineProcessor.CancellationTokenSource.Token);
                        } catch (ThreadInterruptedException) {
                            // get interrupted after send input
                        }
                        expressionEvaluationResult = task.Result;
                    }
                    catch (OperationCanceledException)
                    {
                        var res = task.Result;
                        Warningln($"command canceled: {commandLine}");
                    }
                    finally
                    {
                    }
                }
                catch (Exception ex)
                {
                    LogError(ex);
                }
                finally
                {
                    CommandLineProcessor.CancellationTokenSource.Dispose();
                    CommandLineProcessor.CancellationTokenSource = null;
                    sc.CancelKeyPress -= CancelKeyPress;
                    lock (ConsoleLock)
                    {
                        /*if (!WorkArea.rect.IsEmpty && (WorkArea.rect.Y != CursorTop || WorkArea.rect.X != CursorLeft))
                         *  LineBreak();*/// case of auto line break (spacing)

                        //Out.RestoreDefaultColors();   // FGZ removed
                    }
                }
            }
            if (enableHistory && !string.IsNullOrWhiteSpace(commandLine))
            {
                CommandLineProcessor.CmdsHistory.HistoryAppend(commandLine);
            }
        }