[CLI] Cleanup

pull/497/head
Tyrrrz 4 years ago
parent 58464d8708
commit 1b8af364d3

@ -13,7 +13,6 @@ using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Exceptions; using DiscordChatExporter.Core.Exceptions;
using DiscordChatExporter.Core.Exporting; using DiscordChatExporter.Core.Exporting;
using DiscordChatExporter.Core.Utils.Extensions; using DiscordChatExporter.Core.Utils.Extensions;
using Spectre.Console;
using Tyrrrz.Extensions; using Tyrrrz.Extensions;
namespace DiscordChatExporter.Cli.Commands.Base namespace DiscordChatExporter.Cli.Commands.Base
@ -64,46 +63,37 @@ namespace DiscordChatExporter.Cli.Commands.Base
// Export // Export
try try
{ {
var guild = await Discord.GetGuildAsync(channel.GuildId); await progressContext.StartTaskAsync($"{channel.Category} / {channel.Name}", async progress =>
var request = new ExportRequest(
guild,
channel,
OutputPath,
ExportFormat,
After,
Before,
PartitionLimit,
ShouldDownloadMedia,
ShouldReuseMedia,
DateFormat
);
var progress = progressContext.AddTask(
$"{channel.Category} / {channel.Name}",
new ProgressTaskSettings {MaxValue = 1}
);
try
{ {
var guild = await Discord.GetGuildAsync(channel.GuildId);
var request = new ExportRequest(
guild,
channel,
OutputPath,
ExportFormat,
After,
Before,
PartitionLimit,
ShouldDownloadMedia,
ShouldReuseMedia,
DateFormat
);
await Exporter.ExportChannelAsync(request, progress); await Exporter.ExportChannelAsync(request, progress);
} });
finally
{
progress.StopTask();
}
} }
catch (DiscordChatExporterException ex) when (!ex.IsCritical) catch (DiscordChatExporterException ex) when (!ex.IsCritical)
{ {
errors[channel] = ex.Message; errors[channel] = ex.Message;
} }
}, ParallelLimit.ClampMin(1)); }, ParallelLimit.ClampMin(1));
await console.Output.WriteLineAsync();
}); });
await console.Output.WriteLineAsync();
// Print result // Print result
using (console.WithForegroundColor(ConsoleColor.Green)) using (console.WithForegroundColor(ConsoleColor.White))
{ {
await console.Output.WriteLineAsync( await console.Output.WriteLineAsync(
$"Successfully exported {channels.Count - errors.Count} channel(s)." $"Successfully exported {channels.Count - errors.Count} channel(s)."
@ -116,7 +106,11 @@ namespace DiscordChatExporter.Cli.Commands.Base
await console.Output.WriteLineAsync(); await console.Output.WriteLineAsync();
using (console.WithForegroundColor(ConsoleColor.Red)) using (console.WithForegroundColor(ConsoleColor.Red))
await console.Output.WriteLineAsync($"Failed to export {errors.Count} channel(s):"); {
await console.Output.WriteLineAsync(
$"Failed to export {errors.Count} channel(s):"
);
}
foreach (var (channel, error) in errors) foreach (var (channel, error) in errors)
{ {
@ -143,7 +137,7 @@ namespace DiscordChatExporter.Cli.Commands.Base
{ {
if (ShouldReuseMedia && !ShouldDownloadMedia) if (ShouldReuseMedia && !ShouldDownloadMedia)
{ {
throw new CommandException("The --reuse-media option cannot be used without the --media option."); throw new CommandException("Option --reuse-media cannot be used without --media.");
} }
return default; return default;

@ -1,4 +1,6 @@
using CliFx.Infrastructure; using System;
using System.Threading.Tasks;
using CliFx.Infrastructure;
using Spectre.Console; using Spectre.Console;
namespace DiscordChatExporter.Cli.Utils.Extensions namespace DiscordChatExporter.Cli.Utils.Extensions
@ -34,5 +36,22 @@ namespace DiscordChatExporter.Cli.Utils.Extensions
new ProgressBarColumn(), new ProgressBarColumn(),
new PercentageColumn() new PercentageColumn()
}); });
public static async ValueTask StartTaskAsync(
this ProgressContext progressContext,
string description,
Func<ProgressTask, ValueTask> performOperationAsync)
{
var progressTask = progressContext.AddTask(description, new ProgressTaskSettings {MaxValue = 1});
try
{
await performOperationAsync(progressTask);
}
finally
{
progressTask.StopTask();
}
}
} }
} }
Loading…
Cancel
Save