Files
SethDiscordBot/PluginManager/Others/Console Utilities.cs
2022-05-03 21:58:44 +03:00

154 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PluginManager.Others
{
public class Console_Utilities
{
/// <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>
public static void FormatAndAlignTable(List<string[]> 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)
{
//Console.Write("\t");
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
}
//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>
/// Write the text using color options( &g-green; &b-blue; &r-red; &c-clear; )
///
/// </summary>
/// <param name="text">The text</param>
public static void WriteColorText(string text, bool appendNewLine = true)
{
string[] words = text.Split(' ');
Dictionary<string, ConsoleColor> colors = new Dictionary<string, ConsoleColor>()
{
{"&g", ConsoleColor.Green },
{"&b", ConsoleColor.Blue },
{"&r", ConsoleColor.Red },
{"&m", ConsoleColor.Magenta },
{"&c", Console.ForegroundColor }
};
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.Replace("&g", "").Replace("&b", "").Replace("&r", "").Replace("&c", "").Replace("&m", "");
Console.Write(m + " ");
}
if (appendNewLine)
Console.Write('\n');
}
}
}