This commit is contained in:
2022-05-04 19:59:42 +03:00
parent 3567392dd9
commit d787457753
2 changed files with 144 additions and 58 deletions

View File

@@ -12,6 +12,7 @@ using PluginManager.Online;
using System.Diagnostics; using System.Diagnostics;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
namespace DiscordBot namespace DiscordBot
{ {
@@ -83,7 +84,10 @@ namespace DiscordBot
if (listLanguagAtStartup) if (listLanguagAtStartup)
await languageManager.ListAllLanguages(); await languageManager.ListAllLanguages();
Console_Utilities.ProgressBar pbar;
IProgress<float> progress = null; IProgress<float> progress = null;
Task t;
int prg = 0;
while (true) while (true)
@@ -136,9 +140,28 @@ namespace DiscordBot
string path = "./Data/Plugins/" + info[0] + "s/" + name + ".dll"; string path = "./Data/Plugins/" + info[0] + "s/" + name + ".dll";
progress = new Progress<float>(percent => progress = new Progress<float>(percent =>
{ {
Console.Title = $"Downloading {info[0]}: {name} ({MathF.Round(percent, 2)}%)"; prg = (int)percent;
/*Console.Title = $"Downloading: {Lname} ({MathF.Round(percent, 2)}%)";*/
}); });
pbar = new Console_Utilities.ProgressBar(100, "Downloading Plugin");
Console.WriteLine();
t = new Task(async () =>
{
while (true)
{
if (prg == 100) break;
pbar.Update(prg);
Thread.Sleep(500);
}
});
new Thread(t.Start).Start();
await ServerCom.DownloadFileAsync(info[1], path, progress); await ServerCom.DownloadFileAsync(info[1], path, progress);
//Console.WriteLine();
pbar.Update(100);
prg = 100;
Console.WriteLine("\n");
// check requirements if any // check requirements if any
@@ -152,13 +175,33 @@ namespace DiscordBot
foreach (var line in lines) foreach (var line in lines)
{ {
string[] split = line.Split(','); string[] split = line.Split(',');
Console.WriteLine($"Downloading item: {split[1]}"); Console.WriteLine($"\nDownloading item: {split[1]}");
progress = new Progress<float>(bytes => progress = new Progress<float>(percent =>
{ {
Console.Title = $"Downloading {MathF.Round(bytes, 2)}% ({i}/{lines.Count})"; prg = (int)percent;
}); });
pbar = new Console_Utilities.ProgressBar(100, "Downloading Requirements");
bool finish = false;
t = new Task(async () =>
{
while (!finish)
{
pbar.Update(prg);
//if (prg == 100) break;
Thread.Sleep(500);
}
});
new Thread(t.Start).Start();
await ServerCom.DownloadFileAsync(split[0], "./" + split[1], progress); await ServerCom.DownloadFileAsync(split[0], "./" + split[1], progress);
Console_Utilities.WriteColorText($"Downloaded item {split[1]}"); // prg = 100;
finish = true;
pbar.Update(100);
Console.WriteLine();
i++; i++;
} }
Console.WriteLine(); Console.WriteLine();
@@ -197,11 +240,31 @@ namespace DiscordBot
{ {
string path2 = Functions.langFolder + Lname + ".lng"; string path2 = Functions.langFolder + Lname + ".lng";
IProgress<float> progress1 = new Progress<float>(percent =>
progress = new Progress<float>(percent =>
{ {
Console.Title = $"Downloading Language: {Lname} ({MathF.Round(percent, 2)}%)"; prg = (int)percent;
/*Console.Title = $"Downloading: {Lname} ({MathF.Round(percent, 2)}%)";*/
}); });
await ServerCom.DownloadFileAsync(link[0], path2, progress1); pbar = new Console_Utilities.ProgressBar(100, "Downloading Language");
t = new Task(async () =>
{
while (true)
{
if (prg == 100) break;
pbar.Update(prg);
Thread.Sleep(500);
}
});
new Thread(t.Start).Start();
await ServerCom.DownloadFileAsync(link[0], path2, progress);
pbar.Update(100);
prg = 100;
Console.WriteLine("\n");
} }
else Console_Utilities.WriteColorText("The language you are trying to download (&b" + Lname + "&c) is not compatible with the version of this bot. User &glistlang &ccommand in order to see all available languages for your current version !\n" + link[1]); else Console_Utilities.WriteColorText("The language you are trying to download (&b" + Lname + "&c) is not compatible with the version of this bot. User &glistlang &ccommand in order to see all available languages for your current version !\n" + link[1]);
break; break;
@@ -393,6 +456,20 @@ namespace DiscordBot
private static async Task HandleInput(string[] args) private static async Task HandleInput(string[] args)
{ {
if (args.Length > 0)
if (args[0] == "progress")
{
Console_Utilities.ProgressBar bar = new Console_Utilities.ProgressBar(100, "Download");
for (int i = 0; i <= 100; i++)
{
bar.Update(i);
await Task.Delay(10);
}
return;
}
if (args.Length == 0) if (args.Length == 0)
{ {
if (File.Exists("./ref/startupArguments.txt")) if (File.Exists("./ref/startupArguments.txt"))

View File

@@ -2,12 +2,71 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace PluginManager.Others namespace PluginManager.Others
{ {
public class Console_Utilities public class Console_Utilities
{ {
/// <summary>
/// Progress bar object
/// </summary>
public class ProgressBar
{
public int Progress { get; set; }
public int Max { get; set; }
public string Message { get; set; }
public ProgressBar(int max, string message)
{
Max = max;
Message = message;
}
public async void Update(int progress, bool r = false)
{
//progress bar
Console.CursorLeft = 0;
Console.Write("[");
Console.CursorLeft = 32;
Console.Write("]");
Console.CursorLeft = 1;
float onechunk = 30.0f / Max;
int position = 1;
for (int i = 0; i < onechunk * progress; i++)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.CursorLeft = position++;
Console.Write(" ");
}
for (int i = position; i <= 31; i++)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.CursorLeft = position++;
Console.Write(" ");
}
Console.CursorLeft = 35;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(progress.ToString() + " of " + Max.ToString() + " ");
if (r == false)
Update(progress, true);
}
public void Finish()
{
Console.Write("\r{0} {1}%", Message, 100);
Console.WriteLine();
}
}
/// <summary> /// <summary>
/// A way to create a table based on input data /// A way to create a table based on input data
@@ -65,56 +124,6 @@ namespace PluginManager.Others
Console.WriteLine(); //end line Console.WriteLine(); //end line
} }
//Obsolite
#region Old Code -> Spacing by the lomgest item in any cell
/*
int maxLen = 0;
foreach (string[] row in data)
foreach (string s in row)
if (s.Length > maxLen)
maxLen = s.Length;
int div = (maxLen + 4) / 2;
foreach (string[] row in data)
{
//Console.Write("\t");
if (row[0] == "-") Console.Write("+");
else Console.Write("|");
foreach (string s in row)
{
if (s == "-")
{
for (int i = 0; i < maxLen + 4; ++i)
Console.Write("-");
}
else if (s.Length == maxLen)
{
Console.Write(" ");
Console.Write(s);
Console.Write(" ");
}
else
{
int lenHalf = s.Length / 2;
for (int i = 0; i < div - lenHalf; ++i)
Console.Write(" ");
Console.Write(s);
for (int i = div + lenHalf + 1; i < maxLen + 4; ++i)
Console.Write(" ");
if (s.Length % 2 == 0)
Console.Write(" ");
}
if (s == "-") Console.Write("+");
else Console.Write("|");
}
Console.WriteLine(); //end line
}*/
#endregion
} }
/// <summary> /// <summary>