-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathTabularStringFormat.cs
101 lines (86 loc) · 3.28 KB
/
TabularStringFormat.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
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class TabularStringFormat
{
public const char DefaultVerticalLineChar = '|';
public const char DefaultHorizontalLineChar = '_';
private readonly IList<Column> columns;
private readonly char horizontalLineChar;
private readonly char verticalLineChar;
private readonly string tableTitle;
public TabularStringFormat(IList<Column> columns, char horizontalLineChar = DefaultHorizontalLineChar, char verticalLineChar = DefaultVerticalLineChar, string tableTitle = null)
{
this.columns = columns;
this.horizontalLineChar = horizontalLineChar;
this.verticalLineChar = verticalLineChar;
this.tableTitle = tableTitle;
}
public string GenerateString(IEnumerable<IList<object>> rows)
{
var sb = new StringBuilder();
if (!string.IsNullOrWhiteSpace(this.tableTitle))
{
this.PrintTitleSection(sb);
}
else
{
this.WriteFlatLine(sb, false);
}
sb.Append(this.verticalLineChar);
foreach (var column in this.columns)
{
sb.Append(column.Header.PadRight(column.Width));
sb.Append(this.verticalLineChar);
}
this.WriteFlatLine(sb);
foreach (var row in rows)
{
sb.Append(this.verticalLineChar);
if (row.Count != this.columns.Count)
{
throw new InvalidOperationException("All rows must have length equal to the number of columns present.");
}
for (var i = 0; i < this.columns.Count; i++)
{
var dataString = this.columns[i].Format != null ? string.Format(this.columns[i].Format, row[i]) : row[i].ToString();
sb.Append(dataString.PadRight(this.columns[i].Width));
sb.Append(this.verticalLineChar);
}
this.WriteFlatLine(sb);
}
return sb.ToString();
}
private void PrintTitleSection(StringBuilder sb)
{
this.WriteFlatLine(sb, false);
var tableWidth = this.columns.Sum(column => column.Width);
sb.Append(this.verticalLineChar);
sb.Append(this.tableTitle.PadRight(tableWidth + this.columns.Count - 1));
sb.Append(this.verticalLineChar);
sb.AppendLine();
sb.Append(this.verticalLineChar);
for (var i = 0; i < this.columns.Count - 1; i++)
{
sb.Append(string.Empty.PadRight(this.columns[i].Width, this.horizontalLineChar));
sb.Append(this.horizontalLineChar);
}
sb.Append(string.Empty.PadRight(this.columns[this.columns.Count - 1].Width, this.horizontalLineChar));
sb.Append(this.verticalLineChar);
sb.AppendLine();
}
private void WriteFlatLine(StringBuilder sb, bool withPipes = true)
{
var splitCharacter = withPipes ? this.verticalLineChar : this.horizontalLineChar;
sb.AppendLine();
sb.Append(splitCharacter);
for (var i = 0; i < this.columns.Count; i++)
{
sb.Append(string.Empty.PadRight(this.columns[i].Width, this.horizontalLineChar));
sb.Append(splitCharacter);
}
sb.AppendLine();
}
}