コード例 #1
0
        public void Choose(IAnsiConsole console, int pageSize = 5)
        {
            var prompt = new SelectionPrompt <string>
            {
                Title    = "What is your favorite color?",
                PageSize = pageSize
            }.AddChoices("blue", "purple", "red", "orange", "yellow", "green");
            var answer = console.Prompt(prompt);

            console.WriteLine(answer);
        }
コード例 #2
0
        private static string AskSport(IAnsiConsole console)
        {
            console.WriteLine();
            console.Write(new Rule("[yellow]Choices[/]").RuleStyle("grey").LeftAligned());

            return(console.Prompt(
                       new TextPrompt <string>("What's your [green]favorite sport[/]?")
                       .InvalidChoiceMessage("[red]That's not a sport![/]")
                       .DefaultValue("Sport?")
                       .AddChoice("Soccer")
                       .AddChoice("Hockey")
                       .AddChoice("Basketball")));
        }
コード例 #3
0
        private List <string> MultiPrompt(IAnsiConsole ansiConsole, string prompt)
        {
            var answers = new List <string>();

            ansiConsole.WriteLine(prompt);
            while (true)
            {
                var textPrompt = new TextPrompt <string>("> ")
                {
                    AllowEmpty = true
                };
                var answer = ansiConsole.Prompt(textPrompt);
                if (string.IsNullOrEmpty(answer))
                {
                    return(answers);
                }

                answers.Add(answer);
            }
        }
コード例 #4
0
        public override async Task <int> ExecuteAsync(CommandContext context, BrentOptions settings)
        {
            var archives = new List <BrentArchive>()
            {
                new("https://downloads.brentozar.com/StackOverflow-SQL-Server-2010.torrent",
                    "Small: 10GB database as of 2010", "small"),
                new("https://downloads.brentozar.com/StackOverflow2013.torrent", "Medium: 50GB database as of 2013",
                    "medium"),
                new("https://downloads.brentozar.com/StackOverflowCore.torrent", "Large 150GB database as of 2019",
                    "large"),
                new("https://downloads.brentozar.com/StackOverflow-SQL-Server-202006.torrent",
                    "Extra-Large: current 381GB database as of 2020/06", "extra-large"),
            };

            var archiveName = settings.Archive;

            if (string.IsNullOrWhiteSpace(archiveName))
            {
                var choice = _console.Prompt(
                    new SelectionPrompt <BrentArchive>()
                    .PageSize(10)
                    .Title("Pick an archive to download")
                    .AddChoices(archives));
                archiveName = choice.ShortName;
            }

            var archive = archives.FirstOrDefault(a =>
                                                  a.ShortName.Equals(archiveName, StringComparison.InvariantCultureIgnoreCase));

            if (archive == null)
            {
                throw new SoddiException($"Could not find an archive matching \"{archiveName}\"");
            }

            var outputPath = settings.Output;

            if (string.IsNullOrWhiteSpace(outputPath))
            {
                outputPath = _fileSystem.Directory.GetCurrentDirectory();
            }

            if (!_fileSystem.Directory.Exists(outputPath))
            {
                throw new SoddiException($"Output path {outputPath} not found");
            }

            var downloadedFiles = await _torrentDownloader.Download(archive.Url, settings.EnablePortForwarding,
                                                                    outputPath,
                                                                    CancellationToken.None);

            var sevenZipFiles = downloadedFiles.Where(i =>
                                                      _fileSystem.Path.GetExtension(i).Equals(".7z", StringComparison.InvariantCultureIgnoreCase));

            var stopWatch = Stopwatch.StartNew();


            var progressBar = _console.Progress()
                              .AutoClear(false)
                              .Columns(new ProgressColumn[]
            {
                new SpinnerColumn {
                    CompletedText = Emoji.Known.CheckMark
                }, new TaskDescriptionColumn(),
                new ProgressBarColumn(), new PercentageColumn(), new TransferSpeedColumn(),
                new RemainingTimeColumn()
            });

            progressBar.Start(ctx =>
            {
                foreach (var sevenZipFile in sevenZipFiles)
                {
                    using var stream          = _fileSystem.File.OpenRead(sevenZipFile);
                    using var sevenZipArchive = SevenZipArchive.Open(stream);

                    var tasks = sevenZipArchive.Entries.ToImmutableDictionary(
                        e => e.Key,
                        e => ctx.AddTask(e.Key, new ProgressTaskSettings {
                        MaxValue = e.Size, AutoStart = false
                    }));

                    foreach (var entry in sevenZipArchive.Entries)
                    {
                        var currentTask = tasks[entry.Key];
                        currentTask.StartTask();
                        var totalRead = 0L;

                        void Handler(object?sender, CompressedBytesReadEventArgs args)
                        {
                            var diff = args.CurrentFilePartCompressedBytesRead - totalRead;
                            currentTask.Increment(diff);
                            totalRead = args.CurrentFilePartCompressedBytesRead;
                        }

                        sevenZipArchive.CompressedBytesRead += Handler;
                        entry.WriteToDirectory(outputPath, new ExtractionOptions {
                            Overwrite = true
                        });
                        sevenZipArchive.CompressedBytesRead -= Handler;
                    }
                }
            });

            stopWatch.Stop();
            _console.MarkupLine($"Extraction complete in [blue]{stopWatch.Elapsed.Humanize()}[/].");

            return(0);
        }