Skip to content

Commit 0d7cba9

Browse files
authored
Merge pull request #44 from santisq/42-add-coloring-support-for-get-pstreeregistry
Adds coloring support for `Get-PSTreeRegistry`
2 parents de746c2 + 9dbcd2d commit 0d7cba9

26 files changed

+602
-254
lines changed

CHANGELOG.md

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

3+
- __02/26/2025__
4+
- Introduced the `RegistryStyle` class and added a `Registry` property to the `TreeStyle` class.
5+
- Moved file system style properties from `TreeStyle` to a new `FileSystemStyle` class, with a `FileSystem` property added to `TreeStyle`.
6+
- Added coloring support for `Get-PSTreeRegistry`, allowing customization based on the `Kind` property of `TreeRegistryBase` instances.
7+
38
- __02/22/2025__
49
- Renamed base types by removing the leading `PS` prefix (e.g., `PSTreeFile``TreeFile`, `PSTreeRegistryKey``TreeRegistryKey`) to streamline naming and improve consistency.
510
- Added the `Get-PSTreeRegistry` cmdlet, enabling tree-style traversal of the Windows Registry. This Windows-only cmdlet supports parameters like `-Path`, `-LiteralPath`, `-Depth`, `-Force`, and `-KeysOnly` for filtering and controlling output depth. Includes comprehensive Pester tests and documentation updates.
File renamed without changes.
File renamed without changes.

assets/Get-PSTreeRegistry.After.png

34.5 KB
Loading

assets/Get-PSTreeRegistry.Before.png

33.6 KB
Loading

assets/TreeStyle.png

-30.8 KB
Loading

docs/en-US/Get-PSTreeStyle.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
22
external help file: PSTree.dll-Help.xml
33
Module Name: PSTree
4-
online version:
4+
online version: https://github.com/santisq/PSTree/blob/main/docs/en-US/Get-PSTreeStyle.md
55
schema: 2.0.0
66
---
77

88
# Get-PSTreeStyle
99

1010
## SYNOPSIS
1111

12-
Gets the instance of `TreeStyle` used for output rendering.
12+
Retrieves the `TreeStyle` instance used for output rendering.
1313

1414
## SYNTAX
1515

@@ -20,7 +20,9 @@ Get-PSTreeStyle
2020

2121
## DESCRIPTION
2222

23-
The `Get-PSTreeStyle` cmdlet can be used to access the instance of `TreeStyle` used for rendering and customizing the `Get-PSTree` output. See [__about_TreeStyle__](./about_TreeStyle.md) for more information.
23+
The `Get-PSTreeStyle` cmdlet provides access to the `TreeStyle` instance that controls the rendering and customization of output for the `Get-PSTree` and `Get-PSTreeRegistry` cmdlets.
24+
25+
For details, see [__about_TreeStyle__](./about_TreeStyle.md).
2426

2527
## EXAMPLES
2628

@@ -30,6 +32,8 @@ The `Get-PSTreeStyle` cmdlet can be used to access the instance of `TreeStyle` u
3032
PS ..\PSTree> $style = Get-PSTreeStyle
3133
```
3234

35+
Stores the `TreeStyle` instance in the `$style` variable.
36+
3337
## PARAMETERS
3438

3539
### CommonParameters

docs/en-US/about_TreeStyle.md

Lines changed: 80 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
# about_TreeStyle
22

3+
## TOPIC
4+
5+
Customizing PSTree Output with TreeStyle.
6+
37
## SHORT DESCRIPTION
48

5-
Describes the available support for ANSI escape sequences in PSTree Module.
9+
The `TreeStyle` class enables customization of the hierarchical output for `Get-PSTree` and `Get-PSTreeRegistry` cmdlets in the PSTree module.
610

711
## LONG DESCRIPTION
812

9-
PSTree v2.2.0 adds support for coloring the hierarchy output from `Get-PSTree` cmdlet via the `TreeStyle` type. The type offers a subset of capabilities that the built-in [`PSStyle`][1] has, more specifically, the [`FileInfoFormatting`][2] subset.
10-
11-
The instance of this type can be accessed via the [`Get-PSTreeStyle`][3] cmdlet or the `[PSTree.Style.TreeStyle]::Instance` property:
13+
PSTree version 2.2.0 and later introduces support for coloring the hierarchical output of the `Get-PSTree` and `Get-PSTreeRegistry` cmdlets using the `TreeStyle` class. This class provides a subset of features similar to those in PowerShell’s built-in [PSStyle][1].
14+
You can access the singleton instance of `TreeStyle` through either the [Get-PSTreeStyle][2] cmdlet or the `[PSTree.Style.TreeStyle]::Instance` property:
1215

1316
<div>
1417
&nbsp;&nbsp;&nbsp;
1518
<img src="../../assets/TreeStyle.png" alt="TreeStyle" width="35%" height="35%">
1619
</div>
1720

18-
It has some useful methods to combine escape sequences as well as add accents, see next section for more details.
21+
The `TreeStyle` class offers methods for combining escape sequences and applying text accents, such as bold or italic. See the next section for additional details.
22+
23+
Here are its members:
1924

2025
```powershell
2126
TypeName: PSTree.Style.TreeStyle
@@ -31,15 +36,14 @@ ResetSettings Method void ResetSettings()
3136
ToBold Method string ToBold(string vt)
3237
ToItalic Method string ToItalic(string vt)
3338
ToString Method string ToString()
34-
Directory Property string Directory {get;set;}
35-
Executable Property string Executable {get;set;}
36-
Extension Property PSTree.Style.Extension Extension {get;}
39+
FileSystem Property PSTree.Style.FileSystemStyle FileSystem {get;}
3740
OutputRendering Property PSTree.Style.OutputRendering OutputRendering {get;set;}
3841
Palette Property PSTree.Style.Palette Palette {get;}
42+
Registry Property PSTree.Style.RegistryStyle Registry {get;}
3943
Reset Property string Reset {get;}
4044
```
4145

42-
The `EscapeSequence()` method can be used to see the escape sequence used to produce the color and accent, for example:
46+
The `.EscapeSequence()` method reveals the escape sequence applied to generate specific colors or accents. For example:
4347

4448
<div>
4549
&nbsp;&nbsp;&nbsp;
@@ -48,56 +52,101 @@ The `EscapeSequence()` method can be used to see the escape sequence used to pro
4852

4953
## CUSTOMIZING OUTPUT
5054

51-
Similar to `PSStyle` you can update the properties of `TreeStyle` as well as add an remove coloring for different extensions.
55+
### Get-PSTree
56+
57+
You can customize the output of [`Get-PSTree`][5] by modifying the properties of the `TreeStyle` class, much like you would with PowerShell’s `PSStyle`. This allows you to update colors for directories, files, and specific file extensions, as well as add or remove color schemes for different file types.
5258

5359
> [!NOTE]
5460
>
55-
> - For now, customizing the output of files that are a __SymbolicLink__ is not supported.
56-
> - The __Executable__ accent is only available for Windows Operating System.
61+
> - Customizing the output for files that are __Symbolic Links__ is not currently supported.
62+
> - The __Executable__ accent is only available on Windows operating systems.
5763
58-
For example, take the standard output:
64+
Consider the standard output of `Get-PSTree`:
5965

6066
<div>
6167
&nbsp;&nbsp;&nbsp;
62-
<img src="../../assets/Example.Before.png" alt="Example.Before" width="45%" height="45%">
68+
<img src="../../assets/Get-PSTree.Before.png" alt="Get-PSTree.Before" width="45%" height="45%">
6369
</div>
6470

65-
We can make a few changes to the style object:
71+
You can adjust the appearance by modifying the `PSTree.Style.FileSystemStyle` object. Here’s an example of how to apply customizations:
6672

6773
```powershell
6874
$style = Get-PSTreeStyle
6975
$palette = $style.Palette
70-
# update the .ps1 extension
71-
$style.Extension['.ps1'] = $style.CombineSequence($palette.Foreground.White, $palette.Background.Red)
72-
# add the .cs extension
73-
$style.Extension['.cs'] = $style.ToItalic($style.ToBold($palette.ForeGround.BrightCyan))
74-
# update the Directory style
75-
$style.Directory = "`e[45m"
76+
77+
# Update the .ps1 extension to white text on a red background
78+
$style.FileSystem.Extension['.ps1'] = $style.CombineSequence($palette.Foreground.White, $palette.Background.Red)
79+
80+
# Add the .cs extension with bold and italic bright cyan text
81+
$style.FileSystem.Extension['.cs'] = $style.ToItalic($style.ToBold($palette.Foreground.BrightCyan))
82+
83+
# Update the Directory style to use a magenta background
84+
$style.FileSystem.Directory = "`e[45m"
7685
```
7786

7887
> [!TIP]
7988
>
80-
> - The `` `e `` escape character was added in PowerShell 6. __Windows PowerShell 5.1__ users can use `[char] 27` instead, for example from previous example, instead of ``"`e[45m"`` you can use `"$([char] 27)[45m"`.
81-
See [__about_Special_Characters__][4] for more details.
82-
> - The `TreeStyle` type has 3 public methods that you can use to add accents or combine VT sequences, `ToItalic()`, `ToBold()` and `CombineSequence()`.
83-
> - You can also reset the style instance to its initial state using `.ResetSettings()` however if you had the instance stored in a variable you will need to re-assign its value, i.e.: `$style.ResetSettings()` then `$style = treestyle`.
89+
> - PowerShell 6 and later support the `` `e `` escape character for VT sequences. For __Windows PowerShell 5.1__, use `[char] 27` instead. For example, replace ``"`e[45m"`` with `"$([char] 27)[45m"`. See [about_Special_Characters][3] for more details.
90+
> - The `TreeStyle` class provides methods like `.ToItalic()`, `.ToBold()`, and `.CombineSequence()` to apply text accents or combine VT sequences.
91+
> - To reset the `TreeStyle` instance to its default state, use `.ResetSettings()`. If stored in a variable, reassign it afterward, e.g., `$style.ResetSettings()` followed by `$style = Get-PSTreeStyle`.
92+
93+
After applying these changes, re-running the same `Get-PSTree` command will display the updated styles:
94+
95+
<div>
96+
&nbsp;&nbsp;&nbsp;
97+
<img src="../../assets/Get-PSTree.After.png" alt="Get-PSTree.After" width="45%" height="45%">
98+
</div>
99+
100+
### Get-PSTreeRegistry
101+
102+
Starting with PSTree version 2.2.3, the [`Get-PSTreeRegistry`][6] cmdlet supports customizable coloring via the `PSTree.Style.RegistryStyle` object. This allows you to define colors for both `TreeRegistryKey` and `TreeRegistryValue` instances.
103+
104+
- __TreeRegistryKey__: The color is set using the `.RegistryKey` property.
105+
- __TreeRegistryValue__: The color is controlled by the `.RegistryValueKind` property, a dictionary that maps [RegistryValueKind][4] types to color settings based on the `.Kind` property of each registry value.
84106

85-
Then, if we re-run the same command we can see those changes in the PSTree output:
107+
> [!NOTE]
108+
> Keys in `.RegistryValueKind` must be of type [RegistryValueKind][4] or convertible to it. For example, both ``.RegistryValueKind[2] = "`e[45m"`` (where `2` corresponds to `ExpandString` Enum value) and ``.RegistryValueKind['ExpandString'] = "`e[45m"`` are valid. Similarly, ``.RegistryValueKind[1] = "`e[45m"`` or ``.RegistryValueKind['String'] = "`e[45m"`` can be used for `String`. String keys must match the enum names exactly, such as `'String'`, `'ExpandString'`, `'Binary'`, etc.
109+
110+
Here’s the standard output of `Get-PSTreeRegistry` before customization:
111+
112+
<div>
113+
&nbsp;&nbsp;&nbsp;
114+
<img src="../../assets/Get-PSTreeRegistry.Before.png" alt="Get-PSTreeRegistry.Before" width="45%" height="45%">
115+
</div>
116+
117+
You can customize the colors with the following code:
118+
119+
```powershell
120+
$style = Get-PSTreeStyle
121+
$palette = $style.Palette
122+
123+
# Set TreeRegistryKey instances to a red background
124+
$style.Registry.RegistryKey = $palette.Background.Red
125+
126+
# Set TreeRegistryValue instances of 'String' kind to bright green foreground
127+
$style.Registry.RegistryValueKind['String'] = $palette.Foreground.BrightGreen
128+
```
129+
130+
After applying these changes, re-running `Get-PSTreeRegistry` reflects the updated styles:
86131

87132
<div>
88133
&nbsp;&nbsp;&nbsp;
89-
<img src="../../assets/Example.After.png" alt="Example.Before" width="45%" height="45%">
134+
<img src="../../assets/Get-PSTreeRegistry.After.png" alt="Get-PSTreeRegistry.After" width="45%" height="45%">
90135
</div>
91136

92137
## DISABLING ANSI OUTPUT
93138

94-
Similarly to `PSStyle`, you can disable the ANSI rendering by updating the `OutputRendering` property:
139+
Just like PowerShell’s `PSStyle`, you can disable ANSI rendering in PSTree’s output by modifying the `.OutputRendering` property of the `TreeStyle` instance. Simply set it to `'PlainText'` using the following command:
95140

96141
```powershell
97142
(Get-PSTreeStyle).OutputRendering = 'PlainText'
98143
```
99144

145+
This disables all ANSI-based coloring and formatting, resulting in plain text output for commands like `Get-PSTree` and `Get-PSTreeRegistry`. It’s a straightforward way to simplify the display when you don’t need the extra visual styling.
146+
100147
[1]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_ansi_terminals
101-
[2]: https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.psstyle.fileinfoformatting
102-
[3]: ./Get-PSTreeStyle.md
103-
[4]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.4
148+
[2]: ./Get-PSTreeStyle.md
149+
[3]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.4
150+
[4]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registryvaluekind?view=net-9.0
151+
[5]: ./Get-PSTree.md
152+
[6]: ./Get-PSTreeRegistry.md

module/PSTree.Format.ps1xml

Lines changed: 96 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,41 @@
4444
</TableRowEntries>
4545
</TableControl>
4646
</View>
47+
<View>
48+
<Name>treeviewreg</Name>
49+
<ViewSelectedBy>
50+
<TypeName>PSTree.TreeRegistryBase</TypeName>
51+
</ViewSelectedBy>
52+
<GroupBy>
53+
<ScriptBlock>[PSTree.Internal._FormattingInternals]::GetSource($_)</ScriptBlock>
54+
<Label>Hive</Label>
55+
</GroupBy>
56+
<TableControl>
57+
<TableHeaders>
58+
<TableColumnHeader>
59+
<Label>Kind</Label>
60+
<Width>12</Width>
61+
<Alignment>Left</Alignment>
62+
</TableColumnHeader>
63+
<TableColumnHeader>
64+
<Label>Hierarchy</Label>
65+
<Alignment>Left</Alignment>
66+
</TableColumnHeader>
67+
</TableHeaders>
68+
<TableRowEntries>
69+
<TableRowEntry>
70+
<TableColumnItems>
71+
<TableColumnItem>
72+
<PropertyName>Kind</PropertyName>
73+
</TableColumnItem>
74+
<TableColumnItem>
75+
<PropertyName>Hierarchy</PropertyName>
76+
</TableColumnItem>
77+
</TableColumnItems>
78+
</TableRowEntry>
79+
</TableRowEntries>
80+
</TableControl>
81+
</View>
4782
<View>
4883
<Name>PSTree.Style.Palette</Name>
4984
<ViewSelectedBy>
@@ -190,6 +225,38 @@
190225
<Label>OutputRendering</Label>
191226
<PropertyName>OutputRendering</PropertyName>
192227
</ListItem>
228+
<ListItem>
229+
<Label>Reset</Label>
230+
<ScriptBlock>
231+
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Reset)
232+
</ScriptBlock>
233+
</ListItem>
234+
<ListItem>
235+
<Label>FileSystem</Label>
236+
<PropertyName>FileSystem</PropertyName>
237+
</ListItem>
238+
<ListItem>
239+
<Label>Registry</Label>
240+
<PropertyName>Registry</PropertyName>
241+
</ListItem>
242+
<ListItem>
243+
<Label>Palette</Label>
244+
<PropertyName>Palette</PropertyName>
245+
</ListItem>
246+
</ListItems>
247+
</ListEntry>
248+
</ListEntries>
249+
</ListControl>
250+
</View>
251+
<View>
252+
<Name>PSTree.Style.FileSystemStyle</Name>
253+
<ViewSelectedBy>
254+
<TypeName>PSTree.Style.FileSystemStyle</TypeName>
255+
</ViewSelectedBy>
256+
<ListControl>
257+
<ListEntries>
258+
<ListEntry>
259+
<ListItems>
193260
<ListItem>
194261
<Label>Directory</Label>
195262
<ScriptBlock>
@@ -203,18 +270,32 @@
203270
</ScriptBlock>
204271
</ListItem>
205272
<ListItem>
206-
<Label>Reset</Label>
207-
<ScriptBlock>
208-
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Reset)
209-
</ScriptBlock>
273+
<Label>Extension</Label>
274+
<PropertyName>Extension</PropertyName>
210275
</ListItem>
276+
</ListItems>
277+
</ListEntry>
278+
</ListEntries>
279+
</ListControl>
280+
</View>
281+
<View>
282+
<Name>PSTree.Style.RegistryStyle</Name>
283+
<ViewSelectedBy>
284+
<TypeName>PSTree.Style.RegistryStyle</TypeName>
285+
</ViewSelectedBy>
286+
<ListControl>
287+
<ListEntries>
288+
<ListEntry>
289+
<ListItems>
211290
<ListItem>
212-
<Label>Palette</Label>
213-
<PropertyName>Palette</PropertyName>
291+
<Label>RegistryKey</Label>
292+
<ScriptBlock>
293+
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.RegistryKey)
294+
</ScriptBlock>
214295
</ListItem>
215296
<ListItem>
216-
<Label>Extension</Label>
217-
<PropertyName>Extension</PropertyName>
297+
<Label>RegistryValueKind</Label>
298+
<PropertyName>RegistryValueKind</PropertyName>
218299
</ListItem>
219300
</ListItems>
220301
</ListEntry>
@@ -244,42 +325,37 @@
244325
<ScriptBlock>$_.Keys -join [Environment]::NewLine</ScriptBlock>
245326
</TableColumnItem>
246327
<TableColumnItem>
247-
<ScriptBlock>[PSTree.Style.Extension]::GetEscapedValues($_)</ScriptBlock>
328+
<ScriptBlock>$_.GetEscapedValues()</ScriptBlock>
248329
</TableColumnItem>
249330
</TableColumnItems>
250331
</TableRowEntry>
251332
</TableRowEntries>
252333
</TableControl>
253334
</View>
254335
<View>
255-
<Name>treeviewreg</Name>
336+
<Name>PSTree.Style.ValueKind</Name>
256337
<ViewSelectedBy>
257-
<TypeName>PSTree.TreeRegistryBase</TypeName>
338+
<TypeName>PSTree.Style.ValueKind</TypeName>
258339
</ViewSelectedBy>
259-
<GroupBy>
260-
<ScriptBlock>[PSTree.Internal._FormattingInternals]::GetSource($_)</ScriptBlock>
261-
<Label>Hive</Label>
262-
</GroupBy>
263340
<TableControl>
264341
<TableHeaders>
265342
<TableColumnHeader>
266343
<Label>Kind</Label>
267344
<Width>12</Width>
268-
<Alignment>Left</Alignment>
269345
</TableColumnHeader>
270346
<TableColumnHeader>
271-
<Label>Hierarchy</Label>
272-
<Alignment>Left</Alignment>
347+
<Label>Style</Label>
273348
</TableColumnHeader>
274349
</TableHeaders>
275350
<TableRowEntries>
276351
<TableRowEntry>
352+
<Wrap>true</Wrap>
277353
<TableColumnItems>
278354
<TableColumnItem>
279-
<PropertyName>Kind</PropertyName>
355+
<ScriptBlock>$_.Keys -join [Environment]::NewLine</ScriptBlock>
280356
</TableColumnItem>
281357
<TableColumnItem>
282-
<PropertyName>Hierarchy</PropertyName>
358+
<ScriptBlock>$_.GetEscapedValues()</ScriptBlock>
283359
</TableColumnItem>
284360
</TableColumnItems>
285361
</TableRowEntry>

0 commit comments

Comments
 (0)