public void Initialize(
     string prompt             = null,
     ICommandLineProcessor clp = null,
     Delegates.ExpressionEvaluationCommandDelegate evalCommandDelegate = null)
 {
     _defaultPrompt        = prompt ?? $"> ";
     Console               = clp.Console;
     _commandLineProcessor = clp;
     if (_commandLineProcessor != null && _commandLineProcessor != null)
     {
         _commandLineProcessor.CommandLineReader = this;
     }
     Initialize(evalCommandDelegate);
 }
        public ExpressionEvaluationResult ProcessCommandLine(
            string commandLine,
            Delegates.ExpressionEvaluationCommandDelegate evalCommandDelegate,
            bool outputStartNextLine    = false,
            bool enableHistory          = false,
            bool enablePrePostComOutput = true)
        {
            var clp = _commandLineProcessor;

            if (commandLine == null)
            {
                return(null);
            }

            if (outputStartNextLine)
            {
                Console.Out.LineBreak();
            }

            if (string.IsNullOrWhiteSpace(commandLine))
            {
                if (enablePrePostComOutput && clp != null)
                {
                    Console.Out.Echo(clp.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPreAnalysisOutput));
                }

                return(null);
            }

            ExpressionEvaluationResult expressionEvaluationResult = null;

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

                clp.ModuleManager.ModuleHookManager.InvokeHooks(
                    clp.CommandEvaluationContext, Hooks.PreProcessCommandLine, commandLine);

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

                try
                {
                    try
                    {
                        task.Wait(clp.CancellationTokenSource.Token);       // TODO: not if {com} &
                    }
                    catch (ThreadInterruptedException)
                    {
                        // get interrupted after send input
                    }
                    expressionEvaluationResult = task.Result;
                }
                catch (OperationCanceledException)
                {
                    clp.ModuleManager.ModuleHookManager.InvokeHooks <CommandLineReader>(
                        clp.CommandEvaluationContext, Hooks.ProcessCommandLineCanceled);

                    expressionEvaluationResult = task.Result;
                    Console.Out.Warningln($"command canceled: {commandLine}");
                }
                finally { }
            }
            catch (Exception evalCommandException)
            {
                clp.ModuleManager.ModuleHookManager.InvokeHooks(
                    clp.CommandEvaluationContext, Hooks.ProcessCommandLineError);
                Console.LogError(evalCommandException);
                expressionEvaluationResult = new ExpressionEvaluationResult(
                    commandLine,
                    null,
                    Parsing.ParseResultType.Empty,
                    null,
                    (int)ReturnCode.Error,
                    evalCommandException,
                    evalCommandException.Message
                    );
            }
            finally
            {
                clp.CancellationTokenSource.Dispose();
                clp.CancellationTokenSource = null;
                sc.CancelKeyPress          -= CancelKeyPress;

                clp.ModuleManager.ModuleHookManager
                .InvokeHooks(clp.CommandEvaluationContext, Hooks.PostProcessCommandLine);

                // post com output

                if (enablePrePostComOutput && clp != null)
                {
                    if (Console.Out.IsModified || Console.Err.IsModified)
                    {
                        if (!(Console.Out.CursorLeft == 0 && Console.Out.CursorTop == 0))
                        {
                            Console.Out.Echo(clp.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPostExecOutModifiedOutput));
                        }
                    }
                    Console.Out.Echo(clp.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPostExecOutput));
                }
            }

            if (enableHistory)
            {
                clp.CmdsHistory.HistoryAppend(clp.CommandEvaluationContext, commandLine);
            }

            return(expressionEvaluationResult);
        }
        void Initialize(Delegates.ExpressionEvaluationCommandDelegate evalCommandDelegate = null)
        {
            if (evalCommandDelegate == null && _commandLineProcessor != null)
            {
                _evalCommandDelegate = _commandLineProcessor.Eval;
            }

            #region disabled

#if manage_embeded_view
            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);
                        }
                    }
                }
            };
#endif
            #endregion
        }