Skip to content

Commit 8e64d34

Browse files
author
Friedrich Weinmann
committed
Tab Expansion for Select-Object
1 parent 9f1e492 commit 8e64d34

File tree

3 files changed

+175
-2
lines changed

3 files changed

+175
-2
lines changed

PSUtil/changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
2-
## Version 2.0.*
2+
## Version 2.0.3 (2018-12-23)
33
- New: Configuration setting 'PSUtil.Import.Alias.SystemOverride'. Persisting this will have PSUtil replace system default aliases.
44
- New: Tab Expansion for PowerShellGet
5+
- New: Tab Expansion for Select-Object and Select-PSFObject
56

67
## Version 2.0.0 (2018-12-15)
78
- new: Command Get-PSUPathAlias lists all current path aliases

PSUtil/internal/TEPP/assignment.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ Register-PSFTeppArgumentCompleter -Command Save-Script -Parameter Repository -Na
2222
Register-PSFTeppArgumentCompleter -Command Get-PSRepository -Parameter Name -Name 'PSUtil-Module-Repository'
2323
Register-PSFTeppArgumentCompleter -Command Unregister-PSRepository -Parameter Name -Name 'PSUtil-Module-Repository'
2424
Register-PSFTeppArgumentCompleter -Command Register-PSRepository -Parameter PackageManagementProvider -Name 'PSUtil-Module-PackageProvider'
25-
#endregion Module
25+
#endregion Module
26+
27+
#region Select
28+
Register-PSFTeppArgumentCompleter -Command Select-PSFObject -Parameter Property -Name PSUtil-Input-Object
29+
Register-PSFTeppArgumentCompleter -Command Select-PSFObject -Parameter ExpandProperty -Name PSUtil-Input-Object
30+
Register-PSFTeppArgumentCompleter -Command Select-PSFObject -Parameter ExcludeProperty -Name PSUtil-Input-Object
31+
Register-PSFTeppArgumentCompleter -Command Select-PSFObject -Parameter ShowProperty -Name PSUtil-Input-Object
32+
Register-PSFTeppArgumentCompleter -Command Select-PSFObject -Parameter ShowExcludeProperty -Name PSUtil-Input-Object
33+
34+
Register-PSFTeppArgumentCompleter -Command Select-Object -Parameter Property -Name PSUtil-Input-Object
35+
Register-PSFTeppArgumentCompleter -Command Select-Object -Parameter ExpandProperty -Name PSUtil-Input-Object
36+
Register-PSFTeppArgumentCompleter -Command Select-Object -Parameter ExcludeProperty -Name PSUtil-Input-Object
37+
#endregion Select
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
Register-PSFTeppScriptblock -Name PSUtil-Input-Object -ScriptBlock {
2+
[System.Management.Automation.Language.PipelineAst]$pipelineAst = $commandAst.parent
3+
$index = $pipelineAst.PipelineElements.IndexOf($commandAst)
4+
5+
#region If it's the first command
6+
if ($index -lt 1)
7+
{
8+
return
9+
}
10+
#endregion If it's the first command
11+
12+
$properties = @()
13+
$constraintsPositive = @()
14+
15+
#region Process pre-commands
16+
$inputIndex = $index - 1
17+
:main while ($true)
18+
{
19+
if ($pipelineAst.PipelineElements[$inputIndex].CommandElements)
20+
{
21+
# Resolve command and fail if it breaks
22+
$command = Get-Command $pipelineAst.PipelineElements[$inputIndex].CommandElements[0].Value -ErrorAction Ignore
23+
if ($command -is [System.Management.Automation.AliasInfo]) { $command = $command.ResolvedCommand }
24+
if (-not $command) { break }
25+
26+
switch ($command.Name)
27+
{
28+
'Where-Object' { $inputIndex = $inputIndex - 1; continue main }
29+
'Tee-Object' { $inputIndex = $inputIndex - 1; continue main }
30+
#region Select-Object
31+
'Select-Object'
32+
{
33+
$firstAst = $pipelineAst.PipelineElements[$inputIndex].CommandElements | Where-Object { $_ -is [System.Management.Automation.Language.ArrayLiteralAst] } | Select-Object -First 1
34+
35+
foreach ($element in $firstAst.Elements)
36+
{
37+
switch ($element.GetType().FullName)
38+
{
39+
'System.Management.Automation.Language.StringConstantExpressionAst'
40+
{
41+
$constraintsPositive += $element.Value
42+
if ($element.Value -notmatch "\*") { $properties += $element.Value }
43+
}
44+
'System.Management.Automation.Language.HashtableAst'
45+
{
46+
$constraintsPositive += ($element.KeyValuePairs | Where-Object Item1 -Match '^N$|^Name$' | Select-Object -First 1).Item2.ToString().Trim('"')
47+
$properties += ($element.KeyValuePairs | Where-Object Item1 -Match '^N$|^Name$' | Select-Object -First 1).Item2.ToString().Trim('"')
48+
}
49+
}
50+
}
51+
$inputIndex = $inputIndex - 1;
52+
continue main
53+
}
54+
#endregion Select-Object
55+
#region Select-PSFObject
56+
'Select-PSFObject'
57+
{
58+
$firstAst = $pipelineAst.PipelineElements[$inputIndex].CommandElements | Where-Object { $_ -is [System.Management.Automation.Language.ArrayLiteralAst] } | Select-Object -First 1
59+
foreach ($element in $firstAst.Elements)
60+
{
61+
switch ($element.GetType().FullName)
62+
{
63+
"System.Management.Automation.Language.StringConstantExpressionAst"
64+
{
65+
$par = [PSFramework.Parameter.SelectParameter]$element.Value
66+
if ($par.Value -match "\*") { $constraintsPositive += $par.Value }
67+
else
68+
{
69+
if ($par.Value -is [System.String])
70+
{
71+
$properties += $par.Value
72+
$constraintsPositive += $par.Value
73+
}
74+
else
75+
{
76+
$properties += $par.Value["Name"]
77+
$constraintsPositive += $par.Value["Name"]
78+
}
79+
}
80+
}
81+
"System.Management.Automation.Language.HashtableAst"
82+
{
83+
$properties += ($element.KeyValuePairs | Where-Object Item1 -Match '^N$|^Name$' | Select-Object -First 1).Item2.ToString().Trim('"')
84+
$constraintsPositive += ($element.KeyValuePairs | Where-Object Item1 -Match '^N$|^Name$' | Select-Object -First 1).Item2.ToString().Trim('"')
85+
}
86+
}
87+
}
88+
$inputIndex = $inputIndex - 1;
89+
}
90+
#endregion Select-PSFObject
91+
default { break main }
92+
}
93+
}
94+
95+
else
96+
{
97+
break
98+
}
99+
}
100+
101+
# Catch moving through _all_ options in the pipeline
102+
if ($inputIndex -lt 0) { return $properties }
103+
#endregion Process pre-commands
104+
105+
106+
#region Input from command
107+
if ($pipelineAst.PipelineElements[$inputIndex].CommandElements)
108+
{
109+
if ($command = Get-Command $pipelineAst.PipelineElements[$inputIndex].CommandElements[0].Value -ErrorAction Ignore)
110+
{
111+
switch ($command.Name)
112+
{
113+
#region Default for commands
114+
default
115+
{
116+
foreach ($type in $command.OutputType.Type)
117+
{
118+
switch ($type.GetType().FullName)
119+
{
120+
'System.IO.FileInfo'
121+
{
122+
$properties += ($type.GetMembers("Instance, Public") | Where-Object MemberType -match "Field|Property").Name
123+
$properties += 'PSChildName', 'PSDrive', 'PSIsContainer', 'PSParentPath', 'PSPath', 'PSProvider', 'BaseName'
124+
break
125+
}
126+
'System.IO.DirectoryInfo'
127+
{
128+
$properties += ($type.GetMembers("Instance, Public") | Where-Object MemberType -match "Field|Property").Name
129+
$properties += 'PSChildName', 'PSDrive', 'PSIsContainer', 'PSParentPath', 'PSPath', 'PSProvider', 'BaseName', 'VersionInfo'
130+
break
131+
}
132+
default { $properties += ($type.GetMembers("Instance, Public") | Where-Object MemberType -match "Field|Property").Name }
133+
}
134+
}
135+
}
136+
#endregion Default for commands
137+
}
138+
}
139+
}
140+
#endregion Input from command
141+
142+
#region Input from Variable
143+
if ($pipelineAst.PipelineElements[$inputIndex].Expression -and $pipelineAst.PipelineElements[0].Expression[0].VariablePath)
144+
{
145+
$properties += ((Get-Variable -Name $pipelineAst.PipelineElements[0].Expression[0].VariablePath.UserPath -ValueOnly) | Select-Object -First 1 | Get-Member -MemberType Properties).Name
146+
}
147+
#endregion Input from Variable
148+
149+
$properties | Select-Object -Unique | Sort-Object | ForEach-Object {
150+
if (-not $constraintsPositive) { $_ }
151+
foreach ($constraint in $constraintsPositive)
152+
{
153+
if ($_ -like $constraint)
154+
{
155+
$_
156+
break
157+
}
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)