mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 04:58:24 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
1 changed files with 784842 additions and 0 deletions
76
Brutal.ImGui.Abstractions/ConsoleWindow/TableString.cs
Normal file
76
Brutal.ImGui.Abstractions/ConsoleWindow/TableString.cs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
using System.Text;
|
||||
|
||||
namespace Brutal.ImGuiApi.Abstractions;
|
||||
|
||||
public class TableString
|
||||
{
|
||||
private readonly List<Row> _rows = new();
|
||||
|
||||
private readonly List<int> _columnWidths = new();
|
||||
|
||||
public readonly struct Row
|
||||
{
|
||||
public readonly String[] Values;
|
||||
|
||||
public Row(string[] values)
|
||||
{
|
||||
Values = values;
|
||||
}
|
||||
|
||||
public static Row Create(params string[] values) => new(values);
|
||||
|
||||
public void ToConsole(ConsoleWindow console)
|
||||
{
|
||||
foreach (var value in Values)
|
||||
console.Print(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddRow(params string[] values) => _rows.Add(new Row(values));
|
||||
|
||||
public void ToConsole(ConsoleWindow console)
|
||||
{
|
||||
// populate column widths
|
||||
foreach (var row in _rows)
|
||||
{
|
||||
for (var i = 0; i < row.Values.Length; i++)
|
||||
{
|
||||
if (i >= _columnWidths.Count)
|
||||
_columnWidths.Add(0);
|
||||
var rowColumnValue = row.Values[i].Length + 1;
|
||||
if (rowColumnValue > _columnWidths[i])
|
||||
_columnWidths[i] = rowColumnValue;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var row in _rows)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
for (var i = 0; i < row.Values.Length; i++)
|
||||
{
|
||||
var value = row.Values[i];
|
||||
switch (i)
|
||||
{
|
||||
case > 0:
|
||||
sb.Append(' ');
|
||||
break;
|
||||
case 0:
|
||||
sb.Append("| ");
|
||||
break;
|
||||
}
|
||||
|
||||
sb.Append(value.PadRight(_columnWidths[i]));
|
||||
sb.Append(" |");
|
||||
}
|
||||
console.Print(sb.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void AddRow(Row toRow) => _rows.Add(toRow);
|
||||
|
||||
public void Sort()
|
||||
{
|
||||
// sort rows by first column
|
||||
_rows.Sort((a, b) => string.Compare(a.Values[0], b.Values[0], StringComparison.Ordinal));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue