# 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"
			}
        	}
    	}
}
