Repository navigation
Expand file tree
/
Copy pathTextHeader.cs
More file actions
170 lines (153 loc) · 5.46 KB
/
Copy pathTextHeader.cs
File metadata and controls
170 lines (153 loc) · 5.46 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSModuleDevelopment.Utility
{
/// <summary>
/// Object containing information
/// </summary>
public class TextHeader
{
/// <summary>
/// The text of the header
/// </summary>
public string Text { get; set; }
/// <summary>
/// The number of lines the header text has
/// </summary>
public int LinesOfText
{
get
{
return Text.Split('\n').Length;
}
}
/// <summary>
/// The maximum width any line of text has.
/// </summary>
public int WidthOfText
{
get
{
int result = 0;
foreach (string line in Text.Split('\n'))
if (line.Length > result)
result = line.Length;
return result;
}
}
/// <summary>
/// The symbol of the left top corner
/// </summary>
public string CornerLT = "#";
/// <summary>
/// The symbol of the right top corner
/// </summary>
public string CornerRT = "#";
/// <summary>
/// The symbol of the left bottom corner
/// </summary>
public string CornerLB = "#";
/// <summary>
/// The symbol of the right bottom corner
/// </summary>
public string CornerRB = "#";
/// <summary>
/// The symbol of the right border
/// </summary>
public string BorderRight = "#";
/// <summary>
/// The symbol of the left border
/// </summary>
public string BorderLeft = "#";
/// <summary>
/// The symbol of the top border
/// </summary>
public string BorderTop = "-";
/// <summary>
/// The symbol of the bottom border
/// </summary>
public string BorderBottom = "-";
/// <summary>
/// Whether the header should be as wide as the window running the code
/// </summary>
public bool MaxWidth = true;
/// <summary>
/// How wide should the header be, if it isn't set to max. Anything shorter than the text width will be ignored
/// </summary>
public int Width = -1;
/// <summary>
/// How the text should be aligned within the header
/// </summary>
public TextAlignment TextAlignment = TextAlignment.Center;
/// <summary>
/// Whether the text should be padded on the side it is aligned to
/// </summary>
public int Padding;
/// <summary>
/// Creates a new header around the specified string
/// </summary>
/// <param name="Text">The string to put in a header</param>
public TextHeader(string Text)
{
this.Text = Text;
}
/// <summary>
/// Converts the header into its string representation
/// </summary>
/// <returns>The header text to use whereever said header is needed.</returns>
public override string ToString()
{
StringBuilder result = new StringBuilder();
// Process Width
int effectivePadding = 0;
if (TextAlignment != TextAlignment.Center)
effectivePadding = Padding;
int width = Width;
if (MaxWidth)
width = UtilityHost.RawUI.WindowSize.Width;
if ((WidthOfText + effectivePadding + 2 + BorderLeft.Length + BorderRight.Length) > width)
width = WidthOfText + effectivePadding + 2 + BorderLeft.Length + BorderRight.Length;
int effectiveContentWidth = width - 2 - BorderLeft.Length - BorderRight.Length;
// Process Top Line
result = result.Append(CornerLT);
for (int n = 0; n < (effectiveContentWidth + 2); n++)
result = result.Append(BorderTop);
result = result.Append(CornerRT + "\n");
// Process Content
foreach (string line in Text.Split('\n'))
{
result = result.Append(BorderLeft + " ");
int spaceLeft = 0;
int spaceRight = 0;
if (TextAlignment == TextAlignment.Left)
{
spaceLeft += Padding;
spaceRight = effectiveContentWidth - line.Length - spaceLeft;
}
else if (TextAlignment == TextAlignment.Right)
{
spaceRight += Padding;
spaceLeft = effectiveContentWidth - line.Length - spaceRight;
}
else
{
spaceLeft = (effectiveContentWidth - line.Length) / 2;
spaceRight = effectiveContentWidth - line.Length - spaceLeft;
}
result = result.Append(' ', spaceLeft);
result = result.Append(line);
result = result.Append(' ', spaceRight);
result = result.Append(" " + BorderRight + "\n");
}
// Process Bottom Line
result = result.Append(CornerLB);
for (int n = 0; n < (effectiveContentWidth + 2); n++)
result = result.Append(BorderBottom);
result = result.Append(CornerRB);
return result.ToString();
}
}
}