Exemplo n.º 1
0
 public virtual void PrintException(Exception ex)
 {
     LConsole.BeginLine()
     .Write("An exception occurred while executing the command: ", ConsoleColor.Red)
     .Write(ex.Message, ConsoleColor.DarkRed)
     .End();
 }
Exemplo n.º 2
0
        // Metodo per scrivere nella console
        private void WriteConsole(string Testo, Color Colore)
        {
            ListViewItem temp = new ListViewItem();

            temp.Text      = Testo;
            temp.ForeColor = Colore;
            LConsole.Items.Insert(0, temp);
            LConsole.EnsureVisible(0);
        }
Exemplo n.º 3
0
        public static object Test(int num, object str = null)
        {
            LConsole.WriteLine($"Hello {num}, {str} ({str?.GetType()})", ConsoleColor.Blue);

            if (num == 42)
            {
                Console.SwitchFrontend(new PlainTextConsoleFrontend());
            }

            return(new TestClass());
        }
Exemplo n.º 4
0
        public static void PrintUsage(this Command cmd)
        {
            using (var writer = LConsole.BeginLine())
            {
                if (cmd.Description != null)
                {
                    writer.WriteLine(cmd.Description, ConsoleColor.Cyan);
                }

                writer.Write("Usage: ", ConsoleColor.DarkYellow);

                string usage = cmd.Name + " " + cmd.GetParamsString();
                writer.Write(usage, ConsoleColor.Cyan);
            }
        }
Exemplo n.º 5
0
        private void HandleVariable(string varName, string args)
        {
            if (!Config.EnableVariables)
            {
                LConsole.WriteLine("Variables are disabled", ConsoleColor.Red);
                return;
            }

            var(exists, value) = _Environment.TryGet(varName);

            if (string.IsNullOrWhiteSpace(args))
            {
                if (!exists)
                {
                    LConsole.WriteLine($"No variable found with name '{varName}'", ConsoleColor.Red);
                    return;
                }
            }
            else if (args.StartsWith(":="))
            {
                if (!HandleVariableOperation(varName, args.Substring(2).Trim()))
                {
                    return;
                }
            }
            else if (args.StartsWith("="))
            {
                value = args.Substring(1).Trim();

                if (string.IsNullOrWhiteSpace(value))
                {
                    _Environment.Remove(varName);
                    LConsole.WriteLine("Cleared variable");
                    return;
                }
                else
                {
                    _Environment.Set(varName, value);
                }
            }

            LConsole.BeginLine()
            .Write(varName, ConsoleColor.DarkYellow)
            .Write(" = ", ConsoleColor.DarkGray)
            .Write(value)
            .End();
        }
Exemplo n.º 6
0
        public void Run(string fileName)
        {
            var  lexemes = Lexer.Lex(fileName, FileSystem).ToArray();
            File ast;

            try
            {
                ast = Parser.ParseFile(lexemes);
            }
            catch (ParseException ex) when(!Debugger.IsAttached)
            {
                LConsole.WriteLine(ex.Message, ConsoleColor.Red);
                return;
            }

            try
            {
                Interpreter.Run(ast);
            }
            catch (RuntimeException ex) //when (!Debugger.IsAttached)
            {
                LConsole.WriteLine(ex.Message, ConsoleColor.Red);
            }
        }
Exemplo n.º 7
0
 public object DoTest(object asd)
 {
     LConsole.WriteLine("Function called: " + asd);
     return(this);
 }
Exemplo n.º 8
0
        ///<summary>Executes a single line</summary>
        /// <exception cref="CommandNotFoundException"></exception>
        /// <exception cref="ParserException"></exception>
        public void ExecuteLine(string line, bool addToHistory = true)
        {
            if (string.IsNullOrWhiteSpace(line))
            {
                return;
            }

            line = line.Trim();

            if (addToHistory)
            {
                History.AddNewItem(line);
            }

            int    cmdNameSeparatorIndex = line.IndexOf(' ');
            string cmdName  = cmdNameSeparatorIndex == -1 ? line.Substring(0) : line.Substring(0, cmdNameSeparatorIndex).Trim();
            string argsLine = cmdNameSeparatorIndex == -1 ? null : line.Substring(cmdNameSeparatorIndex + 1).Trim();

            argsLine = ReplaceVariables(argsLine);

            if (cmdName.StartsWith("$"))
            {
                HandleVariable(cmdName.Substring(1), argsLine);
                return;
            }

            var strArgs = GetArgs(argsLine).ToArray();

            var(success, _, commandsWithSameName) = CommandFinder.Find(cmdName, strArgs.Length);

            if (!success)
            {
                if (commandsWithSameName == null || commandsWithSameName.Length == 0)
                {
                    LConsole.WriteLine("Command not found");
                }
                else
                {
                    foreach (var item in commandsWithSameName)
                    {
                        item.PrintUsage();
                    }
                }

                return;
            }

            object[] cmdArgs = null;
            Command  cmd     = null;

            foreach (var possibleCmd in commandsWithSameName)
            {
                if (possibleCmd.RequiredParamCount > strArgs.Length)
                {
                    continue;
                }

                var(transformSuccess, transformError) = TryTransformParameters(argsLine, strArgs, possibleCmd, out cmdArgs);

                if (transformSuccess)
                {
                    cmd = possibleCmd;
                    break;
                }
            }

            if (cmd == null)
            {
                return;
            }

            object result = null;

            try
            {
                CommandExecutor.Execute(cmd, cmdArgs);
            }
            catch (TargetInvocationException ex)
            {
                if (ex.InnerException.GetType().Name == "SuccessException") //For NUnit
                {
                    throw ex.InnerException;
                }

                HandleException(ex.InnerException);
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }

            if (result != null)
            {
                LConsole.WriteLine("Command returned: " + result, ConsoleColor.DarkGray);
            }

            void HandleException(Exception ex)
            {
                _LastException = ex;

                LConsole.Frontend.PrintException(ex);
            }
        }