PoshCode Archive  Artifact [ede387ab72]

Artifact ede387ab729ca2463019a881d67193fd23ae7e107f450fe82683a274f11255c4:

  • File Out-Colour.ps1 — part of check-in [1a7793ab12] at 2018-06-10 13:03:19 on branch trunk — This function will, when used at the end of pipe, produce colourful output for any format-table’d data. It has some bugs in it (problem with columns which name is right-aligned, kills pipe), but maybe someone will pick up from here and fix those. ;) (user: unknown size: 1636)

# encoding: ascii
# api: powershell
# title: Out-Colour
# description: This function will, when used at the end of pipe, produce colourful output for any format-table’d data. It has some bugs in it (problem with columns which name is right-aligned, kills pipe), but maybe someone will pick up from here and fix those. ;)
# version: 0.1
# type: function
# license: CC0
# function: out-colour
# x-poshcode-id: 1954
# x-archived: 2010-07-11T17:23:09
#
#
function out-colour {
    	if ($Input) {
		[int[]]$columns = @()
		# select colors you prefer and the one that a readable on your console.. :)
		$colours = @('Magenta','Yellow','Cyan','Green')
        	foreach ($obj in ($Input | Out-String -Stream)) {
			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
					$X = $Host.UI.RawUI.CursorPosition.X
					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"
			}
        	}
    	}
}