diff --git a/DiscordBot/Bot/Actions/Plugin.cs b/DiscordBot/Bot/Actions/Plugin.cs index 81a82da..bbfe0ad 100644 --- a/DiscordBot/Bot/Actions/Plugin.cs +++ b/DiscordBot/Bot/Actions/Plugin.cs @@ -43,20 +43,13 @@ public class Plugin : ICommandAction await Program.internalActionManager.Refresh(); break; case "list": - - var data = await manager.GetAvailablePlugins(); - var items = new List - { - new[] { "-", "-", "-", "-" }, - new[] { "Name", "Description", "Type", "Version" }, - new[] { "-", "-", "-", "-" } - }; - - foreach (var plugin in data) items.Add(new[] { plugin[0], plugin[1], plugin[2], plugin[3] }); - - items.Add(new[] { "-", "-", "-", "-" }); - - ConsoleUtilities.FormatAndAlignTable(items, TableFormat.DEFAULT); + var data = await ConsoleUtilities.ExecuteWithProgressBar(manager.GetAvailablePlugins(), "Loading plugins..."); + + TableData tableData = new(new List { "Name", "Description", "Type", "Version" }); + foreach (var plugin in data) tableData.AddRow(plugin); + tableData.HasRoundBorders = false; + tableData.PrintAsTable(); + break; diff --git a/DiscordBot/Utilities/Console Utilities.cs b/DiscordBot/Utilities/Console Utilities.cs index ce8d074..26f9190 100644 --- a/DiscordBot/Utilities/Console Utilities.cs +++ b/DiscordBot/Utilities/Console Utilities.cs @@ -1,11 +1,63 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; +using System.Threading.Tasks; +using Spectre.Console; namespace DiscordBot.Utilities; +public class TableData +{ + public List Columns; + public List Rows; + + public bool IsEmpty => Rows.Count == 0; + public bool HasRoundBorders { get; set; } = true; + + public TableData(List columns) + { + Columns = columns; + Rows = new List(); + } + + public TableData(string[] columns) + { + Columns = columns.ToList(); + Rows = new List(); + } + + public void AddRow(string[] row) + { + Rows.Add(row); + } +} + public static class ConsoleUtilities { + + public static async Task ExecuteWithProgressBar(Task function, string message) + { + T result = default; + await AnsiConsole.Progress() + .Columns(new ProgressColumn[] + { + new TaskDescriptionColumn(), + new ProgressBarColumn(), + new PercentageColumn(), + }) + .StartAsync(async ctx => + { + var task = ctx.AddTask(message); + task.IsIndeterminate = true; + result = await function; + task.Increment(100); + + }); + + return result; + } + private static readonly Dictionary Colors = new() { { 'g', ConsoleColor.Green }, @@ -22,13 +74,38 @@ public static class ConsoleUtilities return MathF.Abs(f - y) < 0.000001; } + public static void PrintAsTable(this TableData tableData) + { + var table = new Table(); + table.Border(tableData.HasRoundBorders ? TableBorder.Rounded : TableBorder.Square); + table.AddColumns(tableData.Columns.ToArray()); + foreach (var row in tableData.Rows) + table.AddRow(row); + + AnsiConsole.Write(table); + } + /// /// A way to create a table based on input data /// - /// The List of arrays of strings that represent the rows. + /// The List of arrays of string that represent the rows. public static void FormatAndAlignTable(List data, TableFormat format) { + if (format == TableFormat.SPECTRE_CONSOLE) + { + var table = new Table(); + table.Border(TableBorder.Rounded); + table.AddColumns(data[0]); + data.RemoveAt(0); + foreach (var row in data) + table.AddRow(row); + + AnsiConsole.Write(table); + + return; + } + if (format == TableFormat.CENTER_EACH_COLUMN_BASED) { var tableLine = '-'; diff --git a/DiscordBot/Utilities/Enums.cs b/DiscordBot/Utilities/Enums.cs index 371ac6c..892a255 100644 --- a/DiscordBot/Utilities/Enums.cs +++ b/DiscordBot/Utilities/Enums.cs @@ -2,6 +2,7 @@ public enum TableFormat { + SPECTRE_CONSOLE, CENTER_EACH_COLUMN_BASED, CENTER_OVERALL_LENGTH, DEFAULT diff --git a/PluginManager/Online/PluginsManager.cs b/PluginManager/Online/PluginsManager.cs index 6a4ea19..64e8932 100644 --- a/PluginManager/Online/PluginsManager.cs +++ b/PluginManager/Online/PluginsManager.cs @@ -42,7 +42,7 @@ public class PluginsManager /// public async Task> GetAvailablePlugins() { - Config.Logger.Log("Got data from " + VersionsLink, this, LogLevel.INFO); + // Config.Logger.Log("Got data from " + VersionsLink, this, LogLevel.INFO); try { var list = await ServerCom.ReadTextFromURL(PluginsLink); @@ -85,9 +85,6 @@ public class PluginsManager } } } - - data.Add(new[] { "-", "-", "-", "-" }); - return data; } catch (Exception exception) @@ -110,7 +107,7 @@ public class PluginsManager var split = item.Split(','); if (split[0] == pakName) { - Config.Logger.Log("Searched for " + pakName + " and found " + split[1] + " as version.", LogLevel.INFO); + // Config.Logger.Log("Searched for " + pakName + " and found " + split[1] + " as version.", LogLevel.INFO); return new VersionString(split[1]); } }