-
Notifications
You must be signed in to change notification settings - Fork 748
/
Copy pathTable.cs
179 lines (147 loc) · 4.77 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace TechTalk.SpecFlow
{
public class Table
{
private readonly string[] header;
private readonly TableRows rows = new TableRows();
public IEnumerable<string> Header
{
get { return header; }
}
public TableRows Rows
{
get { return rows; }
}
public int RowCount
{
get { return rows.Count; }
}
public Table(params string[] header)
{
if (header == null || header.Length == 0)
throw new ArgumentException("invalid header count", "header");
for (int colIndex = 0; colIndex < header.Length; colIndex++)
header[colIndex] = header[colIndex] ?? string.Empty;
this.header = header;
}
internal int GetHeaderIndex(string column)
{
int index = Array.IndexOf(header, column);
if (index < 0)
throw new Exception("could not find column");
return index;
}
public void AddRow(params string[] cells)
{
if (cells == null || cells.Length != header.Length)
throw new Exception("invalid cell count");
var row = new TableRow(this, cells);
rows.Add(row);
}
public override string ToString()
{
int[] columnWidths = new int[header.Length];
for (int colIndex = 0; colIndex < header.Length; colIndex++)
columnWidths[colIndex] = header[colIndex].Length;
foreach (TableRow row in rows)
{
for (int colIndex = 0; colIndex < header.Length; colIndex++)
columnWidths[colIndex] = Math.Max(columnWidths[colIndex], row[colIndex].Length);
}
StringBuilder builder = new StringBuilder();
AddTableRow(builder, header, columnWidths);
foreach (TableRow row in rows)
AddTableRow(builder, row.Select(pair => pair.Value), columnWidths);
return builder.ToString();
}
private void AddTableRow(StringBuilder builder, IEnumerable<string> cells, int[] widths)
{
const string margin = " ";
const string separator = "|";
int colIndex = 0;
builder.Append(separator);
foreach (string cell in cells)
{
builder.Append(margin);
builder.Append(cell);
builder.Append(' ', widths[colIndex] - cell.Length);
builder.Append(margin);
builder.Append(separator);
colIndex++;
}
builder.AppendLine();
}
}
public class TableRows : IEnumerable<TableRow>
{
private readonly List<TableRow> innerList = new List<TableRow>();
public int Count { get { return innerList.Count; } }
public TableRow this[int index]
{
get { return innerList[index]; }
}
public IEnumerator<TableRow> GetEnumerator()
{
return innerList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
internal void Add(TableRow row)
{
innerList.Add(row);
}
}
public class TableRow : IEnumerable<KeyValuePair<string, string>>
{
private readonly Table table;
private readonly string[] items;
internal TableRow(Table table, string[] items)
{
for (int colIndex = 0; colIndex < items.Length; colIndex++)
items[colIndex] = items[colIndex] ?? string.Empty;
this.table = table;
this.items = items;
}
public string this[string header]
{
get
{
int itemIndex = table.GetHeaderIndex(header);
return items[itemIndex];
}
}
public string this[int index]
{
get
{
return items[index];
}
}
public int Count
{
get { return items.Length; }
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
Debug.Assert(items.Length == table.Header.Count());
int itemIndex = 0;
foreach (string header in table.Header)
{
yield return new KeyValuePair<string, string>(header, items[itemIndex]);
itemIndex++;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}