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();
|
await Program.internalActionManager.Refresh();
|
||||||
break;
|
break;
|
||||||
case "list":
|
case "list":
|
||||||
|
var data = await ConsoleUtilities.ExecuteWithProgressBar(manager.GetAvailablePlugins(), "Loading plugins...");
|
||||||
|
|
||||||
var data = await manager.GetAvailablePlugins();
|
TableData tableData = new(new List<string> { "Name", "Description", "Type", "Version" });
|
||||||
var items = new List<string[]>
|
foreach (var plugin in data) tableData.AddRow(plugin);
|
||||||
{
|
tableData.HasRoundBorders = false;
|
||||||
new[] { "-", "-", "-", "-" },
|
tableData.PrintAsTable();
|
||||||
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);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,63 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Spectre.Console;
|
||||||
|
|
||||||
namespace DiscordBot.Utilities;
|
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 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()
|
private static readonly Dictionary<char, ConsoleColor> Colors = new()
|
||||||
{
|
{
|
||||||
{ 'g', ConsoleColor.Green },
|
{ 'g', ConsoleColor.Green },
|
||||||
@@ -22,13 +74,38 @@ public static class ConsoleUtilities
|
|||||||
return MathF.Abs(f - y) < 0.000001;
|
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>
|
/// <summary>
|
||||||
/// 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 string that represent the rows.</param>
|
||||||
public static void FormatAndAlignTable(List<string[]> data, TableFormat format)
|
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)
|
if (format == TableFormat.CENTER_EACH_COLUMN_BASED)
|
||||||
{
|
{
|
||||||
var tableLine = '-';
|
var tableLine = '-';
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
public enum TableFormat
|
public enum TableFormat
|
||||||
{
|
{
|
||||||
|
SPECTRE_CONSOLE,
|
||||||
CENTER_EACH_COLUMN_BASED,
|
CENTER_EACH_COLUMN_BASED,
|
||||||
CENTER_OVERALL_LENGTH,
|
CENTER_OVERALL_LENGTH,
|
||||||
DEFAULT
|
DEFAULT
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ public class PluginsManager
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<List<string[]>> GetAvailablePlugins()
|
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
|
try
|
||||||
{
|
{
|
||||||
var list = await ServerCom.ReadTextFromURL(PluginsLink);
|
var list = await ServerCom.ReadTextFromURL(PluginsLink);
|
||||||
@@ -85,9 +85,6 @@ public class PluginsManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data.Add(new[] { "-", "-", "-", "-" });
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
@@ -110,7 +107,7 @@ public class PluginsManager
|
|||||||
var split = item.Split(',');
|
var split = item.Split(',');
|
||||||
if (split[0] == pakName)
|
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]);
|
return new VersionString(split[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user