Beispiel #1
0
        public async Task <int> RunCommand(string location)
        {
            if (string.IsNullOrEmpty(location))
            {
                location = Directory.GetCurrentDirectory();
            }

            var cartridges = CartridgeHelper.FindAllInDirectory(location);

            _logger.LogDebug("Found {NumCartridges} cartridges in {Location}", cartridges.Count, location);

            var watchers = new List <FileSystemWatcher>();

            foreach (var cartridge in cartridges)
            {
                _console.Write("Watching ");
                _console.Green(cartridge.Name, eol: "");
                _console.Write("...\n");
                var watcher = new FileSystemWatcher()
                {
                    Path         = cartridge.Path,
                    NotifyFilter = NotifyFilters.LastWrite
                                   | NotifyFilters.FileName
                                   | NotifyFilters.DirectoryName
                };
                watcher.Changed += OnChanged;
                watcher.Renamed += OnRenamed;
                watcher.IncludeSubdirectories = true;
                watchers.Add(watcher);

                // TODO https://stackoverflow.com/questions/15519089/avoid-error-too-many-changes-at-once-in-directory/35432077#35432077
            }

            foreach (var watcher in watchers)
            {
                watcher.EnableRaisingEvents = true;
            }

            try
            {
                while (Console.Read() != null)
                {
                    ;
                }
            }
            catch (TaskCanceledException e)
            {
                return(0);
            }

            return(0);
        }
Beispiel #2
0
        private void MakeMove()
        {
            consoleOutput.WriteLine();
            consoleOutput.WriteLine($"{GetGameState()} Move");
            consoleOutput.Write("Row: ");
            string row = consoleInput.ReadLine();

            consoleOutput.Write("Column: ");
            string column = consoleInput.ReadLine();

            var move = new Move(GetPlayer(), Convert.ToInt32(row) - 1, Convert.ToInt32(column) - 1);

            bool moveMade = gameEngine.MakeMove(move);

            if (!moveMade)
            {
                consoleOutput.WriteLine(
                    "There was a problem with the move. Ensure withing board limits and space not already taken. Try again.");
            }
        }
Beispiel #3
0
        public void ShowFieldState(List <IAnimal> animals)
        {
            _stringDrawer.FillRectWithSymbol(INDENT_HORIZONTAL, INDENT_VERTICAL,
                                             _field.Size.X, _field.Size.Y, Constants.EMPTY_SYMBOL);

            foreach (var animal in animals)
            {
                _stringDrawer.PutSymbol(INDENT_HORIZONTAL + animal.Position.X, INDENT_VERTICAL + animal.Position.Y, _animalSymbolProvider.GetSymbol(animal.GetType()));
            }

            _consoleOutput.Clear();
            _consoleOutput.Write(_fieldAsStringBuilder.ToString());
        }
Beispiel #4
0
        public void ProgressHandler(object sender, long current, long total)
        {
            if (!_started)
            {
                _console.WriteLine($"0%".PadLeft(7));
                _started = true;
                Console.SetCursorPosition(0, Console.CursorTop - 1);
            }

            double percent = (double)current / (double)total * 100;

            _console.ClearLine();
            _console.Write($"{percent:F1}%".PadLeft(7));

            if (current == total)
            {
                _console.WriteLine("");
            }
        }
Beispiel #5
0
        private void LsCommandHandler(CommandLineApplication lsCmd)
        {
            lsCmd.Description = "List directory";
            var sortByTime = lsCmd.Option("-t", "Sort by modified time", CommandOptionType.NoValue);
            var filter     = lsCmd.Argument("filter", "Regular expression to filter by");

            lsCmd.OnExecuteAsync(async token =>
            {
                var contents = await _client.ListDirectory(_location, _workingDirectory.AbsolutePath);

                if (!string.IsNullOrEmpty(filter.Value))
                {
                    var filterRegex = new Regex(filter.Value);
                    contents        = contents.Where(f => filterRegex.IsMatch(f.Filename)).ToList();
                }

                contents = sortByTime.HasValue()
                    ? contents.OrderByDescending(f => f.LastModifiedDate).ToList()
                    : contents.OrderBy(f => f.Filename).ToList();

                foreach (var file in contents)
                {
                    _console.Write($"{file.LastModifiedDate:s}\t");
                    _console.Write($"{file.Length,10}\t");
                    if (file.IsDirectory)
                    {
                        _console.Blue($"{file.Filename}");
                    }
                    else
                    {
                        _console.WriteLine($"{file.Filename}");
                    }
                }

                return(0);
            });
        }
Beispiel #6
0
        public async Task <int> RunCommand(string location, bool deleteAndReactivate = false)
        {
            if (string.IsNullOrEmpty(location))
            {
                location = Directory.GetCurrentDirectory();
            }

            var cartridges = CartridgeHelper.FindAllInDirectory(location);

            _logger.LogDebug("Found {NumCartridges} cartridges in {Location}", cartridges.Count, location);

            foreach (var cartridge in cartridges)
            {
                _console.Write("Collecting ");
                _console.Green(cartridge.Name, eol: "");
                _console.Write("...\n");
            }

            if (deleteAndReactivate)
            {
                _console.WriteLine($"Deleting code version {_env.CodeVersion}");
                if (!await _client.DELETE(WebDAVLocation.Cartridges, _env.CodeVersion))
                {
                    _console.Yellow("Code version was not deleted (may not exist)");
                }
            }

            await using (var ms = new MemoryStream())
            {
                _console.Write("Syncing code version ");
                _console.Yellow(_env.CodeVersion, eol: "");
                _console.Write(" on ");
                _console.Yellow(_env.Server);

                // Write a zip archive to the in memory stream
                CartridgeHelper.CartridgesToZipFile(cartridges, _env.CodeVersion, ms);

                var progressBar = _console.CreateProgressBar();

                if (!await _client.PUT(WebDAVLocation.Cartridges, $"{_env.CodeVersion}.zip", ms,
                                       progressBar.ProgressHandler))
                {
                    _logger.LogError("Could not upload code version");
                    return(1);
                }
            }

            _console.WriteLine("Extracting...");
            if (!await _client.UNZIP(WebDAVLocation.Cartridges, $"{_env.CodeVersion}.zip"))
            {
                _logger.LogError("Could not unzip code version");
                return(1);
            }

            //await _client.DELETE(WebDAVLocation.Cartridges, $"{_env.CodeVersion}.zip");
            _console.Green($"Successfully synced cartridges with {_env.Server}");

            if (deleteAndReactivate)
            {
                _console.Write("Activating code version...");
                if (!await _codeVersionsClient.ActivateCodeVersion(_env.CodeVersion))
                {
                    _logger.LogError("Could not activate code version");
                    return(1);
                }
            }

            return(0);
        }
        public override void ExecuteInstruction(long[] program, ref long pc)
        {
            var value = GetValue(0, program, pc);

            _output.Write(value);
        }