Esempio n. 1
0
        private bool OnTextValidatingEnterInternal(string text, bool hasControl)
        {
            _cancellationTokenSource = new CancellationTokenSource();
            CancellationToken        = _cancellationTokenSource.Token;

            var      elapsed = _clockReplInput.ElapsedMilliseconds;
            Template script  = null;

            object result      = null;
            string error       = null;
            int    column      = -1;
            bool   isCancelled = false;

            try
            {
                script = Parse(text);

                if (script.HasErrors)
                {
                    var errorBuilder = new StringBuilder();
                    foreach (var message in script.Messages)
                    {
                        if (errorBuilder.Length > 0)
                        {
                            errorBuilder.AppendLine();
                        }
                        if (column <= 0 && message.Type == ParserMessageType.Error)
                        {
                            column = message.Span.Start.Column;
                        }
                        errorBuilder.Append(message.Message);
                    }

                    error = errorBuilder.ToString();
                }
                else
                {
                    Repl.AfterEditLine.Clear();
                    HighlightOutput.Clear();

                    result = EvaluatePage(script.Page);

                    if (Repl.ExitOnNextEval)
                    {
                        return(false);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ScriptRuntimeException scriptEx)
                {
                    column      = scriptEx.Span.Start.Column;
                    error       = scriptEx.OriginalMessage;
                    isCancelled = ex is ScriptAbortException;
                }
                else
                {
                    error = ex.Message;
                }
            }

            if (error != null)
            {
                Repl.AfterEditLine.Clear();
                Repl.AfterEditLine.Append('\n');
                Repl.AfterEditLine.Begin(ConsoleStyle.Red);
                Repl.AfterEditLine.Append(error);
                if (column >= 0 && column <= Repl.EditLine.Count)
                {
                    Repl.EnableCursorChanged = false;
                    Repl.CursorIndex         = column;
                    Repl.EnableCursorChanged = true;
                }

                bool emitReturnOnError = hasControl || elapsed < OnErrorToNextLineMaxDelayInMilliseconds || isCancelled;
                if (emitReturnOnError)
                {
                    Repl.AfterEditLine.Append('\n');
                }

                return(emitReturnOnError);
            }
            else
            {
                if (result != null)
                {
                    Write(script.Page.Span, result);
                }
                var resultStr = Output.ToString();
                var output    = Output as StringBuilderOutput;
                if (output != null)
                {
                    output.Builder.Length = 0;
                }

                Repl.AfterEditLine.Clear();
                bool hasOutput = resultStr != string.Empty || HighlightOutput.Count > 0;
                if (!Repl.IsClean || hasOutput)
                {
                    Repl.AfterEditLine.Append('\n');

                    bool hasNextOutput = HighlightOutput.Count > 0;
                    if (hasNextOutput)
                    {
                        Repl.AfterEditLine.AddRange(HighlightOutput);
                        HighlightOutput.Clear();
                    }

                    if (resultStr != string.Empty)
                    {
                        if (hasNextOutput)
                        {
                            Repl.AfterEditLine.AppendLine();
                        }
                        Repl.AfterEditLine.Begin(ConsoleStyle.Bold);
                        Repl.AfterEditLine.Append(resultStr);
                    }

                    if (hasOutput)
                    {
                        Repl.AfterEditLine.AppendLine();
                    }
                }
            }

            return(true);
        }
Esempio n. 2
0
 public void ClearScreen()
 {
     Repl?.Clear();
     HighlightOutput.Clear();
 }