-
Notifications
You must be signed in to change notification settings - Fork 3
/
OutputTools.cs
226 lines (186 loc) · 9.08 KB
/
OutputTools.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
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
using System;
using System.Text;
namespace SAD806x
{
public enum OutputCellAlignment
{
Left = 0,
Right = 1
}
public static class OutputTools
{
public static int BorderedWidth = 120;
public static int BorderedLeftMargin = 20;
public static int BorderedRightMargin = 20;
public static int BorderedLeftMarginReduced = 4;
public static int BorderedRightMarginReduced = 4;
public static char BorderTopBottom = '═';
public static char BorderLeftRight = '║';
public static char BorderTopLeftCorner = '╔';
public static char BorderTopRightCorner = '╗';
public static char BorderBottomLeftCorner = '╚';
public static char BorderBottomRightCorner = '╝';
public static string BorderedHeader()
{
return BorderTopLeftCorner + new string(BorderTopBottom, BorderedWidth - 2) + BorderTopRightCorner;
}
public static string BorderedHeader(int oversizedTextLength, bool reducedMargin)
{
int bdLeftMargin = BorderedLeftMargin;
int bdRightMargin = BorderedRightMargin;
if (reducedMargin)
{
bdLeftMargin = BorderedLeftMarginReduced;
bdRightMargin = BorderedRightMarginReduced;
}
int oversizedBorderWidth = oversizedTextLength + 2 + bdLeftMargin + bdRightMargin;
return BorderTopLeftCorner + new string(BorderTopBottom, oversizedBorderWidth - 2) + BorderTopRightCorner;
}
public static string BorderedFooter()
{
return BorderBottomLeftCorner + new string(BorderTopBottom, BorderedWidth - 2) + BorderBottomRightCorner;
}
public static string BorderedFooter(int oversizedTextLength, bool reducedMargin)
{
int bdLeftMargin = BorderedLeftMargin;
int bdRightMargin = BorderedRightMargin;
if (reducedMargin)
{
bdLeftMargin = BorderedLeftMarginReduced;
bdRightMargin = BorderedRightMarginReduced;
}
int oversizedBorderWidth = oversizedTextLength + 2 + bdLeftMargin + bdRightMargin;
return BorderBottomLeftCorner + new string(BorderTopBottom, oversizedBorderWidth - 2) + BorderBottomRightCorner;
}
public static string BorderedEmpty()
{
return BorderLeftRight + new string(' ', BorderedWidth - 2) + BorderLeftRight;
}
public static string BorderedEmpty(int oversizedTextLength, bool reducedMargin)
{
int bdLeftMargin = BorderedLeftMargin;
int bdRightMargin = BorderedRightMargin;
if (reducedMargin)
{
bdLeftMargin = BorderedLeftMarginReduced;
bdRightMargin = BorderedRightMarginReduced;
}
int oversizedBorderWidth = oversizedTextLength + 2 + bdLeftMargin + bdRightMargin;
return BorderLeftRight + new string(' ', oversizedBorderWidth - 2) + BorderLeftRight;
}
public static string BorderedTitle(string text)
{
if (text.Length > BorderedWidth - 2) text = text.Substring(0, BorderedWidth - 2);
int leftMargin = (BorderedWidth - text.Length) / 2;
int rightMargin = BorderedWidth - leftMargin - text.Length;
string format = "{0,1}{1,1}{2,1}{3,1}{4,1}";
return string.Format(format, BorderLeftRight, new string(' ', leftMargin - 1), text, new string(' ', rightMargin - 1), BorderLeftRight);
}
public static string BorderedText(string text)
{
if (text.Length > BorderedWidth - 2 - BorderedLeftMargin - BorderedRightMargin) text = text.Substring(0, BorderedWidth - 2 - BorderedLeftMargin - BorderedRightMargin);
int leftMargin = BorderedLeftMargin;
int rightMargin = BorderedWidth - leftMargin - text.Length;
string format = "{0,1}{1,1}{2,1}{3,1}{4,1}";
return string.Format(format, BorderLeftRight, new string(' ', leftMargin - 1), text, new string(' ', rightMargin - 1), BorderLeftRight);
}
public static string BorderedText(string text, int oversizedTextLength, bool reducedMargin)
{
int bdLeftMargin = BorderedLeftMargin;
int bdRightMargin = BorderedRightMargin;
if (reducedMargin)
{
bdLeftMargin = BorderedLeftMarginReduced;
bdRightMargin = BorderedRightMarginReduced;
}
int oversizedBorderWidth = oversizedTextLength + 2 + bdLeftMargin + bdRightMargin;
int leftMargin = bdLeftMargin;
int rightMargin = oversizedBorderWidth - leftMargin - oversizedTextLength;
string format = "{0,1}{1,1}{2,-" + oversizedTextLength.ToString() + "}{3,1}{4,1}";
return string.Format(format, BorderLeftRight, new string(' ', leftMargin - 1), text, new string(' ', rightMargin - 1), BorderLeftRight);
}
public static string BorderedColumns(string[] arrText)
{
if (arrText == null) return BorderedEmpty();
if (arrText.Length == 0) return BorderedEmpty();
if (arrText.Length == 1) return BorderedText(arrText[0]);
int totalSpace = BorderedWidth - BorderedLeftMargin - BorderedRightMargin - 2;
int totalTextSpace = 0;
foreach (string text in arrText) totalTextSpace += text.Length;
int columnSize = totalSpace / arrText.Length;
string lastColMargin = string.Empty;
if (totalSpace % arrText.Length > 0) new string(' ', totalSpace % arrText.Length);
string[] arrMarginedText = new string[arrText.Length];
for (int iPos = 0; iPos < arrMarginedText.Length; iPos++)
{
string text = arrText[iPos];
if (text.Length > columnSize - 1) text = text.Substring(0, columnSize - 1);
string spaces = new string(' ', columnSize - text.Length);
if (iPos % 2 == 0) arrMarginedText[iPos] = text + spaces;
else arrMarginedText[iPos] = spaces + text;
if (iPos == arrMarginedText.Length - 1) arrMarginedText[iPos] += lastColMargin;
}
return BorderedText(string.Join(string.Empty, arrMarginedText));
}
public static string GetSpacesCenteredString(string sValue, int iWidth)
{
if (iWidth <= 0) return string.Empty;
if (sValue == null || sValue == string.Empty) return new string(' ', iWidth);
if (iWidth <= sValue.Length + 1) return sValue;
int iLeft = (int)(iWidth / 2 + sValue.Length / 2);
return string.Format("{0," + iLeft.ToString() + "}{1," + (iWidth - iLeft).ToString() + "}", sValue, string.Empty);
}
public static string GetCellsOutput(string[] cellsValues, int[] cellsMinSizes, OutputCellAlignment[] cellsAlignments)
{
string cellsFormat = string.Empty;
if (cellsValues == null) return string.Empty;
int cellsNumber = cellsValues.Length;
if (cellsMinSizes == null)
{
cellsMinSizes = new int[cellsNumber];
for (int iCell = 0; iCell < cellsNumber; iCell++)
{
cellsMinSizes[iCell] = cellsValues[iCell].Length + 1;
}
}
if (cellsMinSizes.Length != cellsNumber) return string.Empty;
if (cellsAlignments == null)
{
cellsAlignments = new OutputCellAlignment[cellsNumber];
for (int iCell = 0; iCell < cellsNumber; iCell++) cellsAlignments[iCell] = OutputCellAlignment.Left;
}
if (cellsAlignments.Length != cellsNumber) return string.Empty;
// Padding Mngt
int iPadding = 0;
for (int iCell = 0; iCell < cellsNumber; iCell++)
{
// Security
if (cellsMinSizes[iCell] < 1) cellsMinSizes[iCell] = 1;
if (iPadding > 0)
{
cellsMinSizes[iCell] -= iPadding;
iPadding = 0;
if (cellsMinSizes[iCell] < 1)
{
iPadding = 1 - cellsMinSizes[iCell];
cellsMinSizes[iCell] = 1;
}
}
if (cellsValues[iCell].Length > cellsMinSizes[iCell]) iPadding += cellsValues[iCell].Length - cellsMinSizes[iCell];
}
for (int iCell = 0; iCell < cellsNumber; iCell++)
{
string sFormat = "{" + iCell.ToString() + ",";
switch (cellsAlignments[iCell])
{
case OutputCellAlignment.Left:
sFormat += "-";
break;
}
sFormat += cellsMinSizes[iCell].ToString() + "}";
cellsFormat += sFormat;
}
return string.Format(cellsFormat, cellsValues);
}
}
}