-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAsciiTableGenerator.cs
92 lines (84 loc) · 3.61 KB
/
AsciiTableGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
namespace AsciiTableGenerators
{
public class AsciiTableGenerator
{
public static StringBuilder CreateAsciiTableFromDataTable(DataTable table)
{
var lenghtByColumnDictionary = GetTotalSpaceForEachColumn(table);
var tableBuilder = new StringBuilder();
AppendColumns(table, tableBuilder, lenghtByColumnDictionary);
AppendRows(table, lenghtByColumnDictionary, tableBuilder);
return tableBuilder;
}
private static void AppendRows(DataTable table, IReadOnlyDictionary<int, int> lenghtByColumnDictionary,
StringBuilder tableBuilder)
{
for (var i = 0; i < table.Rows.Count; i++)
{
var rowBuilder = new StringBuilder();
for (var j = 0; j < table.Columns.Count; j++)
{
rowBuilder.Append(PadWithSpaceAndSeperator(table.Rows[i][j].ToString().Trim(),
lenghtByColumnDictionary[j]));
}
tableBuilder.AppendLine(rowBuilder.ToString());
}
}
private static void AppendColumns(DataTable table, StringBuilder builder,
IReadOnlyDictionary<int, int> lenghtByColumnDictionary)
{
for (var i = 0; i < table.Columns.Count; i++)
{
var columName = table.Columns[i].ColumnName.Trim();
var paddedColumNames = PadWithSpaceAndSeperator(ToTitleCase(columName), lenghtByColumnDictionary[i]);
builder.Append(paddedColumNames);
}
builder.AppendLine();
builder.AppendLine(string.Join("", Enumerable.Repeat("-", builder.ToString().Length - 3).ToArray()));
}
private static string ToTitleCase(string columnName)
{
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(columnName.Replace("_", " "));
}
private static Dictionary<int, int> GetTotalSpaceForEachColumn(DataTable table)
{
var lengthByColumn = new Dictionary<int, int>();
for (var i = 0; i < table.Columns.Count; i++)
{
var length = new int[table.Rows.Count];
for (var j = 0; j < table.Rows.Count; j++)
{
length[j] = table.Rows[j][i].ToString().Trim().Length;
}
lengthByColumn[i] = length.Max();
}
return CompareToColumnNameLengthAndUpdate(table, lengthByColumn);
}
private static Dictionary<int, int> CompareToColumnNameLengthAndUpdate(DataTable table,
IReadOnlyDictionary<int, int> lenghtByColumnDictionary)
{
var dictionary = new Dictionary<int, int>();
for (var i = 0; i < table.Columns.Count; i++)
{
var columnNameLength = table.Columns[i].ColumnName.Trim().Length;
dictionary[i] = columnNameLength > lenghtByColumnDictionary[i]
? columnNameLength
: lenghtByColumnDictionary[i];
}
return dictionary;
}
private static string PadWithSpaceAndSeperator(string value, int totalColumnLength)
{
var remaningSpace = value.Length < totalColumnLength
? totalColumnLength - value.Length
: value.Length - totalColumnLength;
var spaces = string.Join("", Enumerable.Repeat(" ", remaningSpace).ToArray());
return value + spaces + " | ";
}
}
}