Skip to content

Commit 3567b8b

Browse files
committed
simplifies logic to get item and totalitem count
1 parent e04d1e9 commit 3567b8b

File tree

6 files changed

+42
-36
lines changed

6 files changed

+42
-36
lines changed

src/PSTree/Cache.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,35 @@ internal void Flush()
2323
}
2424
}
2525

26-
internal PSTreeFileSystemInfo[] GetTree(bool condition) =>
27-
condition
28-
? _items.Where(IsIncluded).ToArray().Format()
29-
: _items.ToArray().Format();
26+
internal PSTreeFileSystemInfo[] GetTree(bool condition)
27+
{
28+
PSTreeFileSystemInfo[] result = condition
29+
? [.. _items.Where(static e => e.ShouldInclude)]
30+
: [.. _items];
31+
32+
return result.Format(GetItemCount(result));
33+
}
3034

31-
private static bool IsIncluded(PSTreeFileSystemInfo item)
35+
private static Dictionary<string, int> GetItemCount(PSTreeFileSystemInfo[] items)
3236
{
33-
if (item.ShouldInclude && item is PSTreeDirectory dir)
37+
Dictionary<string, int> counts = [];
38+
foreach (PSTreeFileSystemInfo item in items)
3439
{
35-
dir.IncrementItemCount();
40+
string? path = item.ParentNode?.FullName;
41+
if (path is null)
42+
{
43+
continue;
44+
}
45+
46+
if (!counts.ContainsKey(path))
47+
{
48+
counts[path] = 0;
49+
}
50+
51+
counts[path]++;
3652
}
3753

38-
return item.ShouldInclude;
54+
return counts;
3955
}
4056

4157
internal void Clear()

src/PSTree/Commands/GetPSTreeCommand.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)
108108
PSTreeDirectory next = _stack.Pop();
109109
int level = next.Depth + 1;
110110
long totalLength = 0;
111-
int childCount = 0;
112111

113112
try
114113
{
@@ -142,8 +141,6 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)
142141

143142
if (keepProcessing)
144143
{
145-
childCount++;
146-
147144
PSTreeFile file = PSTreeFile
148145
.Create(fileInfo, source, level)
149146
.AddParent(next)
@@ -167,14 +164,12 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)
167164
if (keepProcessing && Directory || !_withInclude)
168165
{
169166
dir.ShouldInclude = true;
170-
childCount++;
171167
}
172168

173169
_stack.Push(dir);
174170
}
175171

176172
next.Length = totalLength;
177-
next.IndexCount(childCount);
178173

179174
if (RecursiveSize)
180175
{

src/PSTree/Extensions/TreeExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34

45
namespace PSTree.Extensions;
@@ -19,12 +20,21 @@ internal static string Indent(this string inputString, int indentation)
1920
.ToString();
2021
}
2122

22-
internal static PSTreeFileSystemInfo[] Format(this PSTreeFileSystemInfo[] tree)
23+
internal static PSTreeFileSystemInfo[] Format(
24+
this PSTreeFileSystemInfo[] tree,
25+
Dictionary<string, int> itemCounts)
2326
{
2427
int index;
2528
for (int i = 0; i < tree.Length; i++)
2629
{
2730
PSTreeFileSystemInfo current = tree[i];
31+
32+
if (current is PSTreeDirectory directory &&
33+
itemCounts.TryGetValue(directory.FullName, out int count))
34+
{
35+
directory.IndexCount(count);
36+
}
37+
2838
if ((index = current.Hierarchy.IndexOf('└')) == -1)
2939
{
3040
continue;

src/PSTree/PSTreeDirectory.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ internal static PSTreeDirectory Create(DirectoryInfo dir, string source, int dep
5858

5959
internal PSTreeDirectory AddParent(PSTreeDirectory parent)
6060
{
61-
_parent = parent;
61+
ParentNode = parent;
6262
return this;
6363
}
6464

@@ -67,15 +67,15 @@ internal void IndexCount(int count)
6767
ItemCount = count;
6868
TotalItemCount = count;
6969

70-
for (PSTreeDirectory? i = _parent; i is not null; i = i._parent)
70+
for (PSTreeDirectory? i = ParentNode; i is not null; i = i.ParentNode)
7171
{
7272
i.TotalItemCount += count;
7373
}
7474
}
7575

7676
internal void IndexLength(long length)
7777
{
78-
for (PSTreeDirectory? i = _parent; i is not null; i = i._parent)
78+
for (PSTreeDirectory? i = ParentNode; i is not null; i = i.ParentNode)
7979
{
8080
i.Length += length;
8181
}
@@ -85,7 +85,7 @@ internal void SetIncludeFlag()
8585
{
8686
ShouldInclude = true;
8787

88-
for (PSTreeDirectory? i = _parent; i is not null; i = i._parent)
88+
for (PSTreeDirectory? i = ParentNode; i is not null; i = i.ParentNode)
8989
{
9090
if (i.ShouldInclude)
9191
{
@@ -95,19 +95,4 @@ internal void SetIncludeFlag()
9595
i.ShouldInclude = true;
9696
}
9797
}
98-
99-
internal void IncrementItemCount()
100-
{
101-
if (_parent is null)
102-
{
103-
return;
104-
}
105-
106-
_parent.ItemCount++;
107-
108-
for (PSTreeDirectory? i = _parent; i is not null; i = i._parent)
109-
{
110-
i.TotalItemCount++;
111-
}
112-
}
11398
}

src/PSTree/PSTreeFile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ internal static PSTreeFile Create(FileInfo file, string source, int depth)
4040

4141
internal PSTreeFile AddParent(PSTreeDirectory parent)
4242
{
43-
_parent = parent;
43+
ParentNode = parent;
4444
return this;
4545
}
4646

4747
internal PSTreeFile SetIncludeFlagIf(bool condition)
4848
{
4949
if (condition)
5050
{
51-
_parent?.SetIncludeFlag();
51+
ParentNode?.SetIncludeFlag();
5252
}
5353

5454
return this;

src/PSTree/PSTreeFileSystemInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace PSTree;
22

33
public abstract class PSTreeFileSystemInfo(string hierarchy, string source)
44
{
5-
protected PSTreeDirectory? _parent;
5+
internal PSTreeDirectory? ParentNode { get; set; }
66

77
internal bool ShouldInclude { get; set; }
88

0 commit comments

Comments
 (0)