Moved the loading bar to its own class. Small fix.
This commit is contained in:
parent
b869de9be8
commit
4d2e98ce42
79
SrcMod/Shell/LoadingBar.cs
Normal file
79
SrcMod/Shell/LoadingBar.cs
Normal file
@ -0,0 +1,79 @@
|
||||
namespace SrcMod.Shell;
|
||||
|
||||
internal static class LoadingBar
|
||||
{
|
||||
public static int position = -1;
|
||||
public static int bufferSize = 0;
|
||||
public static int lastValue = -1;
|
||||
public static float value = 0;
|
||||
public static ConsoleColor color = Console.ForegroundColor;
|
||||
|
||||
public static bool Enabled { get; private set; }
|
||||
|
||||
public static void End(bool clear = true)
|
||||
{
|
||||
if (position == -1) throw new("No loading bar is active.");
|
||||
|
||||
if (clear)
|
||||
{
|
||||
Int2 oldPos = (Console.CursorLeft, Console.CursorTop);
|
||||
|
||||
Console.CursorLeft = 0;
|
||||
Console.CursorTop = position;
|
||||
Console.Write(new string(' ', Console.BufferWidth));
|
||||
Console.CursorLeft = 0;
|
||||
|
||||
Console.SetCursorPosition(oldPos.x, oldPos.y);
|
||||
}
|
||||
position = -1;
|
||||
Enabled = false;
|
||||
}
|
||||
public static void Set(float value, ConsoleColor? color = null)
|
||||
{
|
||||
const string left = " --- [",
|
||||
right = "] --- ";
|
||||
int barSize = Console.BufferWidth - left.Length - right.Length,
|
||||
filled = (int)(barSize * value);
|
||||
|
||||
if (filled == lastValue) return;
|
||||
lastValue = filled;
|
||||
|
||||
Int2 oldPos = (Console.CursorLeft, Console.CursorTop);
|
||||
|
||||
LoadingBar.value = value;
|
||||
LoadingBar.color = color ?? Console.ForegroundColor;
|
||||
|
||||
// Erase last bar.
|
||||
Console.SetCursorPosition(0, position);
|
||||
Console.Write(new string(' ', bufferSize));
|
||||
Console.CursorLeft = 0;
|
||||
|
||||
// Add new bar.
|
||||
bufferSize = Console.BufferWidth;
|
||||
|
||||
Write(left, newLine: false);
|
||||
ConsoleColor oldFore = Console.ForegroundColor;
|
||||
|
||||
if (color is not null) Console.ForegroundColor = color.Value;
|
||||
Write(new string('=', filled), newLine: false);
|
||||
if (color is not null) Console.ForegroundColor = oldFore;
|
||||
Write(new string(' ', barSize - filled), newLine: false);
|
||||
Write(right, newLine: false);
|
||||
|
||||
if (oldPos.y == Console.CursorTop) oldPos.y++;
|
||||
while (oldPos.y >= Console.BufferHeight)
|
||||
{
|
||||
Console.WriteLine();
|
||||
oldPos.y--;
|
||||
position--;
|
||||
}
|
||||
Console.SetCursorPosition(oldPos.x, oldPos.y);
|
||||
}
|
||||
public static void Start(float value = 0, int? position = null, ConsoleColor? color = null)
|
||||
{
|
||||
if (LoadingBar.position != -1) throw new("The loading bar has already been enabled.");
|
||||
LoadingBar.position = position ?? Console.CursorTop;
|
||||
Enabled = true;
|
||||
Set(value, color);
|
||||
}
|
||||
}
|
||||
@ -45,7 +45,7 @@ public static class BaseModule
|
||||
|
||||
Write($"Copying directory \"{source}\" to \"{destination}\"...");
|
||||
|
||||
LoadingBarStart();
|
||||
LoadingBar.Start();
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
string file = files[i],
|
||||
@ -53,7 +53,7 @@ public static class BaseModule
|
||||
destFile = Path.Combine(destination, file);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(destFile)!);
|
||||
File.Copy(sourceFile, destFile);
|
||||
LoadingBarSet((i + 1) / (float)files.Length, ConsoleColor.DarkGreen);
|
||||
LoadingBar.Set((i + 1) / (float)files.Length, ConsoleColor.DarkGreen);
|
||||
Console.CursorLeft = 0;
|
||||
string message = $"{sourceFile}";
|
||||
int remainder = Console.BufferWidth - message.Length;
|
||||
@ -63,7 +63,7 @@ public static class BaseModule
|
||||
Write(message, newLine: false);
|
||||
}
|
||||
|
||||
LoadingBarEnd();
|
||||
LoadingBar.End();
|
||||
|
||||
Console.CursorLeft = 0;
|
||||
Write(new string(' ', Console.BufferWidth), newLine: false);
|
||||
@ -361,13 +361,13 @@ public static class BaseModule
|
||||
|
||||
Thread.Sleep(rand.Next(500, 1000));
|
||||
|
||||
LoadingBarStart();
|
||||
LoadingBar.Start();
|
||||
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
FileInfo file = new(files[i]);
|
||||
Thread.Sleep((int)(rand.Next(50, 100) * (file.Length >> 20)));
|
||||
LoadingBarSet((i + 1) / (float)files.Length, ConsoleColor.Red);
|
||||
LoadingBar.Set((i + 1) / (float)files.Length, ConsoleColor.Red);
|
||||
Console.CursorLeft = 0;
|
||||
string message = $"{files[i]}";
|
||||
int remainder = Console.BufferWidth - message.Length;
|
||||
@ -377,7 +377,7 @@ public static class BaseModule
|
||||
Write(message, newLine: false);
|
||||
}
|
||||
|
||||
LoadingBarEnd();
|
||||
LoadingBar.End();
|
||||
|
||||
Console.CursorLeft = 0;
|
||||
Write(new string(' ', Console.BufferWidth), newLine: false);
|
||||
|
||||
@ -168,7 +168,7 @@ public static class CompressionModule
|
||||
|
||||
int failed = 0;
|
||||
|
||||
LoadingBarStart();
|
||||
LoadingBar.Start();
|
||||
for (int i = 0; i < files.Count; i++)
|
||||
{
|
||||
bool failedThisTime = false;
|
||||
@ -181,7 +181,7 @@ public static class CompressionModule
|
||||
failedThisTime = true;
|
||||
failed++;
|
||||
}
|
||||
LoadingBarSet((i + 1) / (float)files.Count, failedThisTime ? ConsoleColor.Red : ConsoleColor.DarkGreen); ;
|
||||
LoadingBar.Set((i + 1) / (float)files.Count, failedThisTime ? ConsoleColor.Red : ConsoleColor.DarkGreen); ;
|
||||
Console.CursorLeft = 0;
|
||||
string message = $"{relative[i]}";
|
||||
int remainder = Console.BufferWidth - message.Length;
|
||||
@ -194,7 +194,7 @@ public static class CompressionModule
|
||||
archive.Dispose();
|
||||
writer.Dispose();
|
||||
|
||||
LoadingBarEnd();
|
||||
LoadingBar.End();
|
||||
|
||||
Console.CursorLeft = 0;
|
||||
Write(new string(' ', Console.BufferWidth), newLine: false);
|
||||
|
||||
@ -247,12 +247,12 @@ public class Shell
|
||||
catch (TargetInvocationException ex)
|
||||
{
|
||||
Write($"[ERROR] {ex.InnerException!.Message}", ConsoleColor.Red);
|
||||
if (LoadingBarEnabled) LoadingBarEnd();
|
||||
if (LoadingBar.Enabled) LoadingBar.End();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Write($"[ERROR] {ex.Message}", ConsoleColor.Red);
|
||||
if (LoadingBarEnabled) LoadingBarEnd();
|
||||
if (LoadingBar.Enabled) LoadingBar.End();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -2,16 +2,8 @@
|
||||
|
||||
namespace SrcMod.Shell;
|
||||
|
||||
public static class Tools
|
||||
internal static class Tools
|
||||
{
|
||||
private static int loadingPosition = -1;
|
||||
private static int lastLoadingBufferSize = 0;
|
||||
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 void DisplayWithPages(IEnumerable<string> lines, ConsoleColor? color = null)
|
||||
{
|
||||
int written = 0;
|
||||
@ -73,73 +65,6 @@ public static class Tools
|
||||
return allFiles;
|
||||
}
|
||||
|
||||
public static void LoadingBarEnd(bool clear = true)
|
||||
{
|
||||
if (loadingPosition == -1) throw new("No loading bar is active.");
|
||||
|
||||
if (clear)
|
||||
{
|
||||
Int2 oldPos = (Console.CursorLeft, Console.CursorTop);
|
||||
|
||||
Console.CursorLeft = 0;
|
||||
Console.CursorTop = loadingPosition;
|
||||
Console.Write(new string(' ', Console.BufferWidth));
|
||||
Console.CursorLeft = 0;
|
||||
|
||||
Console.SetCursorPosition(oldPos.x, oldPos.y);
|
||||
}
|
||||
loadingPosition = -1;
|
||||
LoadingBarEnabled = false;
|
||||
}
|
||||
public static void LoadingBarSet(float value, ConsoleColor? color = null)
|
||||
{
|
||||
const string left = " --- [",
|
||||
right = "] --- ";
|
||||
int barSize = Console.BufferWidth - left.Length - right.Length,
|
||||
filled = (int)(barSize * value);
|
||||
|
||||
if (filled == lastLoadingValue) return;
|
||||
lastLoadingValue = filled;
|
||||
|
||||
Int2 oldPos = (Console.CursorLeft, Console.CursorTop);
|
||||
|
||||
loadingBarValue = value;
|
||||
loadingBarColor = color ?? Console.ForegroundColor;
|
||||
|
||||
// Erase last bar.
|
||||
Console.SetCursorPosition(0, loadingPosition);
|
||||
Console.Write(new string(' ', lastLoadingBufferSize));
|
||||
Console.CursorLeft = 0;
|
||||
|
||||
// Add new bar.
|
||||
lastLoadingBufferSize = Console.BufferWidth;
|
||||
|
||||
Write(left, newLine: false);
|
||||
ConsoleColor oldFore = Console.ForegroundColor;
|
||||
|
||||
if (color is not null) Console.ForegroundColor = color.Value;
|
||||
Write(new string('=', filled), newLine: false);
|
||||
if (color is not null) Console.ForegroundColor = oldFore;
|
||||
Write(new string(' ', barSize - filled), newLine: false);
|
||||
Write(right, newLine: false);
|
||||
|
||||
if (oldPos.y == Console.CursorTop) oldPos.y++;
|
||||
while (oldPos.y >= Console.BufferHeight)
|
||||
{
|
||||
Console.WriteLine();
|
||||
oldPos.y--;
|
||||
loadingPosition--;
|
||||
}
|
||||
Console.SetCursorPosition(oldPos.x, oldPos.y);
|
||||
}
|
||||
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.");
|
||||
loadingPosition = position ?? Console.CursorTop;
|
||||
LoadingBarEnabled = true;
|
||||
LoadingBarSet(value, color);
|
||||
}
|
||||
|
||||
public static void Write(object? message, ConsoleColor? col = null, bool newLine = true)
|
||||
{
|
||||
ConsoleColor prevCol = Console.ForegroundColor;
|
||||
@ -150,10 +75,10 @@ public static class Tools
|
||||
|
||||
Console.ForegroundColor = prevCol;
|
||||
|
||||
if (newLine && LoadingBarEnabled && Console.CursorTop >= Console.BufferHeight - 1)
|
||||
if (newLine && LoadingBar.Enabled && Console.CursorTop >= Console.BufferHeight - 1)
|
||||
{
|
||||
loadingPosition--;
|
||||
LoadingBarSet(loadingBarValue, loadingBarColor);
|
||||
LoadingBar.position--;
|
||||
LoadingBar.Set(LoadingBar.value, LoadingBar.color);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user