# encoding: ascii
# api: powershell
# title: Colorize Subversion SVN
# description: Colorize STAT, UPDATE and DIFF (without params) commands output for Subversion (svn) and Mercurial (hg).
# version: 0.1
# type: function
# author: Bishop
# license: CC0
# x-poshcode-id: 1148
# x-archived: 2009-06-09T10:58:57
#
# Here’s a PowerShell functions that you can use to make those numerous commands you run every day via the PowerShell CLI a little easier to read by adding colors.
# Autodetect for svn or hg.
#
# detect source control management software
function findscm {
	$scm = ''
	:selectscm foreach ($_ in @('svn', 'hg')) {
		$dir = (Get-Location).Path
		while ($dir.Length -gt 3) {
			if (Test-Path ([IO.Path]::combine($dir, ".$_"))) {
				$scm = $_
				break selectscm
			}
			$dir = $dir -Replace '\\[^\\]+$', ''
		}
	}
	return $scm
}

# draw output
function drawlines($colors, $cmd) {
	$scm = findscm
	if (!$cmd -or !$scm) { return }
	foreach ($line in (&$scm $cmd)) {
		$color = $colors[[string]$line[0]]
		if ($color) {
			write-host $line -Fore $color
		} else {
			write-host $line
		}
	}
}

# svn stat
function st {
	drawlines @{ "A"="Magenta"; "D"="Red"; "C"="Yellow"; "G"="Blue"; "M"="Cyan"; "U"="Green"; "?"="DarkGray"; "!"="DarkRed" } "stat"
}

# svn update
function su {
	drawlines @{ "A"="Magenta"; "D"="Red"; "U"="Green"; "C"="Yellow"; "G"="Blue"; } "up"
}

# svn diff
function sd {
	drawlines @{ "@"="Magenta"; "-"="Red"; "+"="Green"; "="="DarkGray"; } "diff"
}
