diff --git a/DiscordChatExporter.Cli/Internal/InlineProgress.cs b/DiscordChatExporter.Cli/Internal/InlineProgress.cs new file mode 100644 index 00000000..f45d3a8b --- /dev/null +++ b/DiscordChatExporter.Cli/Internal/InlineProgress.cs @@ -0,0 +1,28 @@ +using System; + +namespace DiscordChatExporter.Cli.Internal +{ + internal class InlineProgress : IProgress, 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 ✓"); + } + } +} \ No newline at end of file diff --git a/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs index 0669701e..7b2c5e9c 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Threading.Tasks; +using DiscordChatExporter.Cli.Internal; using DiscordChatExporter.Cli.Verbs.Options; using DiscordChatExporter.Core.Helpers; using DiscordChatExporter.Core.Services; @@ -28,27 +29,29 @@ namespace DiscordChatExporter.Cli.Verbs if (Options.MessageGroupLimit > 0) settingsService.MessageGroupLimit = Options.MessageGroupLimit; - // Get chat log - var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), Options.ChannelId, - Options.After, Options.Before); - - // Generate file path if not set or is a directory - var filePath = Options.OutputPath; - if (filePath.IsBlank() || ExportHelper.IsDirectoryPath(filePath)) + // Track progress + Console.Write($"Exporting channel [{Options.ChannelId}]... "); + using (var progress = new InlineProgress()) { - // Generate default file name - var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild, - chatLog.Channel, Options.After, Options.Before); + // Get chat log + var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), Options.ChannelId, + Options.After, Options.Before, progress); - // Combine paths - filePath = Path.Combine(filePath ?? "", fileName); + // Generate file path if not set or is a directory + 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}]"); } } } \ No newline at end of file diff --git a/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs index f49e6a91..5d49bd39 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; +using DiscordChatExporter.Cli.Internal; using DiscordChatExporter.Cli.Verbs.Options; using DiscordChatExporter.Core.Exceptions; using DiscordChatExporter.Core.Helpers; @@ -42,25 +43,24 @@ namespace DiscordChatExporter.Cli.Verbs { try { - // Print current channel name - Console.WriteLine($"Exporting chat from [{channel.Name}]..."); + // Track progress + 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 - var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), channel, - Options.After, Options.Before); + // Generate default file name + var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild, + chatLog.Channel, Options.After, Options.Before); - // Generate default file name - var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild, - chatLog.Channel, Options.After, Options.Before); + // Generate file path + var filePath = Path.Combine(Options.OutputPath ?? "", fileName); - // Generate file path - var filePath = Path.Combine(Options.OutputPath ?? "", fileName); - - // Export - exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit); - - // Print result - Console.WriteLine($"Exported chat to [{filePath}]"); + // Export + exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit); + } } catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) { diff --git a/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs index 3aae103b..7fa39ce7 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; +using DiscordChatExporter.Cli.Internal; using DiscordChatExporter.Cli.Verbs.Options; using DiscordChatExporter.Core.Exceptions; using DiscordChatExporter.Core.Helpers; @@ -43,25 +44,24 @@ namespace DiscordChatExporter.Cli.Verbs { try { - // Print current channel name - Console.WriteLine($"Exporting chat from [{channel.Name}]..."); + // Track progress + 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 - var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), channel, - Options.After, Options.Before); + // Generate default file name + var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild, + chatLog.Channel, Options.After, Options.Before); - // Generate default file name - var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild, - chatLog.Channel, Options.After, Options.Before); + // Generate file path + var filePath = Path.Combine(Options.OutputPath ?? "", fileName); - // Generate file path - var filePath = Path.Combine(Options.OutputPath ?? "", fileName); - - // Export - exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit); - - // Print result - Console.WriteLine($"Exported chat to [{filePath}]"); + // Export + exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit); + } } catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) {