using System; using System.Collections.Generic; namespace PluginManager.Others { public class Console_Utilities { /// /// Progress bar object /// public class ProgressBar { public int Max { get; set; } public string Message { get; set; } public ConsoleColor Color { get; init; } public ProgressBar(int max, string message) { Max = max; Message = message; var consoleColors = Enum.GetValues(typeof(ConsoleColor)); while ((Color = (ConsoleColor)consoleColors.GetValue(new Random().Next(consoleColors.Length))!) == ConsoleColor.White && Color != ConsoleColor.Black) ; } public void Update(int progress, double speed = -1, string? unit = null) { //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 = this.Color; 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; if (speed == -1 || unit == null) { if (progress == Max) Console.Write(progress.ToString() + " % ✓"); else Console.Write(progress.ToString() + " % "); } else Console.Write(progress.ToString() + $"{speed} {unit}/s "); } } /// /// A way to create a table based on input data /// /// The List of arrays of strings that represent the rows. public static void FormatAndAlignTable(List data) { char tableLine = '-'; char tableCross = '+'; char tableWall = '|'; int[] len = new int[data[0].Length]; foreach (var line in data) { for (int i = 0; i < line.Length; i++) if (line[i].Length > len[i]) len[i] = line[i].Length; } foreach (string[] row in data) { if (row[0][0] == tableLine) Console.Write(tableCross); else Console.Write(tableWall); for (int l = 0; l < row.Length; l++) { if (row[l][0] == tableLine) { for (int i = 0; i < len[l] + 4; ++i) Console.Write(tableLine); } else if (row[l].Length == len[l]) { Console.Write(" "); Console.Write(row[l]); Console.Write(" "); } else { int lenHalf = row[l].Length / 2; for (int i = 0; i < ((len[l] + 4) / 2 - lenHalf); ++i) Console.Write(" "); Console.Write(row[l]); for (int i = (len[l] + 4) / 2 + lenHalf + 1; i < len[l] + 4; ++i) Console.Write(" "); if (row[l].Length % 2 == 0) Console.Write(" "); } if (row[l][0] == tableLine) Console.Write(tableCross); else Console.Write(tableWall); } Console.WriteLine(); //end line } } public static void WriteColorText(string text, bool appendNewLine = true) { string[] words = text.Split(' '); ConsoleColor fg = Console.ForegroundColor; Dictionary colors = new Dictionary() { { "&g", ConsoleColor.Green }, { "&b", ConsoleColor.Blue }, { "&r", ConsoleColor.Red }, { "&m", ConsoleColor.Magenta }, { "&y", ConsoleColor.Yellow }, { "&c", fg } }; foreach (string word in words) { if (word.Length >= 2) { string prefix = word.Substring(0, 2); if (colors.ContainsKey(prefix)) Console.ForegroundColor = colors[prefix]; } string m = word; foreach (var key in colors.Keys) { m = m.Replace(key, ""); } Console.Write(m + " "); } if (appendNewLine) Console.Write('\n'); Console.ForegroundColor = fg; } } }