Ejemplo n.º 1
0
            protected override async Task <int> OnExecuteAuthenticatedAsync(QBittorrentClient client, CommandLineApplication app, IConsole console)
            {
                const int resultsPerRequest = 100;
                var       id = await client.StartSearchAsync(Pattern,
                                                             Plugins ?? new[] { "enabled" },
                                                             Category ?? "all");

                Console.CancelKeyPress += OnCancel;

                int offset    = Offset;
                int remaining = Limit ?? int.MaxValue;
                int limit     = Math.Min(remaining, resultsPerRequest);

                var pager  = UsePager ? new Pager() : null;
                var target = (pager != null && pager.Enabled) ? new TextRenderTarget(pager.Writer) : null;

                int index = offset + 1;

                try
                {
                    SearchResults results;
                    int           total;
                    do
                    {
                        results = await client.GetSearchResultsAsync(id, offset, limit);

                        total = results.Total;

                        foreach (var result in results.Results)
                        {
                            Print(result);
                            index += 1;
                        }

                        offset    += results.Results.Count;
                        remaining -= results.Results.Count;
                        limit      = Math.Min(remaining, resultsPerRequest);
                    } while (results.Status == SearchJobStatus.Running && remaining > 0);

                    (pager?.Writer ?? console.Out).WriteLine($"Total results: {total:N0}");
                }
                finally
                {
                    target?.Dispose();
                    pager?.Dispose();

                    Console.CancelKeyPress -= OnCancel;
                    await client.StopSearchAsync(id);

                    await client.DeleteSearchAsync(id);
                }

                return(ExitCodes.Success);

                void Print(SearchResult result)
                {
                    var doc = new Document(
                        new Grid
                    {
                        Stroke   = new LineThickness(LineWidth.None),
                        Columns  = { UIHelper.FieldsColumns },
                        Children =
                        {
                            UIHelper.Row("#",            index),
                            UIHelper.Row("Name",         result.FileName),
                            UIHelper.Row("Size",         result.FileSize != null ? $"{result.FileSize:N0} bytes" : "n/a"),
                            GetVerboseData(),
                            UIHelper.Label("URL"),
                            UIHelper.Data(string.Empty),
                            UIHelper.Data(result.FileUrl)
                            .With(c => c[Grid.ColumnSpanProperty] = 2)
                            .With(c => c.Padding = default)