Skip to content

Instantly share code, notes, and snippets.

@Platonenkov
Created October 24, 2022 17:34
Show Gist options
  • Save Platonenkov/14e61ae40989afe2a5d7ddf0bb00cf1f to your computer and use it in GitHub Desktop.
Save Platonenkov/14e61ae40989afe2a5d7ddf0bb00cf1f to your computer and use it in GitHub Desktop.
Console Table
using System;
using System.Collections.Generic;
using System.Linq;
internal static class ConsoleTable
{
public static void Run()
{
double[] Rows = { 26000, 28000, 30000, 32000, 34000, 36000, 38000, 40000, 42000, 44000, 46000 };
double[] Cols = { 0, 11000, 20000, 30000, 40000 };
double[,] Table =
{
{ 210, 210, 210, 210, 210}, // 0
{ 210, 210, 210, 210, 210}, // 1
{ 210, 210, 210, 210, 210}, // 2
{ 210, 210, 210, 210, 218}, // 3
{ 210, 210, 210, 210, 226}, // 4
{ 210, 210, 210, 216, 233}, // 5
{ 210, 210, 214, 223, 240}, // 6
{ 210, 210, 218, 228, 247}, // 7
{ 216, 216, 224, 235, 255}, // 8
{ 221, 221, 229, 242, 262}, // 9
{ 227, 227, 235, 249, 269} // 10
};
Console.WriteLine();
Console.Clear();
var table_width = Console.WindowWidth;
PrintLine(table_width);
var header = new List<object> { string.Empty };
header.AddRange(Cols.Cast<object>());
PrintRow(table_width, header);
for (var i = 0; i < Rows.Length; i++)
{
var row = new List<object> { Rows[i] };
row.AddRange(Table.GetRow(i).Cast<object>());
PrintRow(table_width, row);
}
Console.ReadLine();
Console.WriteLine(result);
Console.WriteLine($"Y = {result_y}");
Console.WriteLine($"X = {result_x}");
Console.ReadLine();
}
private static void PrintLine(int TableWidth) => Console.WriteLine(new string('-', TableWidth));
private static void PrintRow(int TableWidth, IReadOnlyCollection<object> columns)
{
var width = (TableWidth - columns.Count) / columns.Count;
var row = columns.Aggregate("|", (current, column) => $"{current}{AlignCentre(column.ToString()!, width)}|");
Console.WriteLine(row);
}
private static string AlignCentre(string text, int width)
{
text = text.Length > width
? $"{text[..(width - 3)]}..."
: text;
return string.IsNullOrEmpty(text)
? new string(' ', width)
: text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment