-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,043 additions
and
1,055 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
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; | ||
} | ||
} |
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
Oops, something went wrong.