Skip to content

Commit

Permalink
Merge pull request #37 from santisq/36-add-styles-to-pstree
Browse files Browse the repository at this point in the history
Adds `TreeStyle` to PSTree
  • Loading branch information
santisq authored Sep 12, 2024
2 parents d20cd59 + d390a6e commit 29cab84
Show file tree
Hide file tree
Showing 24 changed files with 985 additions and 102 deletions.
Binary file added assets/EscapeSequence.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Example.After.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Example.Before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/TreeStyle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions docs/en-US/Get-PSTreeStyle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
external help file: PSTree.dll-Help.xml
Module Name: PSTree
online version:
schema: 2.0.0
---

# Get-PSTreeStyle

## SYNOPSIS

Gets the instance of `TreeStyle` used for output rendering.

## SYNTAX

```powershell
Get-PSTreeStyle
[<CommonParameters>]
```

## DESCRIPTION

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.

## EXAMPLES

### Example 1

```powershell
PS ..\PSTree> $style = Get-PSTreeStyle
```

## PARAMETERS

### CommonParameters

This cmdlet supports the common parameters. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).

## INPUTS

### None

## OUTPUTS

### TreeStyle
103 changes: 103 additions & 0 deletions docs/en-US/about_TreeStyle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# about_TreeStyle

## SHORT DESCRIPTION

Describes the available support for ANSI escape sequences in PSTree Module.

## LONG DESCRIPTION

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.

The instance of this type can be accessed via the [`Get-PSTreeStyle`][3] cmdlet or the `[PSTree.Style.TreeStyle]::Instance` property:

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

It has some useful methods to combine escape sequences as well as add accents, see next section for more details.

```powershell
TypeName: PSTree.Style.TreeStyle
Name MemberType Definition
---- ---------- ----------
CombineSequence Method string CombineSequence(string left, string right)
Equals Method bool Equals(System.Object obj)
EscapeSequence Method string EscapeSequence(string vt)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ResetSettings Method void ResetSettings()
ToBold Method string ToBold(string vt)
ToItalic Method string ToItalic(string vt)
ToString Method string ToString()
Directory Property string Directory {get;set;}
Executable Property string Executable {get;set;}
Extension Property PSTree.Style.Extension Extension {get;}
OutputRendering Property PSTree.Style.OutputRendering OutputRendering {get;set;}
Palette Property PSTree.Style.Palette Palette {get;}
Reset Property string Reset {get;}
```

The `EscapeSequence()` method can be used to see the escape sequence used to produce the color and accent, for example:

<div>
&nbsp;&nbsp;&nbsp;
<img src="../../assets/EscapeSequence.png" alt="TreeStyle" width="45%" height="45%">
</div>

## CUSTOMIZING OUTPUT

Similar to `PSStyle` you can update the properties of `TreeStyle` as well as add an remove coloring for different extensions.

> [!NOTE]
>
> - For now, customizing the output of files that are a __SymbolicLink__ is not supported.
> - The __Executable__ accent is only available for Windows Operating System.
For example, take the standard output:

<div>
&nbsp;&nbsp;&nbsp;
<img src="../../assets/Example.Before.png" alt="Example.Before" width="45%" height="45%">
</div>

We can make a few changes to the style object:

```powershell
$style = Get-PSTreeStyle
$palette = $style.Palette
# update the .ps1 extension
$style.Extension['.ps1'] = $style.CombineSequence($palette.Foreground.White, $palette.Background.Red)
# add the .cs extension
$style.Extension['.cs'] = $style.ToItalic($style.ToBold($palette.ForeGround.BrightCyan))
# update the Directory style
$style.Directory = "`e[45m"
```

> [!TIP]
>
> - 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"`.
See [__about_Special_Characters__][4] for more details.
> - The `TreeStyle` type has 3 public methods that you can use to add accents or combine VT sequences, `ToItalic()`, `ToBold()` and `CombineSequence()`.
> - 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`.
Then, if we re-run the same command we can see those changes in the PSTree output:

<div>
&nbsp;&nbsp;&nbsp;
<img src="../../assets/Example.After.png" alt="Example.Before" width="45%" height="45%">
</div>

## DISABLING ANSI OUTPUT

Similarly to `PSStyle`, you can disable the ANSI rendering by updating the `OutputRendering` property:

```powershell
(Get-PSTreeStyle).OutputRendering = 'PlainText'
```

[1]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_ansi_terminals
[2]: https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.psstyle.fileinfoformatting
[3]: ./Get-PSTreeStyle.md
[4]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.4
207 changes: 207 additions & 0 deletions module/PSTree.Format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,212 @@
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>PSTree.Style.Palette</Name>
<ViewSelectedBy>
<TypeName>PSTree.Style.Palette</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<Label>Foreground</Label>
<PropertyName>Foreground</PropertyName>
</ListItem>
<ListItem>
<Label>Background</Label>
<PropertyName>Background</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
<View>
<Name>PSTree.Style.Palette.Palettes</Name>
<ViewSelectedBy>
<TypeName>PSTree.Style.Palette+ForegroundPalette</TypeName>
<TypeName>PSTree.Style.Palette+BackgroundPalette</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<Label>Black</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Black)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>BrightBlack</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.BrightBlack)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>White</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.White)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>BrightWhite</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.BrightWhite)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>Red</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Red)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>BrightRed</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.BrightRed)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>Magenta</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Magenta)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>BrightMagenta</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.BrightMagenta)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>Blue</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Blue)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>BrightBlue</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.BrightBlue)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>Cyan</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Cyan)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>BrightCyan</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.BrightCyan)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>Green</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Green)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>BrightGreen</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.BrightGreen)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>Yellow</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Yellow)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>BrightYellow</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.BrightYellow)
</ScriptBlock>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
<View>
<Name>PSTree.Style.TreeStyle</Name>
<ViewSelectedBy>
<TypeName>PSTree.Style.TreeStyle</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<Label>OutputRendering</Label>
<PropertyName>OutputRendering</PropertyName>
</ListItem>
<ListItem>
<Label>Directory</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Directory)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>Executable</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Executable)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>Reset</Label>
<ScriptBlock>
[PSTree.Style.TreeStyle]::Instance.EscapeSequence($_.Reset)
</ScriptBlock>
</ListItem>
<ListItem>
<Label>Palette</Label>
<PropertyName>Palette</PropertyName>
</ListItem>
<ListItem>
<Label>Extension</Label>
<PropertyName>Extension</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
<View>
<Name>PSTree.Style.Extension</Name>
<ViewSelectedBy>
<TypeName>PSTree.Style.Extension</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Extension</Label>
<Width>12</Width>
</TableColumnHeader>
<TableColumnHeader>
<Label>Style</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<Wrap>true</Wrap>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>$_.Keys -join [Environment]::NewLine</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>[PSTree.Style.Extension]::GetEscapedValues($_)</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
12 changes: 9 additions & 3 deletions 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.18'
ModuleVersion = '2.2.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -71,13 +71,19 @@
FunctionsToExport = @()

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @('Get-PSTree')
CmdletsToExport = @(
'Get-PSTree'
'Get-PSTreeStyle'
)

# Variables to export from this module
VariablesToExport = @()

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @('pstree')
AliasesToExport = @(
'pstree'
'treestyle'
)

# DSC resources to export from this module
# DscResourcesToExport = @()
Expand Down
Loading

0 comments on commit 29cab84

Please sign in to comment.