Skip to content

Commit 8d4e534

Browse files
authored
Merge pull request #35 from santisq/34-request-add-a-count-property-for-each-folder-of-the-tree
- 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> ```
2 parents 0d99fb6 + 9eb485c commit 8d4e534

File tree

9 files changed

+93
-30
lines changed

9 files changed

+93
-30
lines changed

CHANGELOG.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
# CHANGELOG
22

3+
- __09/03/2024__
4+
- Makes `.Depth` property public for `PSTreeFileSystemInfo` instances.
5+
- Makes `.GetParents()` method private, absolutely no reason to have it public.
6+
- Added property `ItemCount` and `TotalItemCount` to `PSTreeDirectory` instances, requested in [__Issue #34__][2].
7+
8+
```powershell
9+
PS ..\PSTree> pstree -Recurse -Force -Directory | Select-Object Hierarchy, Depth, ItemCount, TotalItemCount -First 15
10+
11+
Hierarchy Depth ItemCount TotalItemCount
12+
--------- ----- --------- --------------
13+
PSTree 0 15 1476
14+
├── .git 1 13 1078
15+
│ ├── hooks 2 13 13
16+
│ ├── info 2 1 1
17+
│ ├── logs 2 2 24
18+
│ │ └── refs 3 2 22
19+
│ │ ├── heads 4 9 9
20+
│ │ └── remotes 4 1 11
21+
│ │ └── origin 5 10 10
22+
│ ├── objects 2 244 995
23+
│ │ ├── 00 3 3 3
24+
│ │ ├── 01 3 2 2
25+
│ │ ├── 02 3 3 3
26+
│ │ ├── 03 3 4 4
27+
│ │ ├── 04 3 2 2
28+
29+
PS ..\PSTree> (Get-ChildItem -Force).Count
30+
15
31+
PS ..\PSTree> (Get-ChildItem -Force -Recurse).Count
32+
1476
33+
PS ..\PSTree> (Get-ChildItem .git -Force -Recurse).Count
34+
1078
35+
PS ..\PSTree> (Get-ChildItem .git -Force).Count
36+
13
37+
PS ..\PSTree>
38+
```
39+
340
- __08/29/2024__
441
- Added method `.GetUnderlyingObject()`. Outputs the underlying `FileSystemInfo` instance.
542
- Fixes [__Issue #9: Sort by ascending values__][1]:
@@ -233,3 +270,4 @@ d---- └── Format 1.83 Kb
233270
[18]: https://github.com/jborean93/
234271
[19]: /docs/en-US/Get-PSTree.md
235272
[20]: https://www.powershellgallery.com/
273+
[2]: https://github.com/santisq/PSTree/issues/34

module/PSTree.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
RootModule = 'bin/netstandard2.0/PSTree.dll'
1212

1313
# Version number of this module.
14-
ModuleVersion = '2.1.17'
14+
ModuleVersion = '2.1.18'
1515

1616
# Supported PSEditions
1717
# CompatiblePSEditions = @()

src/PSTree/Commands/GetPSTreeCommand.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace PSTree.Commands;
99
[Cmdlet(VerbsCommon.Get, "PSTree", DefaultParameterSetName = "Path")]
1010
[OutputType(typeof(PSTreeDirectory), typeof(PSTreeFile))]
1111
[Alias("pstree")]
12-
public sealed partial class GetPSTreeCommand : PSCmdlet
12+
public sealed class GetPSTreeCommand : PSCmdlet
1313
{
1414
private bool _isLiteral;
1515

@@ -143,18 +143,17 @@ private PSTreeFileSystemInfo[] Traverse(
143143

144144
while (_stack.Count > 0)
145145
{
146-
IOrderedEnumerable<FileSystemInfo> enumerator;
147146
PSTreeDirectory next = _stack.Pop();
148147
int level = next.Depth + 1;
149148
long size = 0;
149+
int childCount = 0;
150150

151151
try
152152
{
153-
enumerator = next.GetSortedEnumerable(_comparer);
154153
bool keepProcessing = level <= Depth;
155-
156-
foreach (FileSystemInfo item in enumerator)
154+
foreach (FileSystemInfo item in next.GetSortedEnumerable(_comparer))
157155
{
156+
childCount++;
158157
if (!Force.IsPresent && item.IsHidden())
159158
{
160159
continue;
@@ -190,10 +189,11 @@ private PSTreeFileSystemInfo[] Traverse(
190189
}
191190

192191
next.Length = size;
192+
_indexer.IndexItemCount(next, childCount);
193193

194194
if (RecursiveSize.IsPresent)
195195
{
196-
_indexer.Index(next, size);
196+
_indexer.IndexLength(next, size);
197197
}
198198

199199
if (next.Depth <= Depth)

src/PSTree/Internal/_FormattingInternals.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public static string GetFormattedDate(DateTime date) =>
2828
string.Format(CultureInfo.CurrentCulture, "{0,10:d} {0,8:t}", date);
2929

3030
[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
31-
public static string GetSource(PSTreeFileSystemInfo item) =>
32-
item.Source;
31+
public static string GetSource(PSTreeFileSystemInfo item) => item.Source;
3332

3433
[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
3534
public static string GetFormattedLength(long length)

src/PSTree/PSTreeDirectory.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ namespace PSTree;
66

77
public sealed class PSTreeDirectory : PSTreeFileSystemInfo<DirectoryInfo>
88
{
9+
private string[]? _parents;
10+
11+
internal string[] Parents { get => _parents ??= GetParents(FullName); }
12+
913
public DirectoryInfo Parent => Instance.Parent;
1014

15+
public int ItemCount { get; internal set; }
16+
17+
public int TotalItemCount { get; internal set; }
18+
1119
internal PSTreeDirectory(DirectoryInfo directoryInfo, int depth, string source) :
1220
base(directoryInfo, depth, source)
1321
{ }
@@ -25,15 +33,17 @@ public IEnumerable<DirectoryInfo> EnumerateDirectories() =>
2533
public IEnumerable<FileSystemInfo> EnumerateFileSystemInfos() =>
2634
Instance.EnumerateFileSystemInfos();
2735

28-
public IEnumerable<string> GetParents()
36+
private static string[] GetParents(string path)
2937
{
3038
int index = -1;
31-
string path = Instance.FullName;
39+
List<string> parents = [];
3240

3341
while ((index = path.IndexOf(Path.DirectorySeparatorChar, index + 1)) != -1)
3442
{
35-
yield return path.Substring(0, index);
43+
parents.Add(path.Substring(0, index));
3644
}
45+
46+
return [.. parents];
3747
}
3848

3949
internal IOrderedEnumerable<FileSystemInfo> GetSortedEnumerable(PSTreeComparer comparer) =>

src/PSTree/PSTreeFileSystemInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public abstract class PSTreeFileSystemInfo(string hierarchy, string source)
44
{
55
internal string Source { get; set; } = source;
66

7-
internal int Depth { get; set; }
7+
public int Depth { get; protected set; }
88

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

src/PSTree/PSTreeIndexer.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@ internal sealed class PSTreeIndexer
77
{
88
private readonly Dictionary<string, PSTreeDirectory> _indexer = [];
99

10-
internal void Index(PSTreeDirectory directory, long length)
10+
internal void IndexLength(PSTreeDirectory directory, long length)
1111
{
1212
_indexer[directory.FullName.TrimEnd(Path.DirectorySeparatorChar)] = directory;
1313

14-
foreach (string parent in directory.GetParents())
14+
foreach (string parent in directory.Parents)
1515
{
16-
if (_indexer.ContainsKey(parent))
16+
if (_indexer.TryGetValue(parent, out PSTreeDirectory paretDir))
1717
{
18-
_indexer[parent].Length += length;
18+
paretDir.Length += length;
19+
}
20+
}
21+
}
22+
23+
internal void IndexItemCount(PSTreeDirectory directory, int count)
24+
{
25+
directory.ItemCount = count;
26+
directory.TotalItemCount = count;
27+
_indexer[directory.FullName.TrimEnd(Path.DirectorySeparatorChar)] = directory;
28+
29+
foreach (string parent in directory.Parents)
30+
{
31+
if (_indexer.TryGetValue(parent, out PSTreeDirectory parentDir))
32+
{
33+
parentDir.TotalItemCount += count;
1934
}
2035
}
2136
}

tests/GetPSTreeCommand.tests.ps1

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,14 @@ Describe 'Get-PSTree' {
9595
Should -BeIn ([PSTree.PSTreeFile], [PSTree.PSTreeDirectory])
9696
}
9797

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

103103
It 'Controls recursion with -Depth parameter' {
104-
$method = [PSTree.PSTreeFileSystemInfo].GetProperty(
105-
'Depth',
106-
[System.Reflection.BindingFlags] 'NonPublic, Instance')
107-
108104
$tree = Get-PSTree $testPath -Depth 1
109-
110-
$tree | ForEach-Object { $method.GetValue($_) } |
105+
$tree | ForEach-Object Depth |
111106
Should -Not -BeGreaterThan 2
112107

113108
$ref = (Get-ChildItem $testPath -Directory | Get-ChildItem).FullName

tests/PSTreeDirectory.tests.ps1

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ Describe 'PSTreeDirectory' {
2323
Should -BeIn ([System.IO.FileInfo], [System.IO.DirectoryInfo])
2424
}
2525

26-
It 'Can enumerate its parent directories with .GetParents()' {
27-
$treedir = $testPath | Get-PSTree -Depth 0
28-
$parent = $treedir.Parent
29-
$parent | Should -BeOfType ([System.IO.DirectoryInfo])
30-
$paths = $parent.FullName.Split([System.IO.Path]::DirectorySeparatorChar)
31-
$treedir.GetParents() | Should -HaveCount $paths.Count
26+
It '.Parent property gets the PSTreeDirectory Parent DirectoryInfo instance' {
27+
(Get-PSTree $testPath -Depth 0).Parent | Should -BeOfType ([System.IO.DirectoryInfo])
28+
}
29+
30+
It 'ItemCount gets the count of direct childs' {
31+
$childCount = @(Get-ChildItem -Force $testPath).Count
32+
(Get-PSTree $testPath -Depth 1 -Force)[0].ItemCount | Should -BeExactly $childCount
33+
}
34+
35+
It 'TotalItemCount gets the recursive count of childs' {
36+
$childCount = @(Get-ChildItem -Force $testPath -Recurse).Count
37+
(Get-PSTree $testPath -Recurse -Force)[0].TotalItemCount | Should -BeExactly $childCount
3238
}
3339
}

0 commit comments

Comments
 (0)