-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathProgram.cs
More file actions
293 lines (226 loc) · 8.19 KB
/
Program.cs
File metadata and controls
293 lines (226 loc) · 8.19 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
namespace ConsoleTable.Text.Examples;
class Program
{
static void Main(string[] args)
{
WriteDefaultTable();
WriteDefaultTableWithProperties();
WriteTableWithStyling(true, true, true, 10);
WriteTableWithStyling(false, true, true, 10);
WriteTableWithStyling(true, false, true, 10);
WriteTableWithStyling(true, true, false, 10);
WriteTableWithStyling(false, false, false, 10);
WriteTableOnlyHeaders();
WriteTableOnlyRows();
WriteTableOnlyFooters();
WriteTableMoreHeaders();
WriteTableLessHeaders();
WriteTableEachRowRandom();
WriteTableFluent();
WriteBigTable();
Console.Read();
}
private static void WriteDefaultTable()
{
Console.WriteLine();
Console.WriteLine("Default table:");
// Setup the table
var table = new Table();
// Set headers
table.SetHeaders("Name", "Age", "City");
// Add rows
table.AddRow("Alice Cooper", "30", "New York");
table.AddRows(new string[][]
{
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
});
// Set footers
table.SetFooters("Total: 3", "Total Age: 102");
// Display the table
Console.WriteLine(table.ToTable());
Console.WriteLine();
}
private static void WriteDefaultTableWithProperties()
{
Console.WriteLine();
Console.WriteLine("Default table with properties instead of methods:");
var table = new Table
{
CachingEnabled = true,
Headers = new string[] { "Name", "Age", "City" },
Rows = new List<string[]>
{
new string[] { "Alice Cooper", "30", "New York" },
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
}
};
Console.WriteLine(table.ToTable());
Console.WriteLine();
}
private static void WriteTableOnlyRows()
{
Console.WriteLine();
Console.WriteLine("Table only rows:");
var table = new Table();
for (int i = 1; i <= 5; i++)
{
table.AddRow($"name {i}", (i * 15).ToString());
}
Console.WriteLine(table.ToString());
Console.WriteLine();
}
private static void WriteTableOnlyHeaders()
{
Console.WriteLine();
Console.WriteLine("Table only headers:");
var table = new Table();
table.SetHeaders("Name", "Age", "City");
Console.WriteLine(table.ToString());
Console.WriteLine();
}
private static void WriteTableOnlyFooters()
{
Console.WriteLine();
Console.WriteLine("Table only footers:");
var table = new Table();
table.SetFooters("Total: 3", "Total Age: 102");
Console.WriteLine(table.ToString());
Console.WriteLine();
}
private static void WriteTableMoreHeaders()
{
Console.WriteLine();
Console.WriteLine("Table with more headers:");
var table = new Table();
table.SetHeaders("Name", "Age", "City", "Country");
table.AddRows(
new string[] { "Alice Cooper", "30" },
new string[] { "Bob", "25" },
new string[] { "Charlie Brown", "47" }
);
Console.WriteLine(table.ToString());
Console.WriteLine();
}
private static void WriteTableLessHeaders()
{
Console.WriteLine();
Console.WriteLine("Table with less headers:");
var table = new Table();
table.SetHeaders("Name");
table.AddRows(
new string[] { "Alice Cooper", "30", "New York" },
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
);
Console.WriteLine(table.ToString());
Console.WriteLine();
}
private static void WriteTableEachRowRandom()
{
Console.WriteLine();
Console.WriteLine("Table with random row column count:");
var table = new Table();
table.SetHeaders("Name", "Age", "City");
table.AddRow("Alice");
table.AddRow("Bob", "25", "Antwerp", "Belgium");
table.AddRow("Charlie", "47", "Chicago");
table.AddRow("Karina", "33", "Lima", "Peru", "South-America");
table.AddRow("Jenny", "43");
table.AddRow("John");
table.AddRow("Johny");
table.AddRow();
table.AddRow(null!);
table.AddRow("Thomas", "33", "Brussels", "Belgium", "Europe", "Earth", "Solar System");
table.AddRow("Nathalie", "29", "Paris", "France", "Europe", "Earth", "Solar System");
table.AddRow("Mathias", "37", "Oslo", "Norway", "Europe", "Earth", "Solar System");
table.AddRow("Kenny", "55", "Tokyo");
table.SetFooters("Footer 1", "Footer 2");
Console.WriteLine(table.ToString());
Console.WriteLine();
}
private static void WriteTableWithStyling(bool headerTextAlignRight, bool rowTextAlignRight, bool footerTextAlignRight, int padding)
{
Console.WriteLine();
Console.WriteLine($"Table with following styling:");
Console.WriteLine($"Header text alignment: {(headerTextAlignRight ? "right" : "left")}");
Console.WriteLine($"Row text alignment: {(rowTextAlignRight ? "right" : "left")}");
Console.WriteLine($"Footer text alignment: {(footerTextAlignRight ? "right" : "left")}");
Console.WriteLine($"Padding: {padding}");
var table = new Table
{
CachingEnabled = true,
Padding = padding,
HeaderTextAlignmentRight = headerTextAlignRight,
RowTextAlignmentRight = rowTextAlignRight,
FooterTextAlignmentRight = footerTextAlignRight
};
table.SetHeaders("Name", "Age", "City");
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
table.AddRow($"Name {i}", (i * 8).ToString(), $"City {i}");
else
table.AddRow($"Very Long Name {i}", (i * 8).ToString(), $"City {i}");
}
table.SetFooters("Footer 1", "Footer 2", "Footer 3");
Console.WriteLine(table.ToString());
Console.WriteLine();
}
private static void WriteTableFluent()
{
Console.WriteLine();
Console.WriteLine("Table fluent:");
var tableString = new Table()
.SetHeaders("Name", "Age", "City")
.AddRow("Alice Cooper", "30", "New York")
.AddRows(
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
)
.SetFooters("Total: 3", "Total Age: 102")
.ToTable();
Console.WriteLine(tableString);
Console.WriteLine();
}
private static void WriteBigTable()
{
Console.WriteLine();
Console.WriteLine("Big table (may take some seconds to generate):");
var table = new Table
{
CachingEnabled = true,
HeaderTextAlignmentRight = false,
RowTextAlignmentRight = false,
Padding = 2
};
var columnCount = 5;
var headers = new List<string>();
for (var columnPos = 1; columnPos <= columnCount; columnPos++)
{
headers.Add($"Header {columnPos}");
}
table.Headers = headers.ToArray();
var rows = new List<string[]>();
for (var rowPos = 1; rowPos <= 100000; rowPos++)
{
var row = new string[columnCount];
for (var columnPos = 1; columnPos <= columnCount; columnPos++)
{
row[columnPos - 1] = $"Row {rowPos} -> Column {columnPos}";
}
rows.Add(row);
}
table.Rows = rows;
var footers = new List<string>();
for (var columnPos = 1; columnPos <= columnCount; columnPos++)
{
footers.Add($"Footer {columnPos}");
}
table.Footers = footers.ToArray();
var tableString = table.ToTable();
Console.WriteLine(tableString);
Console.WriteLine();
}
}