Add progress reporting to CLI

Closes #143
This commit is contained in:
Alexey Golub 2019-03-03 19:59:23 +02:00
parent 492003c75a
commit e493357a9f
4 changed files with 81 additions and 50 deletions

View file

@ -0,0 +1,28 @@
using System;
namespace DiscordChatExporter.Cli.Internal
{
internal class InlineProgress : IProgress<double>, IDisposable
{
private readonly int _posX;
private readonly int _posY;
public InlineProgress()
{
_posX = Console.CursorLeft;
_posY = Console.CursorTop;
}
public void Report(double progress)
{
Console.SetCursorPosition(_posX, _posY);
Console.WriteLine($"{progress:P1}");
}
public void Dispose()
{
Console.SetCursorPosition(_posX, _posY);
Console.WriteLine("Completed ✓");
}
}
}

View file

@ -1,6 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordChatExporter.Cli.Internal;
using DiscordChatExporter.Cli.Verbs.Options; using DiscordChatExporter.Cli.Verbs.Options;
using DiscordChatExporter.Core.Helpers; using DiscordChatExporter.Core.Helpers;
using DiscordChatExporter.Core.Services; using DiscordChatExporter.Core.Services;
@ -28,27 +29,29 @@ namespace DiscordChatExporter.Cli.Verbs
if (Options.MessageGroupLimit > 0) if (Options.MessageGroupLimit > 0)
settingsService.MessageGroupLimit = Options.MessageGroupLimit; settingsService.MessageGroupLimit = Options.MessageGroupLimit;
// Get chat log // Track progress
var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), Options.ChannelId, Console.Write($"Exporting channel [{Options.ChannelId}]... ");
Options.After, Options.Before); using (var progress = new InlineProgress())
// Generate file path if not set or is a directory
var filePath = Options.OutputPath;
if (filePath.IsBlank() || ExportHelper.IsDirectoryPath(filePath))
{ {
// Generate default file name // Get chat log
var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild, var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), Options.ChannelId,
chatLog.Channel, Options.After, Options.Before); Options.After, Options.Before, progress);
// Combine paths // Generate file path if not set or is a directory
filePath = Path.Combine(filePath ?? "", fileName); var filePath = Options.OutputPath;
if (filePath.IsBlank() || ExportHelper.IsDirectoryPath(filePath))
{
// Generate default file name
var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild,
chatLog.Channel, Options.After, Options.Before);
// Combine paths
filePath = Path.Combine(filePath ?? "", fileName);
}
// Export
exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit);
} }
// Export
exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit);
// Print result
Console.WriteLine($"Exported chat to [{filePath}]");
} }
} }
} }

View file

@ -3,6 +3,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordChatExporter.Cli.Internal;
using DiscordChatExporter.Cli.Verbs.Options; using DiscordChatExporter.Cli.Verbs.Options;
using DiscordChatExporter.Core.Exceptions; using DiscordChatExporter.Core.Exceptions;
using DiscordChatExporter.Core.Helpers; using DiscordChatExporter.Core.Helpers;
@ -42,25 +43,24 @@ namespace DiscordChatExporter.Cli.Verbs
{ {
try try
{ {
// Print current channel name // Track progress
Console.WriteLine($"Exporting chat from [{channel.Name}]..."); Console.Write($"Exporting channel [{channel.Name}]... ");
using (var progress = new InlineProgress())
{
// Get chat log
var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), channel,
Options.After, Options.Before, progress);
// Get chat log // Generate default file name
var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), channel, var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild,
Options.After, Options.Before); chatLog.Channel, Options.After, Options.Before);
// Generate default file name // Generate file path
var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild, var filePath = Path.Combine(Options.OutputPath ?? "", fileName);
chatLog.Channel, Options.After, Options.Before);
// Generate file path // Export
var filePath = Path.Combine(Options.OutputPath ?? "", fileName); exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit);
}
// Export
exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit);
// Print result
Console.WriteLine($"Exported chat to [{filePath}]");
} }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
{ {

View file

@ -3,6 +3,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordChatExporter.Cli.Internal;
using DiscordChatExporter.Cli.Verbs.Options; using DiscordChatExporter.Cli.Verbs.Options;
using DiscordChatExporter.Core.Exceptions; using DiscordChatExporter.Core.Exceptions;
using DiscordChatExporter.Core.Helpers; using DiscordChatExporter.Core.Helpers;
@ -43,25 +44,24 @@ namespace DiscordChatExporter.Cli.Verbs
{ {
try try
{ {
// Print current channel name // Track progress
Console.WriteLine($"Exporting chat from [{channel.Name}]..."); Console.Write($"Exporting channel [{channel.Name}]... ");
using (var progress = new InlineProgress())
{
// Get chat log
var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), channel,
Options.After, Options.Before, progress);
// Get chat log // Generate default file name
var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), channel, var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild,
Options.After, Options.Before); chatLog.Channel, Options.After, Options.Before);
// Generate default file name // Generate file path
var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild, var filePath = Path.Combine(Options.OutputPath ?? "", fileName);
chatLog.Channel, Options.After, Options.Before);
// Generate file path // Export
var filePath = Path.Combine(Options.OutputPath ?? "", fileName); exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit);
}
// Export
exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit);
// Print result
Console.WriteLine($"Exported chat to [{filePath}]");
} }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
{ {