OK, it’s not fine and tuned and it uses regex for simple task, and it’s slow, BUT – it works! 🙂
Most of time, of course: as long as your column names are left-aligned. And it also kills pipe, so it has to be improved! 😉
Results first:
Nice, colourful output. You decide which colours to use, how many of them will be displayed, in which order. It is all up to you! 🙂
Code:
PowerShell, using GeSHi 1.0.8.8
-
function out–colour {
-
[int[]]$columns = @()
-
# select colours you prefer and the one that a readable on your console.. 🙂
-
$colours = @(‘Magenta’,‘Yellow’,‘Cyan’,‘Green’)
-
if ($columns.count -eq 0) {
-
$match = $obj | Select-String –AllMatches -Pattern ‘[^|s]-{2,}’ | Select-Object -ExpandProperty Matches
-
if ( ($match | Measure-Object | Select-Object -ExpandProperty Count) -ge 2) {
-
$columns = $match | Select-Object -ExpandProperty Index
-
}
-
Write-Host $obj
-
} else {
-
foreach ($char in $obj.ToCharArray()) {
-
$colour = $null
-
for ($i=0; $i -lt $columns.count – 1; $i++) {
-
Write-Verbose "i = $i ; X = $X"
-
if ( ( $X -ge $columns[$i]) -and ( $X -lt $columns[$i+1] ) ) {
-
$colour = $colours[($i % $colours.count)]
-
}
-
}
-
if (!$colour) {
-
$colour = $colours[(($columns.count –1) % $colours.count)]
-
}
-
Write-Host -ForegroundColor $colour $char -NoNewline
-
}
-
"`r"
-
}
-
}
-
}
-
}
It was fun to write it, and fun to play with it! 🙂 PowerShell is Ring of Power for sure! Wonder if I become a Nazgul one day… 😉
Hi,
Very nice script, but is does not work with grouped output, like from Get-ChildItem -Recurse. It will colour the headers for the next group as well.
(Speed could be better as well)
Remco