-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTable.cs
240 lines (197 loc) · 9.88 KB
/
Table.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleTable
{
public class Table
{
private const string TopLeftJoint = "┌";
private const string TopRightJoint = "┐";
private const string BottomLeftJoint = "└";
private const string BottomRightJoint = "┘";
private const string TopJoint = "┬";
private const string BottomJoint = "┴";
private const string LeftJoint = "├";
private const string MiddleJoint = "┼";
private const string RightJoint = "┤";
private const char HorizontalLine = '─';
private const string VerticalLine = "│";
private string[] _headers;
private List<string[]> _rows = new List<string[]>();
public int Padding { get; set; } = 1;
public bool HeaderTextAlignRight { get; set; }
public bool RowTextAlignRight { get; set; }
public void SetHeaders(params string[] headers)
{
_headers = headers;
}
public void AddRow(params string[] row)
{
_rows.Add(row);
}
public void ClearRows()
{
_rows.Clear();
}
private int[] GetMaxCellWidths(List<string[]> table)
{
var maximumColumns = 0;
foreach (var row in table)
{
if (row.Length > maximumColumns)
maximumColumns = row.Length;
}
var maximumCellWidths = new int[maximumColumns];
for (int i = 0; i < maximumCellWidths.Count(); i++)
maximumCellWidths[i] = 0;
var paddingCount = 0;
if (Padding > 0)
{
//Padding is left and right
paddingCount = Padding * 2;
}
foreach (var row in table)
{
for (int i = 0; i < row.Length; i++)
{
var maxWidth = row[i].Length + paddingCount;
if (maxWidth > maximumCellWidths[i])
maximumCellWidths[i] = maxWidth;
}
}
return maximumCellWidths;
}
private StringBuilder CreateTopLine(int[] maximumCellWidths, int rowColumnCount, StringBuilder formattedTable)
{
for (int i = 0; i < rowColumnCount; i++)
{
if (i == 0 && i == rowColumnCount - 1)
formattedTable.AppendLine(string.Format("{0}{1}{2}", TopLeftJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine), TopRightJoint));
else if (i == 0)
formattedTable.Append(string.Format("{0}{1}", TopLeftJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine)));
else if (i == rowColumnCount - 1)
formattedTable.AppendLine(string.Format("{0}{1}{2}", TopJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine), TopRightJoint));
else
formattedTable.Append(string.Format("{0}{1}", TopJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine)));
}
return formattedTable;
}
private StringBuilder CreateBottomLine(int[] maximumCellWidths, int rowColumnCount, StringBuilder formattedTable)
{
for (int i = 0; i < rowColumnCount; i++)
{
if (i == 0 && i == rowColumnCount - 1)
formattedTable.AppendLine(string.Format("{0}{1}{2}", BottomLeftJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine), BottomRightJoint));
else if (i == 0)
formattedTable.Append(string.Format("{0}{1}", BottomLeftJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine)));
else if (i == rowColumnCount - 1)
formattedTable.AppendLine(string.Format("{0}{1}{2}", BottomJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine), BottomRightJoint));
else
formattedTable.Append(string.Format("{0}{1}", BottomJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine)));
}
return formattedTable;
}
private StringBuilder CreateValueLine(int[] maximumCellWidths, string[] row, bool alignRight, StringBuilder formattedTable)
{
int cellIndex = 0;
int lastCellIndex = row.Length - 1;
var paddingString = string.Empty;
if (Padding > 0)
paddingString = string.Concat(Enumerable.Repeat(' ', Padding));
foreach (var column in row)
{
var restWidth = maximumCellWidths[cellIndex];
if (Padding > 0)
restWidth -= Padding * 2;
var cellValue = alignRight ? column.PadLeft(restWidth, ' ') : column.PadRight(restWidth, ' ');
if (cellIndex == 0 && cellIndex == lastCellIndex)
formattedTable.AppendLine(string.Format("{0}{1}{2}{3}{4}", VerticalLine, paddingString, cellValue, paddingString, VerticalLine));
else if (cellIndex == 0)
formattedTable.Append(string.Format("{0}{1}{2}{3}", VerticalLine, paddingString, cellValue, paddingString));
else if (cellIndex == lastCellIndex)
formattedTable.AppendLine(string.Format("{0}{1}{2}{3}{4}", VerticalLine, paddingString, cellValue, paddingString, VerticalLine));
else
formattedTable.Append(string.Format("{0}{1}{2}{3}", VerticalLine, paddingString, cellValue, paddingString));
cellIndex++;
}
return formattedTable;
}
private StringBuilder CreateSeperatorLine(int[] maximumCellWidths, int previousRowColumnCount, int rowColumnCount, StringBuilder formattedTable)
{
var maximumCells = Math.Max(previousRowColumnCount, rowColumnCount);
for (int i = 0; i < maximumCells; i++)
{
if (i == 0 && i == maximumCells - 1)
{
formattedTable.AppendLine(string.Format("{0}{1}{2}", LeftJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine), RightJoint));
}
else if (i == 0)
{
formattedTable.Append(string.Format("{0}{1}", LeftJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine)));
}
else if (i == maximumCells - 1)
{
if (i > previousRowColumnCount)
formattedTable.AppendLine(string.Format("{0}{1}{2}", TopJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine), TopRightJoint));
else if (i > rowColumnCount)
formattedTable.AppendLine(string.Format("{0}{1}{2}", BottomJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine), BottomRightJoint));
else if (i > previousRowColumnCount - 1)
formattedTable.AppendLine(string.Format("{0}{1}{2}", MiddleJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine), TopRightJoint));
else if (i > rowColumnCount - 1)
formattedTable.AppendLine(string.Format("{0}{1}{2}", MiddleJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine), BottomRightJoint));
else
formattedTable.AppendLine(string.Format("{0}{1}{2}", MiddleJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine), RightJoint));
}
else
{
if (i > previousRowColumnCount)
formattedTable.Append(string.Format("{0}{1}", TopJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine)));
else if (i > rowColumnCount)
formattedTable.Append(string.Format("{0}{1}", BottomJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine)));
else
formattedTable.Append(string.Format("{0}{1}", MiddleJoint, string.Empty.PadLeft(maximumCellWidths[i], HorizontalLine)));
}
}
return formattedTable;
}
public override string ToString()
{
var table = new List<string[]>();
var firstRowIsHeader = false;
if (_headers?.Any() == true)
{
table.Add(_headers);
firstRowIsHeader = true;
}
if (_rows?.Any() == true)
table.AddRange(_rows);
if (!table.Any())
return string.Empty;
var formattedTable = new StringBuilder();
var previousRow = table.FirstOrDefault();
var nextRow = table.FirstOrDefault();
int[] maximumCellWidths = GetMaxCellWidths(table);
formattedTable = CreateTopLine(maximumCellWidths, nextRow.Count(), formattedTable);
int rowIndex = 0;
int lastRowIndex = table.Count - 1;
for (int i = 0; i < table.Count; i++)
{
var row = table[i];
var align = RowTextAlignRight;
if (i == 0 && firstRowIsHeader)
align = HeaderTextAlignRight;
formattedTable = CreateValueLine(maximumCellWidths, row, align, formattedTable);
previousRow = row;
if (rowIndex != lastRowIndex)
{
nextRow = table[rowIndex + 1];
formattedTable = CreateSeperatorLine(maximumCellWidths, previousRow.Count(), nextRow.Count(), formattedTable);
}
rowIndex++;
}
formattedTable = CreateBottomLine(maximumCellWidths, previousRow.Count(), formattedTable);
return formattedTable.ToString();
}
}
}