Skip to content

Commit

Permalink
almost there, need to work on item counts
Browse files Browse the repository at this point in the history
  • Loading branch information
santisq committed Jan 17, 2025
1 parent 6588c0e commit ffc7c85
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 25 deletions.
14 changes: 7 additions & 7 deletions src/PSTree/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ internal sealed class Cache

private readonly List<PSTreeFile> _files = [];

internal void AddFile(PSTreeFile file) => _files.Add(file);
internal void Add(PSTreeFile file) => _files.Add(file);

internal void Add(PSTreeFileSystemInfo item) => _items.Add(item);
internal void Add(PSTreeDirectory directory) => _items.Add(directory);

internal void TryAddFiles()
internal void Flush()
{
if (_files.Count > 0)
{
Expand All @@ -23,10 +23,10 @@ internal void TryAddFiles()
}
}

internal PSTreeFileSystemInfo[] GetTree() => _items
.Where(e => e._shouldInclude)
.ToArray()
.ConvertToTree();
internal PSTreeFileSystemInfo[] GetTree(bool filterInclude) =>
filterInclude
? _items.Where(e => e.ShouldInclude).ToArray().ConvertToTree()
: _items.ToArray().ConvertToTree();

internal void Clear()
{
Expand Down
21 changes: 14 additions & 7 deletions src/PSTree/Commands/GetPSTreeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ protected override void ProcessRecord()
{
if (File.Exists(path))
{
WriteObject(PSTreeFile.Create(path));
FileInfo file = new(path);
if (!ShouldExclude(file) && ShouldInclude(file))
{
WriteObject(PSTreeFile.Create(file, path));
}

continue;
}

Expand Down Expand Up @@ -118,6 +123,7 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)
{
if (Directory.IsPresent)
{
size += fileInfo.Length;
continue;
}

Expand All @@ -128,9 +134,10 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)

PSTreeFile file = PSTreeFile
.Create(fileInfo, source, level)
.WithParent(next);
.WithParent(next)
.WithIncludeFlagIf(_includePatterns is not null);

_cache.AddFile(file);
_cache.Add(file);
}

continue;
Expand All @@ -144,7 +151,7 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)

if (_includePatterns is null)
{
dir._shouldInclude = true;
dir.ShouldInclude = true;
childCount++;
}

Expand All @@ -153,7 +160,7 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)
}

next.Length = size;
next.IndexItemCount(childCount);
next.IndexCount(childCount);

if (RecursiveSize.IsPresent)
{
Expand All @@ -163,7 +170,7 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)
if (next.Depth <= Depth)
{
_cache.Add(next);
_cache.TryAddFiles();
_cache.Flush();
}
}
catch (Exception exception)
Expand All @@ -177,7 +184,7 @@ private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)
}
}

return _cache.GetTree();
return _cache.GetTree(_includePatterns is not null);
}

private static bool MatchAny(string name, WildcardPattern[] patterns)
Expand Down
1 change: 1 addition & 0 deletions src/PSTree/Extensions/TreeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal static PSTreeFileSystemInfo[] ConvertToTree(
{
int index;
PSTreeFileSystemInfo current;

for (int i = 0; i < inputObject.Length; i++)
{
current = inputObject[i];
Expand Down
12 changes: 7 additions & 5 deletions src/PSTree/PSTreeDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ private PSTreeDirectory(
private PSTreeDirectory(
DirectoryInfo dir, string hierarchy, string source)
: base(dir, hierarchy, source)
{ }
{
ShouldInclude = true;
}

public IEnumerable<FileInfo> EnumerateFiles() =>
Instance.EnumerateFiles();
Expand Down Expand Up @@ -59,7 +61,7 @@ internal PSTreeDirectory WithParent(PSTreeDirectory parent)
return this;
}

internal void IndexItemCount(int count)
internal void IndexCount(int count)
{
ItemCount = count;
TotalItemCount = count;
Expand All @@ -80,15 +82,15 @@ internal void IndexLength(long length)

internal void SetIncludeFlag()
{
_shouldInclude = true;
ShouldInclude = true;
for (PSTreeDirectory? parent = _parent; parent is not null; parent = parent._parent)
{
if (parent._shouldInclude)
if (parent.ShouldInclude)
{
return;
}

parent._shouldInclude = true;
parent.ShouldInclude = true;
}
}
}
17 changes: 12 additions & 5 deletions src/PSTree/PSTreeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ private PSTreeFile(
: base(file, hierarchy, source)
{
Length = file.Length;
_shouldInclude = true;
ShouldInclude = true;
}

private PSTreeFile(
FileInfo file, string hierarchy, string source, int depth)
: base(file, hierarchy, source, depth)
{
Length = file.Length;
_shouldInclude = true;
ShouldInclude = true;
}

internal static PSTreeFile Create(string path) => Create(new FileInfo(path), path);

internal static PSTreeFile Create(FileInfo file, string source)
{
string styled = TreeStyle.Instance.GetColoredName(file);
Expand All @@ -43,7 +41,16 @@ internal static PSTreeFile Create(FileInfo file, string source, int depth)
internal PSTreeFile WithParent(PSTreeDirectory parent)
{
_parent = parent;
_parent.SetIncludeFlag();
return this;
}

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

return this;
}
}
2 changes: 1 addition & 1 deletion src/PSTree/PSTreeFileSystemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public abstract class PSTreeFileSystemInfo(string hierarchy, string source)
{
protected PSTreeDirectory? _parent;

internal bool _shouldInclude;
internal bool ShouldInclude { get; set; }

internal string Source { get; set; } = source;

Expand Down
8 changes: 8 additions & 0 deletions tests/GetPSTreeCommand.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ Describe 'Get-PSTree' {
[string[]] $exclude,
[System.Func[string, bool]] { $_.FullName -like $args[0] })
} | Should -Not -BeTrue

Get-ChildItem $testPath -Filter *.ps1 -Recurse |
Get-PSTree -Exclude *.ps1 |
Should -BeNullOrEmpty
}

It 'Includes child items with -Include parameter' {
Expand All @@ -125,6 +129,10 @@ Describe 'Get-PSTree' {
}
)
} | Should -BeTrue

Get-ChildItem $testPath -Filter *.ps1 -Recurse |
Get-PSTree -Include *.ps1 |
Should -Not -BeNullOrEmpty
}

It 'Should prioritize -Depth if used together with -Recurse' {
Expand Down

0 comments on commit ffc7c85

Please sign in to comment.