PoshCode Archive  Artifact [4386994b1d]

Artifact 4386994b1d4c8f4160fd1df4419cbab2ce59f6969ecd2645ea6dde8ca835ab59:

  • File Colorize-Subversion-SVN.ps1 — part of check-in [7129d9ca7e] at 2018-06-10 12:56:32 on branch trunk — Colorize STAT, UPDATE and DIFF (without params) commands output for Subversion (svn) and Mercurial (hg). (user: Bishop size: 1539)

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