Skip to content

Commit

Permalink
Merge branch 'develop' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathoschild committed Nov 6, 2024
2 parents 51662b5 + 4d29f58 commit e0b5d50
Show file tree
Hide file tree
Showing 19 changed files with 1,043 additions and 1,055 deletions.
109 changes: 54 additions & 55 deletions StardewXnbHack/Framework/ConsoleProgressBar.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,72 @@
using System;

namespace StardewXnbHack.Framework
namespace StardewXnbHack.Framework;

/// <summary>Manages a progress bar written to the console.</summary>
internal class ConsoleProgressBar
{
/// <summary>Manages a progress bar written to the console.</summary>
internal class ConsoleProgressBar
{
/*********
** Fields
*********/
/// <summary>The total number of steps to perform.</summary>
private readonly int TotalSteps;
/*********
** Fields
*********/
/// <summary>The total number of steps to perform.</summary>
private readonly int TotalSteps;

/// <summary>The current step being performed.</summary>
private int CurrentStep;
/// <summary>The current step being performed.</summary>
private int CurrentStep;

/// <summary>The last line to which the progress bar was output, if any.</summary>
private int OutputLine = -1;
/// <summary>The last line to which the progress bar was output, if any.</summary>
private int OutputLine = -1;


/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="totalSteps">The total number of steps to perform.</param>
public ConsoleProgressBar(int totalSteps)
{
this.TotalSteps = totalSteps;
}
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="totalSteps">The total number of steps to perform.</param>
public ConsoleProgressBar(int totalSteps)
{
this.TotalSteps = totalSteps;
}

/// <summary>Increment the current step.</summary>
public void Increment()
{
this.CurrentStep++;
}
/// <summary>Increment the current step.</summary>
public void Increment()
{
this.CurrentStep++;
}

/// <summary>Print a progress bar to the console.</summary>
/// <param name="message">The message to print.</param>
/// <param name="removePrevious">Whether to remove the previously output progress bar.</param>
public void Print(string message, bool removePrevious = true)
{
if (removePrevious)
this.Erase();
/// <summary>Print a progress bar to the console.</summary>
/// <param name="message">The message to print.</param>
/// <param name="removePrevious">Whether to remove the previously output progress bar.</param>
public void Print(string message, bool removePrevious = true)
{
if (removePrevious)
this.Erase();

int percentage = (int)((this.CurrentStep / (this.TotalSteps * 1m)) * 100);
int percentage = (int)((this.CurrentStep / (this.TotalSteps * 1m)) * 100);

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"[{"".PadRight(percentage / 10, '#')}{"".PadRight(10 - percentage / 10, ' ')} {percentage}%] {message}");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"[{"".PadRight(percentage / 10, '#')}{"".PadRight(10 - percentage / 10, ' ')} {percentage}%] {message}");
Console.ResetColor();

this.OutputLine = Console.CursorTop - 1;
}
this.OutputLine = Console.CursorTop - 1;
}

/// <summary>Remove the last progress bar written to the console.</summary>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/8946847/262123" />.</remarks>
public void Erase()
{
if (this.OutputLine == -1)
return;
/// <summary>Remove the last progress bar written to the console.</summary>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/8946847/262123" />.</remarks>
public void Erase()
{
if (this.OutputLine == -1)
return;

bool isLastLine = this.OutputLine == Console.CursorTop - 1;
int currentLine = isLastLine
? this.OutputLine
: Console.CursorTop;
bool isLastLine = this.OutputLine == Console.CursorTop - 1;
int currentLine = isLastLine
? this.OutputLine
: Console.CursorTop;

Console.SetCursorPosition(0, this.OutputLine);
Console.Write(new string(' ', Console.BufferWidth));
Console.SetCursorPosition(0, currentLine);
Console.SetCursorPosition(0, this.OutputLine);
Console.Write(new string(' ', Console.BufferWidth));
Console.SetCursorPosition(0, currentLine);

this.OutputLine = -1;
}
this.OutputLine = -1;
}
}
185 changes: 92 additions & 93 deletions StardewXnbHack/Framework/DefaultConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,100 @@
using System.Linq;
using StardewXnbHack.ProgressHandling;

namespace StardewXnbHack.Framework
namespace StardewXnbHack.Framework;

/// <summary>Report updates to the console while the unpacker is running.</summary>
internal class DefaultConsoleLogger : IProgressLogger
{
/// <summary>Report updates to the console while the unpacker is running.</summary>
internal class DefaultConsoleLogger : IProgressLogger
/*********
** Fields
*********/
/// <summary>The context info for the current unpack run.</summary>
private readonly IUnpackContext Context;

/// <summary>Whether to show a 'press any key to exit' prompt on end.</summary>
private readonly bool ShowPressAnyKeyToExit;

/// <summary>The current progress bar written to the console.</summary>
private ConsoleProgressBar ProgressBar;


/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="context">The context info for the current unpack run.</param>
/// <param name="showPressAnyKeyToExit">Whether to show a 'press any key to exit' prompt on end.</param>
public DefaultConsoleLogger(IUnpackContext context, bool showPressAnyKeyToExit)
{
this.Context = context;
this.ShowPressAnyKeyToExit = showPressAnyKeyToExit;
}

/// <inheritdoc />
public void OnFatalError(string error)
{
this.PrintColor(error, ConsoleColor.Red);
}

/// <inheritdoc />
public void OnStepChanged(ProgressStep step, string message)
{
/*********
** Fields
*********/
/// <summary>The context info for the current unpack run.</summary>
private readonly IUnpackContext Context;

/// <summary>Whether to show a 'press any key to exit' prompt on end.</summary>
private readonly bool ShowPressAnyKeyToExit;

/// <summary>The current progress bar written to the console.</summary>
private ConsoleProgressBar ProgressBar;


/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="context">The context info for the current unpack run.</param>
/// <param name="showPressAnyKeyToExit">Whether to show a 'press any key to exit' prompt on end.</param>
public DefaultConsoleLogger(IUnpackContext context, bool showPressAnyKeyToExit)
{
this.Context = context;
this.ShowPressAnyKeyToExit = showPressAnyKeyToExit;
}

/// <inheritdoc />
public void OnFatalError(string error)
{
this.PrintColor(error, ConsoleColor.Red);
}

/// <inheritdoc />
public void OnStepChanged(ProgressStep step, string message)
{
this.ProgressBar?.Erase();

if (step == ProgressStep.Done)
Console.WriteLine();

Console.WriteLine(message);
}

/// <inheritdoc />
public void OnFileUnpacking(string relativePath)
{
if (this.ProgressBar == null)
this.ProgressBar = new ConsoleProgressBar(this.Context.Files.Count());

this.ProgressBar.Increment();
this.ProgressBar.Print(relativePath);
}

/// <inheritdoc />
public void OnFileUnpackFailed(string relativePath, UnpackFailedReason errorCode, string errorMessage)
{
ConsoleColor color = errorCode == UnpackFailedReason.UnsupportedFileType
? ConsoleColor.DarkYellow
: ConsoleColor.Red;

this.ProgressBar.Erase();
this.PrintColor($"{relativePath} => {errorMessage}", color);
}

/// <inheritdoc />
public void OnEnded()
{
if (this.ShowPressAnyKeyToExit)
DefaultConsoleLogger.PressAnyKeyToExit();
}

/// <summary>Show a 'press any key to exit' message and wait for a key press.</summary>
public static void PressAnyKeyToExit()
{
this.ProgressBar?.Erase();

if (step == ProgressStep.Done)
Console.WriteLine();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}


/*********
** Private methods
*********/
/// <summary>Print a message to the console with a foreground color.</summary>
/// <param name="message">The message to print.</param>
/// <param name="color">The foreground color to use.</param>
private void PrintColor(string message, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
}

Console.WriteLine(message);
}

/// <inheritdoc />
public void OnFileUnpacking(string relativePath)
{
if (this.ProgressBar == null)
this.ProgressBar = new ConsoleProgressBar(this.Context.Files.Count());

this.ProgressBar.Increment();
this.ProgressBar.Print(relativePath);
}

/// <inheritdoc />
public void OnFileUnpackFailed(string relativePath, UnpackFailedReason errorCode, string errorMessage)
{
ConsoleColor color = errorCode == UnpackFailedReason.UnsupportedFileType
? ConsoleColor.DarkYellow
: ConsoleColor.Red;

this.ProgressBar.Erase();
this.PrintColor($"{relativePath} => {errorMessage}", color);
}

/// <inheritdoc />
public void OnEnded()
{
if (this.ShowPressAnyKeyToExit)
DefaultConsoleLogger.PressAnyKeyToExit();
}

/// <summary>Show a 'press any key to exit' message and wait for a key press.</summary>
public static void PressAnyKeyToExit()
{
Console.WriteLine();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}


/*********
** Private methods
*********/
/// <summary>Print a message to the console with a foreground color.</summary>
/// <param name="message">The message to print.</param>
/// <param name="color">The foreground color to use.</param>
private void PrintColor(string message, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
}
}
Loading

0 comments on commit e0b5d50

Please sign in to comment.