-
Notifications
You must be signed in to change notification settings - Fork 3
/
PSTreeExtensions.cs
56 lines (44 loc) · 1.53 KB
/
PSTreeExtensions.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
using System.Text;
using System.Text.RegularExpressions;
namespace PSTree;
internal static class PSTreeExtensions
{
private static readonly Regex s_re = new(@"└|\S", RegexOptions.Compiled);
private static readonly StringBuilder s_sb = new();
internal static string Indent(this string inputString, int indentation)
{
s_sb.Clear();
return s_sb.Append(' ', (4 * indentation) - 4)
.Append("└── ")
.Append(inputString)
.ToString();
}
internal static PSTreeFileSystemInfo[] ConvertToTree(
this PSTreeFileSystemInfo[] inputObject)
{
// Well, I don't know what was I thinking when I wrote this, but it works :)
for (int i = 0; i < inputObject.Length; i++)
{
int index = inputObject[i].Hierarchy.IndexOf('└');
if (index < 0)
{
continue;
}
int z = i - 1;
while (!s_re.IsMatch(inputObject[z].Hierarchy[index].ToString()))
{
char[] replace = inputObject[z].Hierarchy.ToCharArray();
replace[index] = '│';
inputObject[z].Hierarchy = new string(replace);
z--;
}
if (inputObject[z].Hierarchy[index] == '└')
{
char[] replace = inputObject[z].Hierarchy.ToCharArray();
replace[index] = '├';
inputObject[z].Hierarchy = new string(replace);
}
}
return inputObject;
}
}