Added a new ProgressBar type

This commit is contained in:
2022-08-24 21:34:06 +03:00
parent 6da9828e5c
commit 77aad985fa
10 changed files with 111 additions and 56 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -6,6 +6,8 @@ using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord;
using DiscordBot.Discord.Core; using DiscordBot.Discord.Core;
using PluginManager; using PluginManager;
@@ -210,10 +212,8 @@ public class Program
/// <param name="args">The arguments</param> /// <param name="args">The arguments</param>
private static async Task HandleInput(string[] args) private static async Task HandleInput(string[] args)
{ {
var b = await StartNoGUI();
consoleCommandsHandler = new ConsoleCommandsHandler(b.client);
var len = args.Length;
var len = args.Length;
if (len == 3 && args[0] == "/download") if (len == 3 && args[0] == "/download")
{ {
@@ -225,16 +225,22 @@ public class Program
return; return;
} }
if (len > 0 && args[0] == "/remplug") if (len > 0 && args[0] == "/test")
{ {
int p = 1;
string plugName = Functions.MergeStrings(args, 1); bool allowed = true;
Console.WriteLine("Starting to remove " + plugName); Console.CancelKeyPress += (sender, e) => allowed = false;
await ConsoleCommandsHandler.ExecuteCommad("remplug " + plugName); Console_Utilities.ProgressBar bar = new Console_Utilities.ProgressBar();
loadPluginsOnStartup = true; Console.WriteLine("Press Ctrl + C to stop.");
len = 0; while (p <= int.MaxValue - 1 && allowed)
{
bar.Update(ProgressBarType.NO_END, 100 / p);
await Task.Delay(100);
p++;
} }
return;
}
if (len > 0 && (args.Contains("--cmd") || args.Contains("--args") || args.Contains("--nomessage"))) if (len > 0 && (args.Contains("--cmd") || args.Contains("--args") || args.Contains("--nomessage")))
{ {
@@ -248,6 +254,19 @@ public class Program
var b = await StartNoGUI();
consoleCommandsHandler = new ConsoleCommandsHandler(b.client);
if (len > 0 && args[0] == "/remplug")
{
string plugName = Functions.MergeStrings(args, 1);
Console.WriteLine("Starting to remove " + plugName);
await ConsoleCommandsHandler.ExecuteCommad("remplug " + plugName);
loadPluginsOnStartup = true;
len = 0;
}
if (len == 0 || (args[0] != "--exec" && args[0] != "--execute")) if (len == 0 || (args[0] != "--exec" && args[0] != "--execute"))
{ {

View File

@@ -1,4 +1,5 @@
using AngleSharp.Dom; using AngleSharp.Dom;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@@ -22,7 +23,7 @@ namespace MusicCommands
internal async Task DownloadAudioFile() internal async Task DownloadAudioFile()
{ {
Process proc = new Process(); Process proc = new Process();
proc.StartInfo.FileName = "MusicDownloader.exe"; proc.StartInfo.FileName = "MusicDownloader";
proc.StartInfo.Arguments = $"{Url},{Name}"; proc.StartInfo.Arguments = $"{Url},{Name}";
proc.StartInfo.UseShellExecute = false; proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardOutput = true;

View File

@@ -185,7 +185,7 @@ public class ConsoleCommandsHandler
{ {
while (isExtracting) while (isExtracting)
{ {
bar.Update(proc); bar.Update(ProgressBarType.NORMAL, proc);
if (proc >= 99.9f) if (proc >= 99.9f)
isExtracting = false; isExtracting = false;
Thread.Sleep(500); Thread.Sleep(500);
@@ -194,10 +194,10 @@ public class ConsoleCommandsHandler
).Start ).Start
).Start(); ).Start();
await Functions.ExtractArchive("./" + split[1], "./", extractProgress, UnzipProgressType.PercentageFromTotalSize); await Functions.ExtractArchive("./" + split[1], "./", extractProgress, UnzipProgressType.PercentageFromTotalSize);
bar.Update(100f); bar.Update(ProgressBarType.NORMAL, 100f);
isExtracting = false; isExtracting = false;
await Task.Delay(1000); await Task.Delay(1000);
bar.Update(100); bar.Update(ProgressBarType.NORMAL, 100);
Console.WriteLine("\n"); Console.WriteLine("\n");
File.Delete("./" + split[1]); File.Delete("./" + split[1]);
} }

View File

@@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using PluginManager.Others; using PluginManager.Others;
using OperatingSystem = PluginManager.Others.OperatingSystem; using OperatingSystem = PluginManager.Others.OperatingSystem;
namespace PluginManager.Online; namespace PluginManager.Online;
@@ -86,7 +88,7 @@ public class PluginsManager
data.Add(new[] { "-", "-", "-", "-", "-" }); data.Add(new[] { "-", "-", "-", "-", "-" });
Console_Utilities.FormatAndAlignTable(data); Console_Utilities.FormatAndAlignTable(data, TableFormat.CENTER_EACH_COLUMN_BASED);
} }
catch (Exception exception) catch (Exception exception)
{ {

View File

@@ -63,7 +63,7 @@ namespace PluginManager.Online
{ {
while (isDownloading) while (isDownloading)
{ {
pbar.Update(c_progress); pbar.Update(ProgressBarType.NORMAL, c_progress);
if (c_progress == 100f) if (c_progress == 100f)
break; break;
Thread.Sleep(500); Thread.Sleep(500);
@@ -76,7 +76,7 @@ namespace PluginManager.Online
c_progress = pbar.Max; c_progress = pbar.Max;
pbar.Update(100f); pbar.Update(ProgressBarType.NORMAL, 100f);
isDownloading = false; isDownloading = false;
} }
} }

View File

@@ -1,4 +1,5 @@
using Discord; using Discord;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -36,18 +37,50 @@ namespace PluginManager.Others
public ConsoleColor Color { get; init; } public ConsoleColor Color { get; init; }
public bool NoColor { get; init; } public bool NoColor { get; init; }
private int BarLength = 32;
private int position = 1;
private bool positive = true;
public void Update(float progress, double speed = -1, string? unit = null) public void Update(ProgressBarType type, float progress)
{
switch (type)
{
case ProgressBarType.NORMAL:
UpdateNormal(progress);
return;
case ProgressBarType.NO_END:
if (progress <= 99.9f)
UpdateNoEnd();
return;
}
}
private void UpdateNoEnd()
{ {
Console.CursorLeft = 0; Console.CursorLeft = 0;
Console.Write("["); Console.Write("[");
Console.CursorLeft = 32; for (int i = 1; i <= position; i++)
Console.Write(" ");
Console.Write("<==()==>");
position += positive ? 1 : -1;
for (int i = position; i <= BarLength - 1 - (positive ? 0 : 2); i++)
Console.Write(" ");
Console.Write("]");
if (position == BarLength - 1 || position == 1)
positive = !positive;
}
private void UpdateNormal(float progress)
{
Console.CursorLeft = 0;
Console.Write("[");
Console.CursorLeft = BarLength;
Console.Write("]"); Console.Write("]");
Console.CursorLeft = 1; Console.CursorLeft = 1;
float onechunk = 30.0f / Max; float onechunk = 30.0f / Max;
int position = 1;
for (int i = 0; i < onechunk * progress; i++) for (int i = 0; i < onechunk * progress; i++)
{ {
Console.BackgroundColor = NoColor ? ConsoleColor.Black : this.Color; Console.BackgroundColor = NoColor ? ConsoleColor.Black : this.Color;
@@ -55,7 +88,7 @@ namespace PluginManager.Others
Console.Write("#"); Console.Write("#");
} }
for (int i = position; i <= 31; i++) for (int i = position; i <= BarLength - 1; i++)
{ {
Console.BackgroundColor = NoColor ? ConsoleColor.Black : ConsoleColor.DarkGray; Console.BackgroundColor = NoColor ? ConsoleColor.Black : ConsoleColor.DarkGray;
Console.CursorLeft = position++; Console.CursorLeft = position++;
@@ -64,16 +97,11 @@ namespace PluginManager.Others
Console.CursorLeft = 35; Console.CursorLeft = 35;
Console.BackgroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Black;
if (speed is -1 || unit == null)
{
if (progress.CanAproximateTo(Max)) if (progress.CanAproximateTo(Max))
Console.Write(progress + " % ✓"); Console.Write(progress + " % ✓");
else else
Console.Write(MathF.Round(progress, 2) + " % "); Console.Write(MathF.Round(progress, 2) + " % ");
} }
else
Console.Write(progress + $"{speed} {unit}/s ");
}
} }
@@ -84,7 +112,7 @@ namespace PluginManager.Others
/// A way to create a table based on input data /// A way to create a table based on input data
/// </summary> /// </summary>
/// <param name="data">The List of arrays of strings that represent the rows.</param> /// <param name="data">The List of arrays of strings that represent the rows.</param>
public static void FormatAndAlignTable(List<string[]> data, TableFormat format = TableFormat.CENTER_EACH_COLUMN_BASED) public static void FormatAndAlignTable(List<string[]> data, TableFormat format)
{ {
if (format == TableFormat.CENTER_EACH_COLUMN_BASED) if (format == TableFormat.CENTER_EACH_COLUMN_BASED)
{ {

View File

@@ -33,3 +33,4 @@ public enum UnzipProgressType { PercentageFromNumberOfFiles, PercentageFromTotal
public enum TableFormat { CENTER_EACH_COLUMN_BASED, CENTER_OVERALL_LENGTH, DEFAULT } public enum TableFormat { CENTER_EACH_COLUMN_BASED, CENTER_OVERALL_LENGTH, DEFAULT }
public enum SaveType { NORMAL, BACKUP } public enum SaveType { NORMAL, BACKUP }
public enum ProgressBarType { NORMAL, NO_END }

View File

@@ -36,7 +36,7 @@ namespace PluginManager.Others
/// <summary> /// <summary>
/// Archives folder /// Archives folder
/// </summary> /// </summary>
public static readonly string pakFolder = @"./Data/Resources/PAK/"; public static readonly string pakFolder = @"./Data/PAKS/";
/// <summary> /// <summary>
/// Beta testing folder /// Beta testing folder
@@ -50,7 +50,7 @@ namespace PluginManager.Others
/// <param name="FileName">The file name that is inside the archive or its full path</param> /// <param name="FileName">The file name that is inside the archive or its full path</param>
/// <param name="archFile">The archive location from the PAKs folder</param> /// <param name="archFile">The archive location from the PAKs folder</param>
/// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns> /// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns>
public static Stream? ReadFromPakAsync(string FileName, string archFile) public static async Task<Stream?> ReadFromPakAsync(string FileName, string archFile)
{ {
archFile = pakFolder + archFile; archFile = pakFolder + archFile;
Directory.CreateDirectory(pakFolder); Directory.CreateDirectory(pakFolder);
@@ -58,7 +58,11 @@ namespace PluginManager.Others
using ZipArchive archive = ZipFile.OpenRead(archFile); using ZipArchive archive = ZipFile.OpenRead(archFile);
ZipArchiveEntry? entry = archive.GetEntry(FileName); ZipArchiveEntry? entry = archive.GetEntry(FileName);
return entry?.Open(); if (entry is null) return Stream.Null;
MemoryStream stream = new MemoryStream();
await (entry?.Open()!).CopyToAsync(stream);
return stream;
} }
/// <summary> /// <summary>