-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New progress reporting system (#1010)
* Replace IProgress<ProgressReport> with ITaskProgress and ITaskLogger in Core * Replace Progress<ProgressReport> with CliTaskProgress in CLI * Replace Progress<ProgressReport> with WpfTaskProgress in WPF * Delete ProgressHandler and ProgressReport * Use the progress reporter to ensure no race conditions * Generics don't work :( * Use distinct status template overloads * Fix warning preamble * Fix chat render time left calculation * DRY * Use logger instead of IProgress * Missed a spot * Use ITaskLogger * Fix missing downloading status in chat downloader * Simplify chat section progress reporting * Progress should never be null - log levels should be used instead * Move emoji decode failure log to Verbose
- Loading branch information
Showing
33 changed files
with
986 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
using System; | ||
using TwitchDownloaderCore.Interfaces; | ||
|
||
namespace TwitchDownloaderCLI.Tools | ||
{ | ||
public class CliTaskProgress : ITaskProgress | ||
{ | ||
private const string STATUS_PREAMBLE = "[STATUS] - "; | ||
private const string VERBOSE_LOG_PREAMBLE = "[VERBOSE] - "; | ||
private const string INFO_LOG_PREAMBLE = "[INFO] - "; | ||
private const string WARNING_LOG_PREAMBLE = "[WARNING] - "; | ||
private const string ERROR_LOG_PREAMBLE = "[ERROR] - "; | ||
private const string FFMPEG_LOG_PREAMBLE = "<FFMPEG> "; | ||
|
||
private string _status; | ||
private bool _statusIsTemplate; | ||
|
||
private bool _lastWriteHadNewLine = true; | ||
private int _lastStatusLength; | ||
private int _lastPercent = -1; | ||
private TimeSpan _lastTime1 = new(-1); | ||
private TimeSpan _lastTime2 = new(-1); | ||
|
||
public CliTaskProgress() | ||
{ | ||
// TODO: Take in ITwitchDownloaderArgs to configure log levels | ||
} | ||
|
||
public void SetStatus(string status) | ||
{ | ||
lock (this) | ||
{ | ||
_status = status; | ||
_statusIsTemplate = false; | ||
|
||
WriteNewLineMessage(STATUS_PREAMBLE, status); | ||
} | ||
} | ||
|
||
public void SetTemplateStatus(string status, int initialPercent) | ||
{ | ||
lock (this) | ||
{ | ||
_status = status; | ||
_statusIsTemplate = true; | ||
|
||
if (!_lastWriteHadNewLine) | ||
{ | ||
Console.WriteLine(); | ||
} | ||
|
||
_lastPercent = -1; // Ensure that the progress report runs | ||
ReportProgress(initialPercent); | ||
} | ||
} | ||
|
||
public void SetTemplateStatus(string status, int initialPercent, TimeSpan initialTime1, TimeSpan initialTime2) | ||
{ | ||
lock (this) | ||
{ | ||
_status = status; | ||
_statusIsTemplate = true; | ||
|
||
if (!_lastWriteHadNewLine) | ||
{ | ||
Console.WriteLine(); | ||
} | ||
|
||
_lastPercent = -1; // Ensure that the progress report runs | ||
ReportProgress(initialPercent, initialTime1, initialTime2); | ||
} | ||
} | ||
|
||
public void ReportProgress(int percent) | ||
{ | ||
lock (this) | ||
{ | ||
if ((!_lastWriteHadNewLine && _lastPercent == percent) | ||
|| !_statusIsTemplate) | ||
{ | ||
return; | ||
} | ||
|
||
var status = string.Format(_status, percent); | ||
_lastStatusLength = WriteSameLineMessage(STATUS_PREAMBLE, status, _lastStatusLength); | ||
|
||
_lastWriteHadNewLine = false; | ||
_lastPercent = percent; | ||
} | ||
} | ||
|
||
public void ReportProgress(int percent, TimeSpan time1, TimeSpan time2) | ||
{ | ||
lock (this) | ||
{ | ||
if ((!_lastWriteHadNewLine && _lastPercent == percent && _lastTime1 == time1 && _lastTime2 == time2) | ||
|| !_statusIsTemplate) | ||
{ | ||
return; | ||
} | ||
|
||
var status = string.Format(_status, percent, time1, time2); | ||
_lastStatusLength = WriteSameLineMessage(STATUS_PREAMBLE, status, _lastStatusLength); | ||
|
||
_lastWriteHadNewLine = false; | ||
_lastPercent = percent; | ||
_lastTime1 = time1; | ||
_lastTime2 = time2; | ||
} | ||
} | ||
|
||
private int WriteSameLineMessage(string preamble, string message, int previousMessageLength) | ||
{ | ||
if (!_lastWriteHadNewLine) | ||
{ | ||
Console.Write('\r'); | ||
} | ||
|
||
Console.Write(preamble); | ||
Console.Write(message); | ||
|
||
var messageLength = preamble.Length + message.Length; | ||
if (messageLength < previousMessageLength) | ||
{ | ||
// Ensure that the previous line is completely overwritten | ||
for (var i = 0; i < previousMessageLength - messageLength; i++) | ||
{ | ||
Console.Write(' '); | ||
} | ||
} | ||
|
||
return messageLength; | ||
} | ||
|
||
public void LogVerbose(string logMessage) | ||
{ | ||
lock (this) | ||
{ | ||
// WriteNewLineMessage(VERBOSE_LOG_PREAMBLE, logMessage); | ||
} | ||
} | ||
|
||
public void LogInfo(string logMessage) | ||
{ | ||
lock (this) | ||
{ | ||
WriteNewLineMessage(INFO_LOG_PREAMBLE, logMessage); | ||
} | ||
} | ||
|
||
public void LogWarning(string logMessage) | ||
{ | ||
lock (this) | ||
{ | ||
WriteNewLineMessage(WARNING_LOG_PREAMBLE, logMessage); | ||
} | ||
} | ||
|
||
public void LogError(string logMessage) | ||
{ | ||
lock (this) | ||
{ | ||
WriteNewLineMessage(ERROR_LOG_PREAMBLE, logMessage); | ||
} | ||
} | ||
|
||
public void LogFfmpeg(string logMessage) | ||
{ | ||
lock (this) | ||
{ | ||
WriteNewLineMessage(FFMPEG_LOG_PREAMBLE, logMessage); | ||
} | ||
} | ||
|
||
private void WriteNewLineMessage(string preamble, string message) | ||
{ | ||
if (!_lastWriteHadNewLine) | ||
{ | ||
Console.WriteLine(); | ||
} | ||
|
||
Console.Write(preamble); | ||
Console.WriteLine(message); | ||
_lastWriteHadNewLine = true; | ||
} | ||
} | ||
} |
Oops, something went wrong.