public CommandExecutionModel(IFileSystemRepository fileSystemRepository, Func <Guid> getSourceKey, Func <string, Task> addCommandToHistory, Func <Task <EnvironmentalConfig> > getEnvironmentalConfig, Func <Task> saveTextEditorContent)
 {
     FileSystemRepository        = fileSystemRepository ?? throw new ArgumentNullException(nameof(fileSystemRepository));
     FileSystemProviderConnector = new FileSystemProviderConnector(fileSystemRepository);
     GetSourceKey           = getSourceKey ?? throw new ArgumentNullException(nameof(getSourceKey));
     AddCommandToHistory    = addCommandToHistory ?? throw new ArgumentNullException(nameof(addCommandToHistory));
     GetEnvironmentalConfig = getEnvironmentalConfig ?? throw new ArgumentNullException(nameof(getEnvironmentalConfig));
     SaveTextEditorContent  = saveTextEditorContent ?? throw new ArgumentNullException(nameof(saveTextEditorContent));
 }
        private async Task ExecuteCommand(string command)
        {
            if (command == "#test-color")
            {
                OutputText = GetTestColor();
                return;
            }

            await SaveTextEditorContent();
            await AddCommandToHistory(command);

            var sourceKey = GetSourceKey();
            var config    = await GetEnvironmentalConfig();

            var fileSystem = await FileSystemProviderConnector.GetProvider();  // TODO set current folder, set encoding

            var engine = new ServiceEngine(
                configureContext: context =>
            {
                context.IsAtty   = config.IsAtty;
                context.TimeZone = config.TimeZone;
                context.SetEnvironmentVariables(config.EnvironmentVariables);
            },
                createCustomProvider: mem =>
            {
                mem.Attach(w => new MemoryAnsiTextWriter(w));
                return(new ApplicationServiceProvider(
                           fileSystemProviderFactory: () => fileSystem,
                           virtualConsoleProviderFactory: () => new VirtualConsoleProvider(mem.ConsoleInput, mem.ConsoleOutput, mem.ConsoleError)));
            });

            ServiceSession session;

            if (sourceKey == FileSystemRepository.InputStream.Key)
            {
                session = engine.CreateSession("-f /dev/stdin", await FileSystemRepository.GetFileContent(sourceKey));
            }
            else
            {
                session = engine.CreateSession($"-f {(await FileSystemRepository.GetItem(sourceKey)).Path}");
            }

            var response = session.ExecuteCommand(command);

            OutputText = response.OutputText;
            ErrorText  = response.ErrorText;
            ResultCode = response.Status;

            // TODO - save possible changes in FS
            //if (fileSystem.HasChanges())
            //    await FileSystemProviderConnector.ApplyChanges();
        }