Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Item count properties #35

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
# CHANGELOG

- __09/03/2024__
- Makes `.Depth` property public for `PSTreeFileSystemInfo` instances.
- Makes `.GetParents()` method private, absolutely no reason to have it public.
- Added property `ItemCount` and `TotalItemCount` to `PSTreeDirectory` instances, requested in [__Issue #34__][2].

```powershell
PS ..\PSTree> pstree -Recurse -Force -Directory | Select-Object Hierarchy, Depth, ItemCount, TotalItemCount -First 15

Hierarchy Depth ItemCount TotalItemCount
--------- ----- --------- --------------
PSTree 0 15 1476
├── .git 1 13 1078
│ ├── hooks 2 13 13
│ ├── info 2 1 1
│ ├── logs 2 2 24
│ │ └── refs 3 2 22
│ │ ├── heads 4 9 9
│ │ └── remotes 4 1 11
│ │ └── origin 5 10 10
│ ├── objects 2 244 995
│ │ ├── 00 3 3 3
│ │ ├── 01 3 2 2
│ │ ├── 02 3 3 3
│ │ ├── 03 3 4 4
│ │ ├── 04 3 2 2

PS ..\PSTree> (Get-ChildItem -Force).Count
15
PS ..\PSTree> (Get-ChildItem -Force -Recurse).Count
1476
PS ..\PSTree> (Get-ChildItem .git -Force -Recurse).Count
1078
PS ..\PSTree> (Get-ChildItem .git -Force).Count
13
PS ..\PSTree>
```

- __08/29/2024__
- Added method `.GetUnderlyingObject()`. Outputs the underlying `FileSystemInfo` instance.
- Fixes [__Issue #9: Sort by ascending values__][1]:
Expand Down Expand Up @@ -233,3 +270,4 @@ d---- └── Format 1.83 Kb
[18]: https://github.com/jborean93/
[19]: /docs/en-US/Get-PSTree.md
[20]: https://www.powershellgallery.com/
[2]: https://github.com/santisq/PSTree/issues/34
2 changes: 1 addition & 1 deletion module/PSTree.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RootModule = 'bin/netstandard2.0/PSTree.dll'

# Version number of this module.
ModuleVersion = '2.1.17'
ModuleVersion = '2.1.18'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
12 changes: 6 additions & 6 deletions src/PSTree/Commands/GetPSTreeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PSTree.Commands;
[Cmdlet(VerbsCommon.Get, "PSTree", DefaultParameterSetName = "Path")]
[OutputType(typeof(PSTreeDirectory), typeof(PSTreeFile))]
[Alias("pstree")]
public sealed partial class GetPSTreeCommand : PSCmdlet
public sealed class GetPSTreeCommand : PSCmdlet
{
private bool _isLiteral;

Expand Down Expand Up @@ -143,18 +143,17 @@ private PSTreeFileSystemInfo[] Traverse(

while (_stack.Count > 0)
{
IOrderedEnumerable<FileSystemInfo> enumerator;
PSTreeDirectory next = _stack.Pop();
int level = next.Depth + 1;
long size = 0;
int childCount = 0;

try
{
enumerator = next.GetSortedEnumerable(_comparer);
bool keepProcessing = level <= Depth;

foreach (FileSystemInfo item in enumerator)
foreach (FileSystemInfo item in next.GetSortedEnumerable(_comparer))
{
childCount++;
if (!Force.IsPresent && item.IsHidden())
{
continue;
Expand Down Expand Up @@ -190,10 +189,11 @@ private PSTreeFileSystemInfo[] Traverse(
}

next.Length = size;
_indexer.IndexItemCount(next, childCount);

if (RecursiveSize.IsPresent)
{
_indexer.Index(next, size);
_indexer.IndexLength(next, size);
}

if (next.Depth <= Depth)
Expand Down
3 changes: 1 addition & 2 deletions src/PSTree/Internal/_FormattingInternals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public static string GetFormattedDate(DateTime date) =>
string.Format(CultureInfo.CurrentCulture, "{0,10:d} {0,8:t}", date);

[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
public static string GetSource(PSTreeFileSystemInfo item) =>
item.Source;
public static string GetSource(PSTreeFileSystemInfo item) => item.Source;

[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
public static string GetFormattedLength(long length)
Expand Down
16 changes: 13 additions & 3 deletions src/PSTree/PSTreeDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ namespace PSTree;

public sealed class PSTreeDirectory : PSTreeFileSystemInfo<DirectoryInfo>
{
private string[]? _parents;

internal string[] Parents { get => _parents ??= GetParents(FullName); }

public DirectoryInfo Parent => Instance.Parent;

public int ItemCount { get; internal set; }

public int TotalItemCount { get; internal set; }

internal PSTreeDirectory(DirectoryInfo directoryInfo, int depth, string source) :
base(directoryInfo, depth, source)
{ }
Expand All @@ -25,15 +33,17 @@ public IEnumerable<DirectoryInfo> EnumerateDirectories() =>
public IEnumerable<FileSystemInfo> EnumerateFileSystemInfos() =>
Instance.EnumerateFileSystemInfos();

public IEnumerable<string> GetParents()
private static string[] GetParents(string path)
{
int index = -1;
string path = Instance.FullName;
List<string> parents = [];

while ((index = path.IndexOf(Path.DirectorySeparatorChar, index + 1)) != -1)
{
yield return path.Substring(0, index);
parents.Add(path.Substring(0, index));
}

return [.. parents];
}

internal IOrderedEnumerable<FileSystemInfo> GetSortedEnumerable(PSTreeComparer comparer) =>
Expand Down
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)
{
internal string Source { get; set; } = source;

internal int Depth { get; set; }
public int Depth { get; protected set; }

public string Hierarchy { get; internal set; } = hierarchy;

Expand Down
23 changes: 19 additions & 4 deletions src/PSTree/PSTreeIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,30 @@ internal sealed class PSTreeIndexer
{
private readonly Dictionary<string, PSTreeDirectory> _indexer = [];

internal void Index(PSTreeDirectory directory, long length)
internal void IndexLength(PSTreeDirectory directory, long length)
{
_indexer[directory.FullName.TrimEnd(Path.DirectorySeparatorChar)] = directory;

foreach (string parent in directory.GetParents())
foreach (string parent in directory.Parents)
{
if (_indexer.ContainsKey(parent))
if (_indexer.TryGetValue(parent, out PSTreeDirectory paretDir))
{
_indexer[parent].Length += length;
paretDir.Length += length;
}
}
}

internal void IndexItemCount(PSTreeDirectory directory, int count)
{
directory.ItemCount = count;
directory.TotalItemCount = count;
_indexer[directory.FullName.TrimEnd(Path.DirectorySeparatorChar)] = directory;

foreach (string parent in directory.Parents)
{
if (_indexer.TryGetValue(parent, out PSTreeDirectory parentDir))
{
parentDir.TotalItemCount += count;
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions tests/GetPSTreeCommand.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,14 @@ Describe 'Get-PSTree' {
Should -BeIn ([PSTree.PSTreeFile], [PSTree.PSTreeDirectory])
}

It 'Excludes PSTressFile instances with -Directory' {
It 'Excludes PSTreeFile instances with -Directory' {
Get-PSTree -LiteralPath $testPath -Directory |
Should -BeOfType ([PSTree.PSTreeDirectory])
}

It 'Controls recursion with -Depth parameter' {
$method = [PSTree.PSTreeFileSystemInfo].GetProperty(
'Depth',
[System.Reflection.BindingFlags] 'NonPublic, Instance')

$tree = Get-PSTree $testPath -Depth 1

$tree | ForEach-Object { $method.GetValue($_) } |
$tree | ForEach-Object Depth |
Should -Not -BeGreaterThan 2

$ref = (Get-ChildItem $testPath -Directory | Get-ChildItem).FullName
Expand Down
18 changes: 12 additions & 6 deletions tests/PSTreeDirectory.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ Describe 'PSTreeDirectory' {
Should -BeIn ([System.IO.FileInfo], [System.IO.DirectoryInfo])
}

It 'Can enumerate its parent directories with .GetParents()' {
$treedir = $testPath | Get-PSTree -Depth 0
$parent = $treedir.Parent
$parent | Should -BeOfType ([System.IO.DirectoryInfo])
$paths = $parent.FullName.Split([System.IO.Path]::DirectorySeparatorChar)
$treedir.GetParents() | Should -HaveCount $paths.Count
It '.Parent property gets the PSTreeDirectory Parent DirectoryInfo instance' {
(Get-PSTree $testPath -Depth 0).Parent | Should -BeOfType ([System.IO.DirectoryInfo])
}

It 'ItemCount gets the count of direct childs' {
$childCount = @(Get-ChildItem -Force $testPath).Count
(Get-PSTree $testPath -Depth 1 -Force)[0].ItemCount | Should -BeExactly $childCount
}

It 'TotalItemCount gets the recursive count of childs' {
$childCount = @(Get-ChildItem -Force $testPath -Recurse).Count
(Get-PSTree $testPath -Recurse -Force)[0].TotalItemCount | Should -BeExactly $childCount
}
}