-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConsoleTable.cs
158 lines (133 loc) · 4.42 KB
/
ConsoleTable.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
/* License
* ------------------------------------------------------------------------------
* Copyright (c) 2019 UX Digital Systems Ltd
*
* Permission is hereby granted, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software
* for the continued use and development of the system on which it was installed,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* Any persons obtaining the software have no rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software without
* written persmission from UX Digital Systems Ltd, if it is not for use on the
* system on which it was originally installed.
* ------------------------------------------------------------------------------
* UX.Digital
* ----------
* http://ux.digital
*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UX.Lib2
{
public class ConsoleTable
{
#region Fields
private readonly List<string> _headers;
private readonly int[] _columnWidths;
private readonly List<IEnumerable<string>> _rows = new List<IEnumerable<string>>();
#endregion
#region Constructors
public ConsoleTable(params string[] headers)
{
_headers = new List<string>(headers);
_columnWidths = new int[headers.Count()];
var col = 0;
foreach (var header in _headers)
{
_columnWidths[col] = header.Length;
col++;
}
}
#endregion
#region Finalizers
#endregion
#region Events
#endregion
#region Delegates
#endregion
#region Properties
public int TotalWidth
{
get
{
return _columnWidths.Aggregate(1, (current, width) => current + (width + 3));
}
}
#endregion
#region Methods
public void AddRow(params object[] items)
{
var values = new List<string>();
var col = 0;
foreach (var item in items)
{
if (item == null)
{
values.Add(string.Empty);
col++;
continue;
}
var s = item.ToString();
values.Add(s);
if (s.Length > _columnWidths[col])
_columnWidths[col] = s.Length;
col++;
}
_rows.Add(values);
}
public override string ToString()
{
return ToString(false);
}
public string ToString(bool useColor)
{
var sb = new StringBuilder();
var colTag1 = useColor ? Debug.AnsiYellow : string.Empty;
var colTag2 = useColor ? Debug.AnsiGreen : string.Empty;
var colTagClose = useColor ? Debug.AnsiReset : string.Empty;
sb.Append('|');
var divLine = "|";
var col = 0;
foreach (var header in _headers)
{
var s = colTag1 + header.PadRight(_columnWidths[col]) + colTagClose;
sb.Append(" " + s + " |");
var dashes = string.Empty;
for (var i = 0; i < _columnWidths[col]; i++)
{
dashes = dashes + "-";
}
divLine = divLine + " " + dashes + " |";
col++;
}
sb.AppendLine();
sb.AppendLine(divLine);
foreach (var row in _rows)
{
col = 0;
var items = row as string[] ?? row.ToArray();
sb.Append('|');
foreach (var item in items)
{
var s = item.PadRight(_columnWidths[col]);
if (col == 0)
{
s = colTag2 + s + colTagClose;
}
sb.Append(" " + s + " |");
col++;
}
sb.AppendLine();
}
return sb.ToString();
}
#endregion
}
}