Skip to content

Commit

Permalink
Merge pull request #242 from StartAutomating/More-Posh
Browse files Browse the repository at this point in the history
More posh
  • Loading branch information
StartAutomating committed Aug 27, 2023
2 parents 4958c67 + a165c6a commit 9b552c8
Show file tree
Hide file tree
Showing 24 changed files with 3,030 additions and 1,407 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/FeatureRequest.yml
@@ -1,6 +1,6 @@
name: Feature Request
description: Request a Feature
title: ""
title: "Request - "
labels: ["enhancement"]
body:
- type: markdown
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,17 @@
## Posh 0.1.6: Posh Reflection

* Added multiple custom views for System.Type (#216):
* Can now view 'Inheritance', 'Interfaces', 'Public', 'Private', 'Full'
* Private methods are highlighted in red.
* New Formatting:
* System.Version (#230, #231, #232)
* New Methods:
* Adding Posh.Host.Choose (#131)
* Improving formatting for RSS Feeds (#238)
* Adding PowerShell Podcast to .News/.Links (#228)

---

## Posh 0.1.5: Posh Issues and Discussions

* Adding PSModuleInfo.Discussion (Fixes #212)
Expand Down
4 changes: 2 additions & 2 deletions Demos/Posh.demo.ps1
Expand Up @@ -62,13 +62,13 @@ Get-Command | Get-Member

# To see _everything_ about an object, use something like

[int] | Format-Custom -View System.Type.Full
[int] | Format-Custom -View Public


# This also works for generic types.

[Collections.Generic.Dictionary[string, PSObject]] |
Format-Custom -View System.Type.Full
Format-Custom -View Public

#2.5 Colorized XML

Expand Down
24 changes: 9 additions & 15 deletions Formatting/Posh/Posh.LinkControl.format.ps1
Expand Up @@ -17,22 +17,16 @@ Write-FormatView -TypeName n/a -Name "Posh.Link" -AsControl -Action {
) -join ''
}

Write-FormatView -TypeName n/a -Name "Posh.Link.Line" -AsControl -Action {
Write-FormatView -TypeName n/a -Name "Posh.Link.Play" -AsControl -Action {
@(
if ($_.Name -and $_.Url) {
if ($psStyle.FormatHyperlink -and -not $env:GITHUB_WORKSPACE) {
$psStyle.FormatHyperlink($_.Name, $_.Url)
} else {
"[$($_.Name)]($($_.Url))"
}
} elseif ($_.Url) {
$uri = [uri]$_.Url
if ($psStyle.FormatHyperlink -and -not $env:GITHUB_WORKSPACE) {
$psStyle.FormatHyperlink($_.Url, $uri)
} else {
"[$($_.Url)]($($uri))"
}
if ($_.enclosure.url -and
$psStyle.FormatHyperlink -and -not $env:GITHUB_WORKSPACE) {
" $($psStyle.FormatHyperlink("", $_.enclosure.url)) "
}
[Environment]::NewLine
) -join ''
}

Write-FormatView -TypeName n/a -Name "Posh.Link.Line" -AsControl -Action {
Write-FormatViewExpression -ControlName Posh.Link -ScriptBlock { $_ }
Write-FormatViewExpression -Newline
}
7 changes: 6 additions & 1 deletion Formatting/Posh/Posh.RSS.Article.format.ps1
@@ -1,5 +1,10 @@
Write-FormatView -TypeName Posh.RSS.Article -Action {
Write-FormatViewExpression -ScriptBlock { $_ } -ControlName Posh.Link
Write-FormatViewExpression -ScriptBlock { $_ } -ControlName Posh.Link
Write-FormatViewExpression -If { $_.DatePublished -is [datetime] } -ScriptBlock {
" ($($_.DatePublished.ToShortDateString())) "
}
Write-FormatViewExpression -If { $_.Enclosure } -ScriptBlock { $_ } -ControlName Posh.Link.Play

} -GroupByProperty Source

Write-FormatView -TypeName Posh.RSS.Article -Property Title, Link
2 changes: 1 addition & 1 deletion Formatting/Posh/Posh.format.ps1
Expand Up @@ -2,7 +2,7 @@ Write-FormatView -TypeName Posh -Action {

Write-FormatViewExpression -Style "Foreground.Blue", "Bold" -Property Name
Write-FormatViewExpression -Style "Foreground.Cyan" -Text ' @ '
Write-FormatViewExpression -Style "Foreground.Cyan", "Bold" -Property Version
Write-FormatViewExpression -Property Version -ControlName VersionControl
Write-FormatViewExpression -Newline
Write-FormatViewExpression -Newline

Expand Down
135 changes: 85 additions & 50 deletions Formatting/Reflection/System.Type.format.ps1
@@ -1,64 +1,99 @@
Write-FormatView -TypeName System.Type -Property FullName, BaseType, IsPublic, IsSerializable -AutoSize

Write-FormatView -TypeName System.Type -Action {
Write-FormatView -TypeName n/a -AsControl -Name TypeInheritanceControl -Action {
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ScriptBlock { '-' * ($Host.UI.RawUI.BufferSize.Width - 1) }
Write-FormatViewExpression -ScriptBlock { ' ' * 1 }
Write-FormatViewExpression -ForegroundColor 'Verbose' -ScriptBlock { $_ } -ControlName TypeNameControl
Write-FormatViewExpression -ScriptBlock { ' ' * 1 }
Write-FormatViewExpression -Style 'Formatting.Verbose' -ScriptBlock { $_ } -ControlName TypeNameControl
Write-FormatViewExpression -ScriptBlock { ' ' * 1 }
Write-FormatViewExpression -If { $_.BaseType -and -not $_.IsValueType } -ScriptBlock {
':'
}
Write-FormatViewExpression -If { $_.BaseType -and -not $_.IsValueType -and $_.BaseType -ne [Object] } -Property BaseType -ControlName TypeBase
Write-FormatViewExpression -If { $_.GetInterfaces() } -ScriptBlock { $_.GetInterfaces() | Sort-Object Name} -Enumerate -ControlName TypeBase
} -GroupLabel 'Type Summary' -GroupByScript { '| Format-Custom -View System.Type.Full for more'} -Name System.Type.Summary

Write-FormatView -TypeName System.Type -Action {
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ScriptBlock { '-' * ($Host.UI.RawUI.BufferSize.Width - 1) }
Write-FormatViewExpression -ScriptBlock { ' ' * 1 }
Write-FormatViewExpression -ForegroundColor 'Verbose' -ScriptBlock { $_ } -ControlName TypeNameControl
Write-FormatViewExpression -ScriptBlock { ' ' * 1 }
Write-FormatViewExpression -If { $_.BaseType -and -not $_.IsValueType -and $_.BaseType -ne [Object] } -ScriptBlock {
':'
}
Write-FormatViewExpression -If { $_.BaseType -and -not $_.IsValueType -and $_.BaseType -ne [Object] } -Property BaseType -ControlName TypeBase
Write-FormatViewExpression -If { $_.GetInterfaces() } -ScriptBlock { $_.GetInterfaces() | Sort-Object Name} -Enumerate -ControlName TypeBase
Write-FormatViewExpression -ScriptBlock { '=' * ($Host.UI.RawUI.BufferSize.Width - 1) }
}

Write-FormatViewExpression -If { $_.GetConstructors('Instance,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Constructors:'
}
Write-FormatViewExpression -If { $_.GetConstructors('Instance,Public') } -ScriptBlock {
$_.GetConstructors('Instance,Public')
} -Enumerate -ControlName TypeMethodControl
Write-FormatViewExpression -If { $_.GetEvents('Instance,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Events:'
}
Write-FormatViewExpression -If { $_.GetEvents('Instance,Public') } -ScriptBlock {
$_.GetEvents('Instance,Public') | Sort-Object Name
} -Enumerate -ControlName TypeEventControl
Write-FormatViewExpression -If { $_.GetProperties('Static,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Static Properties:'

$TypeGrouping = [Ordered]@{
GroupByScript = {
"
... | Format-Custom -View Full # To show public and private members
... | Format-Custom -View Public # To show public members
... | Format-Custom -View Private # To show private members
... | Format-Custom -View Interfaces # To show interfaces
"
}
Write-FormatViewExpression -If { $_.GetProperties('Static,Public')} -ScriptBlock {
$_.GetProperties('Static,Public') | Sort-Object Name
} -Enumerate -ControlName TypePropertyControl
Write-FormatViewExpression -If { $_.GetProperties('Instance,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Properties:'
}

Write-FormatView -TypeName System.Type -Action {
Write-FormatViewExpression -ControlName TypeInheritanceControl -ScriptBlock { $_ }
} -GroupLabel 'Type Inheritance' @TypeGrouping -Name Inheritance

Write-FormatView -TypeName System.Type -Action {
Write-FormatViewExpression -ControlName TypeInheritanceControl -ScriptBlock { $_ }
Write-FormatViewExpression -If { $_.GetInterfaces() } -ScriptBlock { $_.GetInterfaces() | Sort-Object Name} -Enumerate -ControlName TypeInterfaceControl
} -GroupLabel 'Type Inheritance' @TypeGrouping -Name Interfaces

foreach ($viewName in 'Public','Private','Full') {
$bindingFlags =
switch ($viewName) {
Public {
'Public'
}
Private {
'NonPublic'
}
Full {
'Public,NonPublic'
}
}
$assignView = [ScriptBlock]::Create("
`$_ |
Add-Member NoteProperty '.View' '$viewName' -PassThru -Force |
Add-Member NoteProperty '.BindingFlags' ([Reflection.BindingFlags]'$bindingFlags') -Force
")

Write-FormatView -TypeName System.Type -Action {
Write-FormatViewExpression -If $assignView -ScriptBlock { "" }

Write-FormatViewExpression -ControlName TypeInheritanceControl -ScriptBlock { $_ }

Write-FormatViewExpression -If {
$_.GetInterfaces()
} -ScriptBlock {
$_.GetInterfaces() | Sort-Object Name
} -Enumerate -ControlName TypeInterfaceControl

Write-FormatViewExpression -ControlName TypeConstructorsControl -ScriptBlock {
if ($_.'.BindingFlags') {
@{Type=$_;BindingFlags="Instance,$($_.'.BindingFlags')"}
} else {
$_
}
}
Write-FormatViewExpression -If { $_.GetProperties('Instance,Public')} -ScriptBlock {
$_.GetProperties('Instance,Public') | Sort-Object Name
} -Enumerate -ControlName TypePropertyControl
Write-FormatViewExpression -If { $_.GetMethods('Static,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Static Methods:'

Write-FormatViewExpression -ControlName TypeEventsControl -ScriptBlock {
if ($_.'.BindingFlags') {
@{Type=$_;BindingFlags="Instance,$($_.'.BindingFlags')"}
} else {
$_
}
}
Write-FormatViewExpression -If { $_.GetMethods('Static,Public') } -ScriptBlock {
$_.GetMethods('Static,Public') | Sort-Object Name | Where-Object { -not $_.IsSpecialName }
} -Enumerate -ControlName TypeMethodControl
Write-FormatViewExpression -If { $_.GetMethods('Instance,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Methods:'

Write-FormatViewExpression -ControlName TypePropertiesControl -ScriptBlock {
if ($_.'.BindingFlags') {
@{Type=$_;BindingFlags="$($_.'.BindingFlags')"}
} else {
$_
}
}
Write-FormatViewExpression -If { $_.GetMethods('Instance,Public') } -ScriptBlock {
$_.GetMethods('Instance,Public') | Sort-Object Name | Where-Object { -not $_.IsSpecialName }
} -Enumerate -ControlName TypeMethodControl
} -Name System.Type.Full

Write-FormatViewExpression -ControlName TypeMethodsControl -ScriptBlock {
if ($_.'.BindingFlags') {
@{Type=$_;BindingFlags="$($_.'.BindingFlags')"}
} else {
$_
}
}
} -Name "$ViewName" -GroupLabel "Type $ViewName" @TypeGrouping
}
13 changes: 13 additions & 0 deletions Formatting/Reflection/TypeConstructorsControl.format.ps1
@@ -0,0 +1,13 @@
$getConstructors = {
if ($_ -is [Type]) {
$_.GetConstructors('Instance,Public')
} elseif ($_.Type -and $_.BindingFlags) {
$_.Type.GetConstructors($_.BindingFlags)
}
}
Write-FormatControl -Name TypeConstructorsControl -Action {
Write-FormatViewExpression -If $getConstructors -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Constructors:'
}
Write-FormatViewExpression -If $getConstructors -ScriptBlock $getConstructors -Enumerate -ControlName TypeMethodControl
}
14 changes: 14 additions & 0 deletions Formatting/Reflection/TypeEventsControl.format.ps1
@@ -0,0 +1,14 @@
$getEvents = {
if ($_ -is [Type]) {
$_.GetEvents('Instance,Public')
} elseif ($_.Type -and $_.BindingFlags) {
$_.Type.GetEvents($_.BindingFlags)
}
}

Write-FormatControl -Name TypeEventsControl -Action {
Write-FormatViewExpression -If $getEvents -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Events:'
}
Write-FormatViewExpression -If $getEvents -ScriptBlock $getEvents -Enumerate -ControlName TypeEventControl
}
5 changes: 5 additions & 0 deletions Formatting/Reflection/TypeInterfaceControl.format.ps1
@@ -0,0 +1,5 @@
Write-FormatView -Action {
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ScriptBlock { ' *' }
Write-FormatViewExpression -ControlName TypeNameControl -ScriptBlock {$_ } -Style 'Foreground.Cyan', 'Bold'
} -TypeName n/a -Name TypeInterfaceControl -AsControl
31 changes: 30 additions & 1 deletion Formatting/Reflection/TypeMethodControl.format.ps1
@@ -1,10 +1,39 @@
Write-FormatView -Action {
Write-FormatViewExpression -If { -not $script:DisplayingMember } -ScriptBlock { [Environment]::NewLine }

Write-FormatViewExpression -ScriptBlock { ' *' }

Write-FormatViewExpression -If { $_.IsStatic } -ScriptBlock { ' static ' }

Write-FormatViewExpression -If {$_.IsConstructor } -ScriptBlock { $_.DeclaringType } -ControlName TypeNameControl -Style 'Foreground.Cyan','Bold'
Write-FormatViewExpression -If { -not $_.IsConstructor -and $_.ReturnType } -ScriptBlock { $_.ReturnType } -ControlName TypeNameControl -Style 'Foreground.Cyan','Bold'
Write-FormatViewExpression -If { -not $_.IsConstructor } -ScriptBlock { ' ' + $_.Name } -Style 'Formatting.Warning'

Write-FormatViewExpression -ScriptBlock {
@(if (-not $_.IsConstructor) {
' '
if ($_.IsPublic) {
if ($PSStyle) {
$PSStyle.Formatting.Warning
}
}
elseif ($_.IsPrivate) {
if ($PSStyle) {
$PSStyle.Formatting.Error
}
}
else {
if ($PSStyle) {
$PSStyle.Formatting.Warning
}
}

$_.Name
if ($PSStyle) {
$PSStyle.Reset
}
}) -join ''
}

Write-FormatViewExpression -ScriptBlock { ' (' }
Write-FormatViewExpression -ScriptBlock {
$MethodParameters = @($_.GetParameters())
Expand Down
5 changes: 2 additions & 3 deletions Formatting/Reflection/TypeMethodParameterControl.format.ps1
@@ -1,6 +1,5 @@
Write-FormatView -Action {
Write-FormatViewExpression -If { $_.N -gt 0} -ScriptBlock { ', ' }
Write-FormatViewExpression -Property ParameterType -ControlName TypeNameControl -Style 'Formatting.Verbose'
Write-FormatViewExpression -ScriptBlock { '$' + $_.Name } -Style 'Formatting.Warning'
Write-FormatViewExpression -ScriptBlock { ' ' }
Write-FormatViewExpression -Property ParameterType -ControlName TypeNameControl -Style 'Foreground.Cyan','Bold'
Write-FormatViewExpression -ScriptBlock { '$' + $_.Name + ' ' } -Style 'Formatting.Warning' -If {$_.Name }
} -TypeName TypeMethodParameterControl -Name TypeMethodParameterControl -AsControl
26 changes: 26 additions & 0 deletions Formatting/Reflection/TypeMethodsControl.format.ps1
@@ -0,0 +1,26 @@
$getStaticMethods = {
if ($_ -is [Type]) {
$_.GetMethods('Static,Public')
} elseif ($_.Type -and $_.BindingFlags) {
$_.Type.GetMethods("Static,$($_.BindingFlags)")
}
}

$getInstanceMethods = {
if ($_ -is [Type]) {
$_.GetMethods('Instance,Public')
} elseif ($_.Type -and $_.BindingFlags) {
$_.Type.GetMethods("Instance,$($_.BindingFlags)")
}
}

Write-FormatControl -Name TypeMethodsControl -Action {
Write-FormatViewExpression -If $getStaticMethods -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Static Methods:'
}
Write-FormatViewExpression -If $getStaticMethods -ScriptBlock $getStaticMethods -Enumerate -ControlName TypeMethodControl
Write-FormatViewExpression -If $getInstanceMethods -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Methods:'
}
Write-FormatViewExpression -If $getInstanceMethods -ScriptBlock $getInstanceMethods -Enumerate -ControlName TypeMethodControl
}
26 changes: 26 additions & 0 deletions Formatting/Reflection/TypePropertiesControl.format.ps1
@@ -0,0 +1,26 @@
$getStaticProperties = {
if ($_ -is [Type]) {
$_.GetProperties('Static,Public')
} elseif ($_.Type -and $_.BindingFlags) {
$_.Type.GetProperties("Static,$($_.BindingFlags)")
}
}

$getInstanceProperties = {
if ($_ -is [Type]) {
$_.GetProperties('Instance,Public')
} elseif ($_.Type -and $_.BindingFlags) {
$_.Type.GetProperties("Instance,$($_.BindingFlags)")
}
}

Write-FormatControl -Name TypePropertiesControl -Action {
Write-FormatViewExpression -If $getStaticProperties -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Static Properties:'
}
Write-FormatViewExpression -If $getStaticProperties -ScriptBlock $getStaticProperties -Enumerate -ControlName TypePropertyControl
Write-FormatViewExpression -If $getInstanceProperties -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Properties:'
}
Write-FormatViewExpression -If $getInstanceProperties -ScriptBlock $getInstanceProperties -Enumerate -ControlName TypePropertyControl
}
3 changes: 3 additions & 0 deletions Formatting/System.Version.format.ps1
@@ -0,0 +1,3 @@
Write-FormatView -TypeName System.Version -Action {
Write-FormatViewExpression -ScriptBlock { $_ } -ControlName VersionControl
}

0 comments on commit 9b552c8

Please sign in to comment.