Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active May 29, 2022 03:40
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 Jaykul/79b0f6f0ee44e06be56d2bd298ab084c to your computer and use it in GitHub Desktop.
Save Jaykul/79b0f6f0ee44e06be56d2bd298ab084c to your computer and use it in GitHub Desktop.
The most items, in the smallest space. And it works with strings.

Some experiments to see if we can make Format-Wide more dense. I made it handle strings, first...

And then built in colors, so it can show long lines wrapped. It just alternates colors and supports adding a little -Padding. By default there's one space between strings, but you can set it to 0, or to 2 or whatever you like.

If you pass one or more colors to the -RgbColor parameter like 0x336699, it automatically alternates colors and wraps lines, instead of doing columns -- unless you explicitly specify -Column or -AutoWrap.

Currently it's not totally compatible with the built-in Format-Wide, but it should seem compatible.

image

<#
.SYNOPSIS
Instead of columns, use alternating colors.
#>
[CmdletBinding(HelpUri = 'https://go.microsoft.com/fwlink/?LinkID=2096930')]
param(
[Parameter(Position = 0)]
[System.Object]
${Property},
[switch]
${AutoSize},
[ValidateRange(0, 2147483647)]
[int]
${Padding} = 1,
[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},
[Array]$RgbColor
)
begin {
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 ($RgbColor.Count -lt 1) {
$RgbColor = @("", "")
} else {
$RgbColor = foreach ($C in @($RgbColor)) {
if ($C.Length -gt ($C -replace "\u001B.*?\p{L}").Length) {
$C
} elseif ($PSStyle.Foreground."$C") {
$PSStyle.Foreground."$C"
} else {
try {
$PSStyle.Foreground.FromRgb($C)
} catch {
([PoshCode.Pansies.RgbColor]$C).ToVtEscapeSequence()
}
}
}
if ($RgbColor.Count -lt 2) {
$RgbColor += "$([char]27)[1;37m"
}
}
$Wrap = $PSBoundParameters.Remove("RgbColor")
$Wrap = $PSBoundParameters.Remove("Padding") -or $Wrap
$scriptCmd = {
& $wrappedCmd @PSBoundParameters |
ForEach-Object { $c = 0 } {
$Item = $_
if (-not ($Item.PSTypeNames -match "FormatEntryData")) {
Write-Host "$([char]27)[0m" -NoNewline
$Item
} elseif (!$Item.formatEntryInfo.formatPropertyField.propertyValue) {
$Item
} elseif ($Column -or $AutoSize -or !$Wrap) {
$Item.formatEntryInfo.formatPropertyField.propertyValue = "$($RgbColor[($c++) % $RgbColor.Count])$($Item.formatEntryInfo.formatPropertyField.propertyValue)"
$Item
} else {
Write-Host "$($RgbColor[($c++) % $RgbColor.Count])$($Item.formatEntryInfo.formatPropertyField.propertyValue)$(" " * $Padding)" -NoNewline
}
} { $PSStyle.Reset }
}
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}
process {
try {
$steppablePipeline.Process($(if ($_ -is [string]) { [PSCustomObject]@{ String = $_ } } else { $_ }))
} 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