Skip to content

Commit

Permalink
simplifies logic to get item and totalitem count
Browse files Browse the repository at this point in the history
  • Loading branch information
santisq committed Jan 19, 2025
1 parent e04d1e9 commit 3567b8b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 36 deletions.
32 changes: 24 additions & 8 deletions src/PSTree/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,35 @@ internal void Flush()
}
}

internal PSTreeFileSystemInfo[] GetTree(bool condition) =>
condition
? _items.Where(IsIncluded).ToArray().Format()
: _items.ToArray().Format();
internal PSTreeFileSystemInfo[] GetTree(bool condition)
{
PSTreeFileSystemInfo[] result = condition
? [.. _items.Where(static e => e.ShouldInclude)]
: [.. _items];

return result.Format(GetItemCount(result));
}

private static bool IsIncluded(PSTreeFileSystemInfo item)
private static Dictionary<string, int> GetItemCount(PSTreeFileSystemInfo[] items)
{
if (item.ShouldInclude && item is PSTreeDirectory dir)
Dictionary<string, int> counts = [];
foreach (PSTreeFileSystemInfo item in items)
{
dir.IncrementItemCount();
string? path = item.ParentNode?.FullName;
if (path is null)
{
continue;
}

if (!counts.ContainsKey(path))
{
counts[path] = 0;
}

counts[path]++;
}

return item.ShouldInclude;
return counts;
}

internal void Clear()
Expand Down
5 changes: 0 additions & 5 deletions src/PSTree/Commands/GetPSTreeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)
PSTreeDirectory next = _stack.Pop();
int level = next.Depth + 1;
long totalLength = 0;
int childCount = 0;

try
{
Expand Down Expand Up @@ -142,8 +141,6 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)

if (keepProcessing)
{
childCount++;

PSTreeFile file = PSTreeFile
.Create(fileInfo, source, level)
.AddParent(next)
Expand All @@ -167,14 +164,12 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)
if (keepProcessing && Directory || !_withInclude)
{
dir.ShouldInclude = true;
childCount++;
}

_stack.Push(dir);
}

next.Length = totalLength;
next.IndexCount(childCount);

if (RecursiveSize)
{
Expand Down
12 changes: 11 additions & 1 deletion src/PSTree/Extensions/TreeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;

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

internal static PSTreeFileSystemInfo[] Format(this PSTreeFileSystemInfo[] tree)
internal static PSTreeFileSystemInfo[] Format(
this PSTreeFileSystemInfo[] tree,
Dictionary<string, int> itemCounts)
{
int index;
for (int i = 0; i < tree.Length; i++)
{
PSTreeFileSystemInfo current = tree[i];

if (current is PSTreeDirectory directory &&
itemCounts.TryGetValue(directory.FullName, out int count))
{
directory.IndexCount(count);
}

if ((index = current.Hierarchy.IndexOf('└')) == -1)
{
continue;
Expand Down
23 changes: 4 additions & 19 deletions src/PSTree/PSTreeDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal static PSTreeDirectory Create(DirectoryInfo dir, string source, int dep

internal PSTreeDirectory AddParent(PSTreeDirectory parent)
{
_parent = parent;
ParentNode = parent;
return this;
}

Expand All @@ -67,15 +67,15 @@ internal void IndexCount(int count)
ItemCount = count;
TotalItemCount = count;

for (PSTreeDirectory? i = _parent; i is not null; i = i._parent)
for (PSTreeDirectory? i = ParentNode; i is not null; i = i.ParentNode)
{
i.TotalItemCount += count;
}
}

internal void IndexLength(long length)
{
for (PSTreeDirectory? i = _parent; i is not null; i = i._parent)
for (PSTreeDirectory? i = ParentNode; i is not null; i = i.ParentNode)
{
i.Length += length;
}
Expand All @@ -85,7 +85,7 @@ internal void SetIncludeFlag()
{
ShouldInclude = true;

for (PSTreeDirectory? i = _parent; i is not null; i = i._parent)
for (PSTreeDirectory? i = ParentNode; i is not null; i = i.ParentNode)
{
if (i.ShouldInclude)
{
Expand All @@ -95,19 +95,4 @@ internal void SetIncludeFlag()
i.ShouldInclude = true;
}
}

internal void IncrementItemCount()
{
if (_parent is null)
{
return;
}

_parent.ItemCount++;

for (PSTreeDirectory? i = _parent; i is not null; i = i._parent)
{
i.TotalItemCount++;
}
}
}
4 changes: 2 additions & 2 deletions src/PSTree/PSTreeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ internal static PSTreeFile Create(FileInfo file, string source, int depth)

internal PSTreeFile AddParent(PSTreeDirectory parent)
{
_parent = parent;
ParentNode = parent;
return this;
}

internal PSTreeFile SetIncludeFlagIf(bool condition)
{
if (condition)
{
_parent?.SetIncludeFlag();
ParentNode?.SetIncludeFlag();
}

return this;
Expand Down
2 changes: 1 addition & 1 deletion src/PSTree/PSTreeFileSystemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace PSTree;

public abstract class PSTreeFileSystemInfo(string hierarchy, string source)
{
protected PSTreeDirectory? _parent;
internal PSTreeDirectory? ParentNode { get; set; }

internal bool ShouldInclude { get; set; }

Expand Down

0 comments on commit 3567b8b

Please sign in to comment.