Exemplo n.º 1
0
        void ExecutionCycle(Context currentContext)
        {
            context = currentContext;
            context.NewOutputReceived     += context_NewOutputReceived;
            context.InputRefreshed        += context_InputRefreshed;
            context.NewInputReceived      += context_NewInputReceived;
            context.ExecutionStateChanged += context_ExecutionStateChanged;
            context.KeyPressed            += new EventHandler <EventArgs <ConsoleKeyInfo> >(context_KeyPressed);

            Console.CursorVisible = false;

            currentContext.Define(
                "exit",
                (_invocation, _context) =>
            {
                PropertyDecorator <string> _message = new PropertyDecorator <string>("Shutting down execution cycle.");
                _message.Set("sync", true);
                _message.Set("language", "english");
                _context.SystemOutput.Enqueue(
                    new CommandOutput(
                        OutputLevel.Information,
                        _message,
                        DateTime.Now));
                isRunning = false;
            });

            while (isRunning)
            {
                context.State = ExecutionState.Waiting;
                currentContext.MainInput.Enqueue( );
                context.State = ExecutionState.Idle;
            }
        }
Exemplo n.º 2
0
        PropertyDecorator <string> Decorat(this string self, string key, double value)
        {
            PropertyDecorator <string> _decorated = new PropertyDecorator <string>(self);

            _decorated.Set(key, value);
            return(_decorated);
        }
Exemplo n.º 3
0
        PropertyDecorator <string> DecoratAsDoNotRead(this string self)
        {
            PropertyDecorator <string> _decorated = new PropertyDecorator <string>(self);

            _decorated.Set("do not read", true);
            return(_decorated);
        }
Exemplo n.º 4
0
        PropertyDecorator <string> DecoratAsEnglish(this string self)
        {
            PropertyDecorator <string> _decorated = new PropertyDecorator <string>(self);

            _decorated.Set("language", "english");
            return(_decorated);
        }
Exemplo n.º 5
0
        void Execute(string userInput)
        {
            State = ExecutionState.Running;
            CommandInvocation _currentInvocation = new CommandInvocation(userInput);

            if (commands.ContainsKey(_currentInvocation.FingerPrint))
            {
                try
                {
                    commands[_currentInvocation.FingerPrint].Execute(_currentInvocation, this);
                }
                catch (Exception _exc)
                {
                    MainOutput.Enqueue(_exc.Message.Decorat("language", "english"), OutputLevel.Error);
                    if (!SuppressExceptions)
                    {
                        throw;
                    }
                }
            }
            else
            {
                PropertyDecorator <string> _message =
                    new PropertyDecorator <string>(
                        "The command-fingerprint \"{0}\" is undefined in the current context.".LegacyForm(( object )userInput));
                _message.Set("language", "english");
                SystemOutput.Enqueue(
                    new CommandOutput(
                        OutputLevel.Error,
                        _message,
                        DateTime.Now));
            }
            State = ExecutionState.Idle;
        }
Exemplo n.º 6
0
 void context_InputRefreshed(object sender, EventArgs <InputCollector> e)
 {
     foreach (var _emitter in emitters)
     {
         if (_emitter.IsSensibleFor(e.Value))
         {
             PropertyDecorator <string> _glint = new PropertyDecorator <string>(e.Value.Glint( ));
             _glint.Set("complete", false);
             _emitter.EmitSingle(_glint);
         }
     }
 }
Exemplo n.º 7
0
        void context_NewInputReceived(object sender, EventArgs <InputCollector> e)
        {
            foreach (var _emitter in emitters)
            {
                if (_emitter.IsSensibleFor(e.Value))
                {
                    foreach (string _currentInput in e.Value.PeekAll( ))
                    {
                        PropertyDecorator <string> _wrap = new PropertyDecorator <string>(_currentInput);
                        _wrap.Set("complete", true);
                        _emitter.EmitSingle(_wrap);
                    }
                }
            }

            while (e.Value.Any( ))
            {
                context.Execute(e.Value.Dequeue( ));
            }
        }
Exemplo n.º 8
0
        Context( )
        {
            SuppressExceptions = true;

            MainOutput.NewOutputReceived   += collector_NewOutputReceived;
            SystemOutput.NewOutputReceived += collector_NewOutputReceived;

            MainInput.NewInputLineAdded  += collector_NewInputReceived;
            MainInput.CurrentLineChanged += MainInput_CurrentLineChanged;
            MainInput.KeyPressed         += MainInput_KeyPressed;

            Define(
                "do nothing",
                (_invocation, _context) =>
            {
                #region
                PropertyDecorator <string> _message = new PropertyDecorator <string>("I didn't do anything.");
                _message.Set("language", "english");
                _context.MainOutput.Enqueue(
                    new CommandOutput(
                        OutputLevel.Information,
                        _message,
                        DateTime.Now));
                #endregion
            });

            Define("help",
                   (_invocation, _context) =>
            {
                #region
                PropertyDecorator <string> _message =
                    new PropertyDecorator <string>(
                        "There is {0} command defined in this context.".LegacyForm(_context.commands.Count( )));
                _message.Set("language", "english");
                _context.MainOutput.Enqueue(
                    new CommandOutput(
                        OutputLevel.Information,
                        _message,
                        DateTime.Now));

                _message =
                    new PropertyDecorator <string>(
                        "The first 10 command defined in this context.");
                _message.Set("language", "english");
                _context.MainOutput.Enqueue(
                    new CommandOutput(
                        OutputLevel.Information,
                        _message,
                        DateTime.Now));

                var _orderedCommand = _context.commands.OrderBy(_e => _e.Value.FingerPrint);
                foreach (KeyValuePair <string, Command> _commandDefinition in _orderedCommand.Take(10))
                {
                    _context.MainOutput.Enqueue(_commandDefinition.Value.FingerPrint.DecoratAsEnglish( ));
                }

                _message =
                    new PropertyDecorator <string>(
                        "Use command ,,list commands from::th to::th'' for more.");
                _message.Set("language", "english");
                _context.MainOutput.Enqueue(
                    new CommandOutput(
                        OutputLevel.Information,
                        _message,
                        DateTime.Now));

                _message =
                    new PropertyDecorator <string>(
                        "Use command ,,describe ::command'' for help about commands.");
                _message.Set("language", "english");
                _context.MainOutput.Enqueue(
                    new CommandOutput(
                        OutputLevel.Information,
                        _message,
                        DateTime.Now));
                #endregion
            });

            Define("list commands from::th to::th",
                   (_invocation, _context) =>
            {
                #region
                int _from = int.Parse(_invocation["from:th"].Value);
                int _to   = int.Parse(_invocation["to:th"].Value);

                PropertyDecorator <string> _message =
                    new PropertyDecorator <string>(
                        "Commands from {0} to {1} are the followings.".LegacyForm(_from, _to));
                _message.Set("language", "english");
                _context.MainOutput.Enqueue(
                    new CommandOutput(
                        OutputLevel.Information,
                        _message,
                        DateTime.Now));

                var _orderedCommand           = _context.commands.OrderBy(_e => _e.Value.FingerPrint);
                StringBuilder _listOfCommands = new StringBuilder( );
                for (int _index = _from; _index < _to; _index++)
                {
                    _context.MainOutput.Enqueue(
                        _orderedCommand.ElementAt(_index).Value.FingerPrint.DecoratAsEnglish( ));
                }
                #endregion
            });

            Define(
                "execute from::",
                (_invocation, _context) =>
            {
                #region
                string _path    = _invocation[@"^from$:"].Value;
                var _inputLines = loadFile(_path);
                MainOutput.Enqueue(
                    "Totaly {0} input lines loaded. Start execution now.".LegacyForm(_inputLines.Count( )).DecoratAsEnglish( ));
                MainInput.AddRange(_inputLines);
                #endregion
            });
            Define(
                "wait for::milliseconds",
                (_invocation, _context) =>
            {
                #region
                int _timeout = int.Parse(_invocation[@"^for$:^milliseconds$"].Value);
                _context.MainOutput.Enqueue(
                    "Waiting for {0} milliseconds.".LegacyForm(_timeout).DecoratAsEnglish( ));
                Thread.Sleep(_timeout);
                #endregion
            });
            Define(
                "label as::",
                (_invocation, _context) =>
                _context.MainOutput.Enqueue(
                    _invocation[@"^as$:"].Value.DecoratAsEnglish( ),
                    OutputLevel.Note));
        }