New method to execute using a progress bar feedback on process
This commit is contained in:
@@ -43,20 +43,13 @@ public class Plugin : ICommandAction
|
||||
await Program.internalActionManager.Refresh();
|
||||
break;
|
||||
case "list":
|
||||
|
||||
var data = await manager.GetAvailablePlugins();
|
||||
var items = new List<string[]>
|
||||
{
|
||||
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<string> { "Name", "Description", "Type", "Version" });
|
||||
foreach (var plugin in data) tableData.AddRow(plugin);
|
||||
tableData.HasRoundBorders = false;
|
||||
tableData.PrintAsTable();
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
@@ -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<string> Columns;
|
||||
public List<string[]> Rows;
|
||||
|
||||
public bool IsEmpty => Rows.Count == 0;
|
||||
public bool HasRoundBorders { get; set; } = true;
|
||||
|
||||
public TableData(List<string> columns)
|
||||
{
|
||||
Columns = columns;
|
||||
Rows = new List<string[]>();
|
||||
}
|
||||
|
||||
public TableData(string[] columns)
|
||||
{
|
||||
Columns = columns.ToList();
|
||||
Rows = new List<string[]>();
|
||||
}
|
||||
|
||||
public void AddRow(string[] row)
|
||||
{
|
||||
Rows.Add(row);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConsoleUtilities
|
||||
{
|
||||
|
||||
public static async Task<T> ExecuteWithProgressBar<T>(Task<T> 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<char, ConsoleColor> 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);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A way to create a table based on input data
|
||||
/// </summary>
|
||||
/// <param name="data">The List of arrays of strings that represent the rows.</param>
|
||||
/// <param name="data">The List of arrays of string that represent the rows.</param>
|
||||
public static void FormatAndAlignTable(List<string[]> 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 = '-';
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
public enum TableFormat
|
||||
{
|
||||
SPECTRE_CONSOLE,
|
||||
CENTER_EACH_COLUMN_BASED,
|
||||
CENTER_OVERALL_LENGTH,
|
||||
DEFAULT
|
||||
|
||||
@@ -42,7 +42,7 @@ public class PluginsManager
|
||||
/// <returns></returns>
|
||||
public async Task<List<string[]>> 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]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user