Skip to content

Instantly share code, notes, and snippets.

@espoelstra
Forked from ecampidoglio/Out-Diff.ps1
Created December 21, 2016 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espoelstra/02c8edc253a1d045a5a157e02ccb93a1 to your computer and use it in GitHub Desktop.
Save espoelstra/02c8edc253a1d045a5a157e02ccb93a1 to your computer and use it in GitHub Desktop.
A PowerShell function to colorize a sequence of text lines that represent an Universal Diff. For more information, see http://megakemp.com/2012/01/19/better-diffs-with-powershell/
function Out-Diff {
<#
.Synopsis
Redirects a Universal DIFF encoded text from the pipeline to the host using colors to highlight the differences.
.Description
Helper function to highlight the differences in a Universal DIFF text using color coding.
.Parameter InputObject
The text to display as Universal DIFF.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[PSObject]$InputObject
)
Process {
$contentLine = $InputObject | Out-String
if ($contentLine -match "^Index:") {
Write-Host $contentLine -ForegroundColor Cyan -NoNewline
} elseif ($contentLine -match "^(\+|\-|\=){3}") {
Write-Host $contentLine -ForegroundColor Gray -NoNewline
} elseif ($contentLine -match "^\@{2}") {
Write-Host $contentLine -ForegroundColor Gray -NoNewline
} elseif ($contentLine -match "^\+") {
Write-Host $contentLine -ForegroundColor Green -NoNewline
} elseif ($contentLine -match "^\-") {
Write-Host $contentLine -ForegroundColor Red -NoNewline
} else {
Write-Host $contentLine -NoNewline
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment