Skip to content

Commit 5ee4888

Browse files
authored
Add Show-ObjectTree cmdlet (#179)
* Add Show-Object * Rename Show-ObjectTree * Sort members alphabetically and add statusbar * Support for expanding IEnumerables * Disable ExpandAll This prevents Ctrl+Right causing infinite loop where a property references a parent. * Add window title with quit shortcut hint and fix NRT warning * Add search to Show-ObjectTree * Fix format/environment error strings Now correctly refers to ObjectTree not GridView * Add bracers to else block * Add command name as tag * Remove Ctrl+Q prompt and use Esc for exit * Adjust look and feel to match grid view * Add Filter, MinUI and Title options and started docs * Change Filter to match ocgv (regex support with error box) * Remove invalid examples and docs on output types to show tree * Add prototype implementation of directory contents listing * Don't output full path names of child objects * Adjust logic to be more readable * Fix class name * Add alias 'sot' for Show-ObjectTree * Add sot to `AliasesToExport` * Add UseNetDriver and Debug flags to Show-ObjectTree
1 parent 4714dd4 commit 5ee4888

File tree

4 files changed

+757
-3
lines changed

4 files changed

+757
-3
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
external help file: ConsoleGuiToolsModule.dll-Help.xml
3+
keywords: powershell,cmdlet
4+
locale: en-us
5+
Module Name: Microsoft.PowerShell.ConsoleGuiTools
6+
ms.date: 07/20/2023
7+
schema: 2.0.0
8+
title: Show-ObjectTree
9+
---
10+
11+
# Show-ObjectTree
12+
13+
## SYNOPSIS
14+
15+
Sends output to an interactive tree in the same console window.
16+
17+
## SYNTAX
18+
19+
```PowerShell
20+
Show-ObjectTree [-InputObject <psobject>] [-Title <string>] [-OutputMode {None | Single |
21+
Multiple}] [-Filter <string>] [-MinUi] [<CommonParameters>]
22+
```
23+
24+
## DESCRIPTION
25+
26+
The **Show-ObjectTree** cmdlet sends the output from a command to a tree view window where the output is displayed in an interactive tree.
27+
28+
You can use the following features of the tree to examine your data:
29+
30+
- Quick Filter. Use the Filter box at the top of the window to search the text in the tree. You can search for literals or multiple words. You can use the `-Filter` command to pre-populate the Filter box. The filter uses regular expressions.
31+
32+
For instructions for using these features, type `Get-Help Show-ObjectTree -Full` and see How to Use the Tree View Window Features in the Notes section.
33+
34+
## EXAMPLES
35+
36+
### Example 1: Output processes to a tree view
37+
38+
```PowerShell
39+
PS C:\> Get-Process | Show-ObjectTree
40+
```
41+
42+
This command gets the processes running on the local computer and sends them to a tree view window.
43+
44+
### Example 2: Save output to a variable, and then output a tree view
45+
46+
```PowerShell
47+
PS C:\> ($A = Get-ChildItem -Path $pshome -Recurse) | sot
48+
```
49+
50+
This command saves its output in a variable and sends it to **Show-ObjectTree**.
51+
52+
The command uses the Get-ChildItem cmdlet to get the files in the Windows PowerShell installation directory and its subdirectories.
53+
The path to the installation directory is saved in the $pshome automatic variable.
54+
55+
The command uses the assignment operator (=) to save the output in the $A variable and the pipeline operator (|) to send the output to **Show-ObjectTree**.
56+
57+
The parentheses in the command establish the order of operations.
58+
As a result, the output from the Get-ChildItem command is saved in the $A variable before it is sent to **Show-ObjectTree**.
59+
60+
## PARAMETERS
61+
62+
### -Filter
63+
Pre-populates the Filter edit box, allowing filtering to be specified on the command line.
64+
65+
```yaml
66+
Type: String
67+
Parameter Sets: (All)
68+
Aliases:
69+
70+
Required: False
71+
Position: Named
72+
Default value: None
73+
Accept pipeline input: False
74+
Accept wildcard characters: False
75+
```
76+
77+
### -InputObject
78+
Specifies that the cmdlet accepts input for **Show-ObjectTree**.
79+
80+
When you use the **InputObject** parameter to send a collection of objects to **Show-ObjectTree**, **Show-ObjectTree** treats the collection as one collection object, and it displays one row that represents the collection.
81+
82+
To display the each object in the collection, use a pipeline operator (|) to send objects to **Show-ObjectTree**.
83+
84+
```yaml
85+
Type: PSObject
86+
Parameter Sets: (All)
87+
Aliases:
88+
89+
Required: False
90+
Position: Named
91+
Default value: None
92+
Accept pipeline input: True (ByValue)
93+
Accept wildcard characters: False
94+
```
95+
96+
### -Title
97+
Specifies the text that appears in the title bar of the **Show-ObjectTree** window.
98+
99+
By default, the title bar displays the command that invokes **Show-ObjectTree**.
100+
101+
```yaml
102+
Type: String
103+
Parameter Sets: (All)
104+
Aliases:
105+
106+
Required: False
107+
Position: Named
108+
Default value: None
109+
Accept pipeline input: False
110+
Accept wildcard characters: False
111+
```
112+
113+
### -MinUi
114+
If specified no window frame, filter box, or status bar will be displayed in the **Show-ObjectTree** window.
115+
116+
```yaml
117+
Type: SwitchParameter
118+
Parameter Sets: (All)
119+
Aliases:
120+
121+
Required: False
122+
Position: Named
123+
Default value: None
124+
Accept pipeline input: False
125+
Accept wildcard characters: False
126+
```
127+
128+
### CommonParameters
129+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
130+
131+
## INPUTS
132+
133+
### System.Management.Automation.PSObject
134+
135+
You can send any object to this cmdlet.
136+
137+
## OUTPUTS
138+
139+
### None
140+
141+
`Show-ObjectTree` does not output any objects.
142+
143+
## NOTES
144+
145+
* The command output that you send to **Show-ObjectTree** should not be formatted, such as by using the Format-Table or Format-Wide cmdlets. To select properties, use the Select-Object cmdlet.
146+
147+
* Deserialized output from remote commands might not be formatted correctly in the tree view window.
148+
149+
## RELATED LINKS
150+
151+
[Out-File](Out-File.md)
152+
153+
[Out-Printer](Out-Printer.md)
154+
155+
[Out-String](Out-String.md)

src/Microsoft.PowerShell.ConsoleGuiTools/Microsoft.PowerShell.ConsoleGuiTools.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ NestedModules = @()
7070
FunctionsToExport = @()
7171

7272
# 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.
73-
CmdletsToExport = @( 'Out-ConsoleGridView' )
73+
CmdletsToExport = @( 'Out-ConsoleGridView', 'Show-ObjectTree' )
7474

7575
# Variables to export from this module
7676
VariablesToExport = '*'
7777

7878
# 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.
79-
AliasesToExport = @( 'ocgv' )
79+
AliasesToExport = @( 'ocgv', 'sot' )
8080

8181
# DSC resources to export from this module
8282
# DscResourcesToExport = @()
@@ -93,7 +93,7 @@ PrivateData = @{
9393
PSData = @{
9494

9595
# Tags applied to this module. These help with module discovery in online galleries.
96-
Tags = @('Console', 'Gui', 'Out-GridView', 'Out-ConsoleGridView', 'Terminal.Gui', 'gui.cs', 'MacOS', 'Windows', 'Linux', 'PSEdition_Core')
96+
Tags = @('Console', 'Gui', 'Out-GridView', 'Out-ConsoleGridView', 'Show-ObjectTree', 'Terminal.Gui', 'gui.cs', 'MacOS', 'Windows', 'Linux', 'PSEdition_Core')
9797

9898
# A URL to the license for this module.
9999
LicenseUri = 'https://github.com/PowerShell/GraphicalTools/blob/master/LICENSE.txt'
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.Management.Automation;
8+
using System.Management.Automation.Internal;
9+
using OutGridView.Models;
10+
11+
namespace OutGridView.Cmdlet
12+
{
13+
[Cmdlet("Show", "ObjectTree")]
14+
[Alias("sot")]
15+
public class ShowObjectTreeCmdletCommand : PSCmdlet, IDisposable
16+
{
17+
#region Properties
18+
19+
private const string DataNotQualifiedForShowObjectTree = nameof(DataNotQualifiedForShowObjectTree);
20+
private const string EnvironmentNotSupportedForShowObjectTree = nameof(EnvironmentNotSupportedForShowObjectTree);
21+
22+
private List<PSObject> _psObjects = new List<PSObject>();
23+
24+
#endregion Properties
25+
26+
#region Input Parameters
27+
28+
/// <summary>
29+
/// This parameter specifies the current pipeline object.
30+
/// </summary>
31+
[Parameter(ValueFromPipeline = true, HelpMessage = "Specifies the input pipeline object")]
32+
public PSObject InputObject { get; set; } = AutomationNull.Value;
33+
34+
/// <summary>
35+
/// Gets/sets the title of the Out-GridView window.
36+
/// </summary>
37+
[Parameter(HelpMessage = "Specifies the text that appears in the title bar of the Out-ConsoleGridView window. y default, the title bar displays the command that invokes Out-ConsoleGridView.")]
38+
[ValidateNotNullOrEmpty]
39+
public string Title { get; set; }
40+
41+
/// <summary>
42+
/// gets or sets the initial value for the filter in the GUI
43+
/// </summary>
44+
[Parameter(HelpMessage = "Pre-populates the Filter edit box, allowing filtering to be specified on the command line. The filter uses regular expressions." )]
45+
public string Filter { set; get; }
46+
47+
/// <summary>
48+
/// gets or sets the whether "minimum UI" mode will be enabled
49+
/// </summary>
50+
[Parameter(HelpMessage = "If specified no window frame, filter box, or status bar will be displayed in the GUI.")]
51+
public SwitchParameter MinUI { set; get; }
52+
/// <summary>
53+
/// gets or sets the whether the Terminal.Gui System.Net.Console-based ConsoleDriver will be used instead of the
54+
/// default platform-specific (Windows or Curses) ConsoleDriver.
55+
/// </summary>
56+
[Parameter(HelpMessage = "If specified the Terminal.Gui System.Net.Console-based ConsoleDriver (NetDriver) will be used.")]
57+
public SwitchParameter UseNetDriver { set; get; }
58+
59+
/// <summary>
60+
/// For the -Debug switch
61+
/// </summary>
62+
public bool Debug => MyInvocation.BoundParameters.TryGetValue("Debug", out var o);
63+
64+
#endregion Input Parameters
65+
66+
// This method gets called once for each cmdlet in the pipeline when the pipeline starts executing
67+
protected override void BeginProcessing()
68+
{
69+
if (Console.IsInputRedirected)
70+
{
71+
ErrorRecord error = new ErrorRecord(
72+
new PSNotSupportedException("Not supported in this environment (when input is redirected)."),
73+
EnvironmentNotSupportedForShowObjectTree,
74+
ErrorCategory.NotImplemented,
75+
null);
76+
77+
ThrowTerminatingError(error);
78+
}
79+
}
80+
81+
// This method will be called for each input received from the pipeline to this cmdlet; if no input is received, this method is not called
82+
protected override void ProcessRecord()
83+
{
84+
if (InputObject == null || InputObject == AutomationNull.Value)
85+
{
86+
return;
87+
}
88+
89+
if (InputObject.BaseObject is IDictionary dictionary)
90+
{
91+
// Dictionaries should be enumerated through because the pipeline does not enumerate through them.
92+
foreach (DictionaryEntry entry in dictionary)
93+
{
94+
ProcessObject(PSObject.AsPSObject(entry));
95+
}
96+
}
97+
else
98+
{
99+
ProcessObject(InputObject);
100+
}
101+
}
102+
103+
private void ProcessObject(PSObject input)
104+
{
105+
106+
object baseObject = input.BaseObject;
107+
108+
// Throw a terminating error for types that are not supported.
109+
if (baseObject is ScriptBlock ||
110+
baseObject is SwitchParameter ||
111+
baseObject is PSReference ||
112+
baseObject is PSObject)
113+
{
114+
ErrorRecord error = new ErrorRecord(
115+
new FormatException("Invalid data type for Show-ObjectTree"),
116+
DataNotQualifiedForShowObjectTree,
117+
ErrorCategory.InvalidType,
118+
null);
119+
120+
ThrowTerminatingError(error);
121+
}
122+
123+
_psObjects.Add(input);
124+
}
125+
126+
// This method will be called once at the end of pipeline execution; if no input is received, this method is not called
127+
protected override void EndProcessing()
128+
{
129+
base.EndProcessing();
130+
131+
//Return if no objects
132+
if (_psObjects.Count == 0)
133+
{
134+
return;
135+
}
136+
137+
var applicationData = new ApplicationData
138+
{
139+
Title = Title ?? "Show-ObjectTree",
140+
Filter = Filter,
141+
MinUI = MinUI,
142+
UseNetDriver = UseNetDriver,
143+
Debug = Debug,
144+
ModuleVersion = MyInvocation.MyCommand.Version.ToString()
145+
};
146+
147+
ShowObjectView.Run(_psObjects, applicationData);
148+
}
149+
150+
public void Dispose()
151+
{
152+
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)