pull/236/head
Alexey Golub 5 years ago
parent 69cfe1845b
commit 05c551f80c

@ -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,8 +50,8 @@ 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);
@ -70,9 +70,7 @@ namespace DiscordChatExporter.Cli.Commands
// Export
await ExportService.ExportChatLogAsync(chatLog, filePath, ExportFormat, PartitionLimit);
// Report successful completion
progress.ReportCompletion();
}
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");
}
}
}

@ -32,8 +32,7 @@ namespace DiscordChatExporter.Core.Services
{
// Create request
const string apiRoot = "https://discordapp.com/api/v6";
using (var request = new HttpRequestMessage(HttpMethod.Get, $"{apiRoot}/{resource}/{endpoint}"))
{
using var request = new HttpRequestMessage(HttpMethod.Get, $"{apiRoot}/{resource}/{endpoint}");
// Set authorization header
request.Headers.Authorization = token.Type == AuthTokenType.Bot
? new AuthenticationHeaderValue("Bot", token.Value)
@ -53,8 +52,7 @@ namespace DiscordChatExporter.Core.Services
}
// Get response
using (var response = await _httpClient.SendAsync(request))
{
using var response = await _httpClient.SendAsync(request);
// Check status code
// We throw our own exception here because default one doesn't have status code
if (!response.IsSuccessStatusCode)
@ -65,8 +63,6 @@ namespace DiscordChatExporter.Core.Services
// Parse
return JToken.Parse(raw);
}
}
});
}

@ -42,7 +42,7 @@ namespace DiscordChatExporter.Core.Services
Directory.CreateDirectory(dirPath);
// Render chat log to output file
using (var writer = File.CreateText(filePath))
await using var writer = File.CreateText(filePath);
await CreateRenderer(chatLog, format).RenderAsync(writer);
}

@ -12,7 +12,7 @@ namespace DiscordChatExporter.Core.Services.Helpers
public static bool IsDirectoryPath(string path) =>
path.Last() == Path.DirectorySeparatorChar ||
path.Last() == Path.AltDirectorySeparatorChar ||
(Path.GetExtension(path).IsNullOrWhiteSpace() && !File.Exists(path));
Path.GetExtension(path).IsNullOrWhiteSpace() && !File.Exists(path);
public static string GetDefaultExportFileName(ExportFormat format, Guild guild, Channel channel,
DateTimeOffset? after = null, DateTimeOffset? before = null)

Loading…
Cancel
Save