using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnrealBuildTool
{
///
/// Class to display an incrementing progress percentage. Handles progress markup and direct console output.
///
public class ProgressWriter : IDisposable
{
///
/// Global setting controlling whether to output markup
///
public static bool bWriteMarkup = false;
///
/// Whether to write messages to the console
///
bool bWriteToConsole;
string Message;
int NumCharsToBackspaceOver;
string CurrentProgressString;
///
/// Constructor
///
/// The message to display before the progress percentage
/// Whether to write progress message to the console
public ProgressWriter(string InMessage, bool bInWriteToConsole)
{
Message = InMessage;
bWriteToConsole = bInWriteToConsole;
if (!bWriteMarkup && bWriteToConsole)
{
Console.Write(Message + " ");
}
Write(0, 100);
}
///
/// Write the terminating newline
///
public void Dispose()
{
if (!bWriteMarkup && bWriteToConsole)
{
Console.WriteLine();
}
}
///
/// Writes the current progress
///
/// Numerator for the progress fraction
/// Denominator for the progress fraction
public void Write(int Numerator, int Denominator)
{
float ProgressValue = Denominator > 0 ? ((float)Numerator / (float)Denominator) : 1.0f;
string ProgressString = String.Format("{0}%", Math.Round(ProgressValue * 100.0f));
if (ProgressString != CurrentProgressString)
{
CurrentProgressString = ProgressString;
if (bWriteMarkup)
{
Log.WriteLine(LogEventType.Console, "@progress '{0}' {1}", Message, ProgressString);
}
else if (bWriteToConsole)
{
// Backspace over previous progress value
while (NumCharsToBackspaceOver-- > 0)
{
Console.Write("\b");
}
// Display updated progress string and keep track of how long it was
NumCharsToBackspaceOver = ProgressString.Length;
Console.Write(ProgressString);
}
}
}
}
}