mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-04-23 06:24:48 +00:00
Refactor
This commit is contained in:
@@ -3,7 +3,7 @@ using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Services;
|
||||
using DiscordChatExporter.Cli.Internal;
|
||||
using CliFx.Utilities;
|
||||
using DiscordChatExporter.Core.Models;
|
||||
using DiscordChatExporter.Core.Services;
|
||||
using DiscordChatExporter.Core.Services.Helpers;
|
||||
@@ -50,29 +50,27 @@ namespace DiscordChatExporter.Cli.Commands
|
||||
SettingsService.DateFormat = DateFormat;
|
||||
|
||||
console.Output.Write($"Exporting channel [{channel.Name}]... ");
|
||||
using (var progress = new InlineProgress(console))
|
||||
var progress = console.CreateProgressTicker();
|
||||
|
||||
// Get chat log
|
||||
var chatLog = await DataService.GetChatLogAsync(GetToken(), channel, After, Before, progress);
|
||||
|
||||
// Generate file path if not set or is a directory
|
||||
var filePath = OutputPath;
|
||||
if (filePath.IsNullOrWhiteSpace() || ExportHelper.IsDirectoryPath(filePath))
|
||||
{
|
||||
// Get chat log
|
||||
var chatLog = await DataService.GetChatLogAsync(GetToken(), channel, After, Before, progress);
|
||||
// Generate default file name
|
||||
var fileName = ExportHelper.GetDefaultExportFileName(ExportFormat, chatLog.Guild,
|
||||
chatLog.Channel, After, Before);
|
||||
|
||||
// Generate file path if not set or is a directory
|
||||
var filePath = OutputPath;
|
||||
if (filePath.IsNullOrWhiteSpace() || ExportHelper.IsDirectoryPath(filePath))
|
||||
{
|
||||
// Generate default file name
|
||||
var fileName = ExportHelper.GetDefaultExportFileName(ExportFormat, chatLog.Guild,
|
||||
chatLog.Channel, After, Before);
|
||||
|
||||
// Combine paths
|
||||
filePath = Path.Combine(filePath ?? "", fileName);
|
||||
}
|
||||
|
||||
// Export
|
||||
await ExportService.ExportChatLogAsync(chatLog, filePath, ExportFormat, PartitionLimit);
|
||||
|
||||
// Report successful completion
|
||||
progress.ReportCompletion();
|
||||
// Combine paths
|
||||
filePath = Path.Combine(filePath ?? "", fileName);
|
||||
}
|
||||
|
||||
// Export
|
||||
await ExportService.ExportChatLogAsync(chatLog, filePath, ExportFormat, PartitionLimit);
|
||||
|
||||
console.Output.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
using System;
|
||||
using CliFx.Services;
|
||||
|
||||
namespace DiscordChatExporter.Cli.Internal
|
||||
{
|
||||
internal class InlineProgress : IProgress<double>, IDisposable
|
||||
{
|
||||
private readonly IConsole _console;
|
||||
|
||||
private string _lastOutput = "";
|
||||
private bool _isCompleted;
|
||||
|
||||
public InlineProgress(IConsole console)
|
||||
{
|
||||
_console = console;
|
||||
}
|
||||
|
||||
private void ResetCursorPosition()
|
||||
{
|
||||
foreach (var c in _lastOutput)
|
||||
_console.Output.Write('\b');
|
||||
}
|
||||
|
||||
public void Report(double progress)
|
||||
{
|
||||
// If output is not redirected - reset cursor position and write progress
|
||||
if (!_console.IsOutputRedirected)
|
||||
{
|
||||
ResetCursorPosition();
|
||||
_console.Output.Write(_lastOutput = $"{progress:P1}");
|
||||
}
|
||||
}
|
||||
|
||||
public void ReportCompletion() => _isCompleted = true;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// If output is not redirected - reset cursor position
|
||||
if (!_console.IsOutputRedirected)
|
||||
{
|
||||
ResetCursorPosition();
|
||||
}
|
||||
|
||||
// Inform about completion
|
||||
_console.Output.WriteLine(_isCompleted ? "Completed ✓" : "Failed X");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user