forked from dotnet/crank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResultTable.cs
184 lines (148 loc) · 5.51 KB
/
ResultTable.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Microsoft.Crank.Controller
{
// Renders a markdown table
public class ResultTable
{
public ResultTable(int columns)
{
Columns = columns;
}
public int Columns { get; }
public List<string> Headers { get; } = new List<string>();
public List<List<Cell>> Rows { get; } = new List<List<Cell>>();
public List<Cell> AddRow()
{
var row = new List<Cell>();
Rows.Add(row);
return row;
}
/// <summary>
/// Returns the most suited column widths for this table
/// </summary>
/// <returns></returns>
public int[] CalculateColumnWidths()
{
var results = new List<int>();
for (var i = 0; i < Columns; i++)
{
var width = 0;
foreach(var row in Rows)
{
// The width is the sum of each element's width, plus 1 for each separator between elements
width = Math.Max(width, row[i].Elements.Select(x => x.Text.Length).Sum() + row[i].Elements.Count() - 1);
}
width = Math.Max(width, Math.Max(Headers[i].Length, 1));
results.Add(width);
}
return results.ToArray();
}
public void RemoveEmptyRows(int startIndex = 0)
{
Rows.RemoveAll(row => row.Skip(startIndex).All(cell => cell.Elements.All(x => String.IsNullOrEmpty(x.Text))));
}
public void Render(TextWriter writer)
{
Render(writer, CalculateColumnWidths());
}
public void Render(TextWriter writer, int[] columnWidths)
{
/*
| Tables | Are | Cool |
| ------------- | ------------- | ----- |
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
*/
// | Tables | Are | Cool |
for (var i = 0; i < Columns; i++)
{
writer.Write($"| {Headers[i].PadRight(columnWidths[i])} ");
}
writer.WriteLine("|");
// | ------------- | ------------- | ----- |
for (var i = 0; i < Columns; i++)
{
writer.Write($"| {new String('-', columnWidths[i])} ");
}
writer.WriteLine("|");
// | col 3 is | right-aligned | $1600 |
foreach (var row in Rows)
{
for (var i = 0; i < Columns; i++)
{
writer.Write($"| ");
if (row[i].Elements.Any())
{
foreach (var element in row[i].Elements.Where(x => x.Alignment == CellTextAlignment.Left))
{
writer.Write(element.Text);
writer.Write(" ");
}
foreach (var element in row[i].Elements.Where(x => x.Alignment == CellTextAlignment.Unspecified))
{
writer.Write(element.Text);
writer.Write(" ");
}
// Add spaces to fill between Left and Right aligned elements
var leftElements = row[i].Elements.Where(x => x.Alignment != CellTextAlignment.Right);
var rightElements = row[i].Elements.Where(x => x.Alignment == CellTextAlignment.Right);
var leftWitdh = leftElements.Any() ? leftElements.Sum(x => x.Text.Length + 1) - 1 : 0;
var rightWidth = rightElements.Any() ? rightElements.Sum(x => x.Text.Length + 1) - 1 : 0;
writer.Write(new String(' ', columnWidths[i] - rightWidth - leftWitdh));
foreach (var element in row[i].Elements.Where(x => x.Alignment == CellTextAlignment.Right))
{
writer.Write(element.Text);
writer.Write(" ");
}
}
else
{
writer.Write(new String(' ', columnWidths[i] + 1));
}
}
writer.WriteLine($"|");
}
}
}
public class Cell
{
public Cell(params CellElement[] elements)
{
if (elements != null)
{
Elements.AddRange(elements);
}
}
public List<CellElement> Elements { get; } = new List<CellElement>();
}
public class CellElement
{
public CellElement()
{
}
public CellElement(string text)
{
Text = text;
}
public CellElement(string text, CellTextAlignment alignment)
{
Text = text;
Alignment = alignment;
}
public string Text { get; set; }
public CellTextAlignment Alignment { get; set; }
}
public enum CellTextAlignment
{
Unspecified,
Left,
Right
}
}