Skip to content

Instantly share code, notes, and snippets.

@lipkau
Last active February 24, 2016 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lipkau/4f27b2da55875402f7df to your computer and use it in GitHub Desktop.
Save lipkau/4f27b2da55875402f7df to your computer and use it in GitHub Desktop.
[PowerShell] ProxyFunction: Pivot Format-Wide output
function Format-Wide {
[CmdletBinding(HelpUri='http://go.microsoft.com/fwlink/?LinkID=113304')]
param(
[Parameter(
Position=0
)]
[System.Object]${Property},
[switch]${AutoSize},
[ValidateRange(1, 2147483647)]
[int]${Column},
[System.Object]${GroupBy},
[string]${View},
[switch]${ShowError},
[switch]${DisplayError},
[switch]${Force},
[ValidateSet(
'CoreOnly',
'EnumOnly',
'Both'
)]
[string]${Expand},
[Parameter(
ValueFromPipeline=$true
)]
[psobject]${InputObject},
# Flip the order of the output so it's ordered by columns instead of rows
[Alias(
"Pivot",
"Vertically"
)]
[switch]$OrderVertically
)
begin
{
function Pivot-Wide {
#.Synopsis
# Pivot the output of Format-Wide
#.Example
# Get-Verb | Sort Verb | Format-Wide -Auto | Pivot-Wide
[CmdletBinding()]
param (
[Parameter(
ValueFromPipeline=$true
)]
$InputObject
)
begin
{
$cache = @()
}
process
{
switch ($InputObject.GetType().Name)
{
"FormatStartData" {
$columns = $InputObject.shapeInfo.columns
$InputObject
Write-Verbose $columns
}
"FormatEntryData" {
$cache += $InputObject
}
"GroupEndData" {
$count = $cache.Count
if($columns -eq 0)
{
$width = ($cache | % {
$_.formatEntryInfo.formatPropertyField.propertyvalue.length
} | measure -Maximum).Maximum + 2
$cols = [Math]::Min([Math]::Floor( $Host.UI.RawUI.BufferSize.Width / $width ),10)
}
else
{
$cols = $columns
}
$rows = [Math]::Ceiling( $count / $cols )
Write-Verbose "$count items in $cols columns means $rows rows, with a max width of $width"
for ($cRow=0; $cRow -lt $rows; $cRow++)
{
for ($c=0; $c -lt $cols; $c++)
{
$i = ($rows * $c) + $cRow
if($i -lt $count)
{
Write-Verbose "Cell ($i)"
$cache[$i]
}
else
{
Write-Verbose "Blank ($i)"
$cache[0].formatEntryInfo.formatPropertyField.propertyvalue = ""
$cache[0]
}
}
}
$cache = @()
$InputObject
}
default { $InputObject }
}
}
}
try
{
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Format-Wide', [System.Management.Automation.CommandTypes]::Cmdlet)
if ($OrderVertically)
{
$parameters = @{} + $PSBoundParameters
$null = $parameters.Remove("OrderVertically")
$scriptCmd = {& $wrappedCmd @parameters | Pivot-Wide -verbose}
}
else
{
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
}
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
}
catch
{
throw
}
}
process
{
try
{
$steppablePipeline.Process($_)
}
catch
{
throw
}
}
end
{
try
{
$steppablePipeline.End()
}
catch
{
throw
}
}
<#
.ForwardHelpTargetName Microsoft.PowerShell.Utility\Format-Wide
.ForwardHelpCategory Cmdlet
#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment