Fixed a loading bar issue (#19)

This commit is contained in:
That_One_Nerd 2023-03-29 08:49:11 -04:00
parent a1d95170bd
commit 8170a86247
3 changed files with 34 additions and 2 deletions

View File

@ -342,6 +342,21 @@ public static class BaseModule
}); });
} }
[Command("testing")]
public static void Testing()
{
LoadingBarStart();
int count = 0;
for (float f = 0; f <= 1; f += 0.01f)
{
LoadingBarSet(f);
count++;
if (count % 10 == 0) Write("wowie!");
Thread.Sleep(15);
}
LoadingBarEnd();
}
[Command("exit")] [Command("exit")]
[Command("quit")] [Command("quit")]
public static void QuitShell(int code = 0) public static void QuitShell(int code = 0)

View File

@ -4,7 +4,7 @@ public class Shell
{ {
public const string Author = "That_One_Nerd"; public const string Author = "That_One_Nerd";
public const string Name = "SrcMod"; public const string Name = "SrcMod";
public const string Version = "Alpha 0.2.1"; public const string Version = "Alpha 0.2.2";
public readonly string? ShellDirectory; public readonly string? ShellDirectory;

View File

@ -7,6 +7,8 @@ public static class Tools
private static int loadingPosition = -1; private static int loadingPosition = -1;
private static int lastLoadingBufferSize = 0; private static int lastLoadingBufferSize = 0;
private static int lastLoadingValue = -1; private static int lastLoadingValue = -1;
private static float loadingBarValue = 0;
private static ConsoleColor loadingBarColor = Console.ForegroundColor;
public static bool LoadingBarEnabled { get; private set; } public static bool LoadingBarEnabled { get; private set; }
@ -101,6 +103,9 @@ public static class Tools
Int2 oldPos = (Console.CursorLeft, Console.CursorTop); Int2 oldPos = (Console.CursorLeft, Console.CursorTop);
loadingBarValue = value;
loadingBarColor = color ?? Console.ForegroundColor;
// Erase last bar. // Erase last bar.
Console.SetCursorPosition(0, loadingPosition); Console.SetCursorPosition(0, loadingPosition);
Console.Write(new string(' ', lastLoadingBufferSize)); Console.Write(new string(' ', lastLoadingBufferSize));
@ -119,14 +124,20 @@ public static class Tools
Write(right, newLine: false); Write(right, newLine: false);
if (oldPos.y == Console.CursorTop) oldPos.y++; if (oldPos.y == Console.CursorTop) oldPos.y++;
while (oldPos.y >= Console.BufferHeight)
{
Console.WriteLine();
oldPos.y--;
loadingPosition--;
}
Console.SetCursorPosition(oldPos.x, oldPos.y); Console.SetCursorPosition(oldPos.x, oldPos.y);
} }
public static void LoadingBarStart(float value = 0, int? position = null, ConsoleColor? color = null) public static void LoadingBarStart(float value = 0, int? position = null, ConsoleColor? color = null)
{ {
if (loadingPosition != -1) throw new("The loading bar has already been enabled."); if (loadingPosition != -1) throw new("The loading bar has already been enabled.");
loadingPosition = position ?? Console.CursorTop; loadingPosition = position ?? Console.CursorTop;
LoadingBarSet(value, color);
LoadingBarEnabled = true; LoadingBarEnabled = true;
LoadingBarSet(value, color);
} }
public static void Write(object? message, ConsoleColor? col = null, bool newLine = true) public static void Write(object? message, ConsoleColor? col = null, bool newLine = true)
@ -138,6 +149,12 @@ public static class Tools
else Console.Write(message); else Console.Write(message);
Console.ForegroundColor = prevCol; Console.ForegroundColor = prevCol;
if (newLine && LoadingBarEnabled && Console.CursorTop >= Console.BufferHeight - 1)
{
loadingPosition--;
LoadingBarSet(loadingBarValue, loadingBarColor);
}
} }
public static bool ValidateUnsafe() public static bool ValidateUnsafe()