{"id":1271,"date":"2021-01-21T08:47:53","date_gmt":"2021-01-21T14:47:53","guid":{"rendered":"http:\/\/ninmonkeys.com\/blog\/?p=1271"},"modified":"2021-01-21T08:47:53","modified_gmt":"2021-01-21T14:47:53","slug":"improving-diff-readability-on-windows-tip","status":"publish","type":"post","link":"https:\/\/ninmonkeys.com\/blog\/2021\/01\/21\/improving-diff-readability-on-windows-tip\/","title":{"rendered":"Improving &#8216;diff&#8217; readability on Windows | Tip"},"content":{"rendered":"\n<p>The output of <code class=\"\" data-line=\"\">diff -q path1 path2<\/code> is pretty verbose. This function<\/p>\n\n\n\n<ul><li>Converts full paths to relative<\/li><li>Differences are <span style=\"color:red\">red<\/span>.<\/li><\/ul>\n\n\n\n<h2>Missing &#8216;diff&#8217; ? <\/h2>\n\n\n\n<p>If <code class=\"\" data-line=\"\">git<\/code> is installed, you may need to update your <code class=\"\" data-line=\"\">%PATH%<\/code> environment variable.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\" data-line=\"\"># in your profile\n$Env:Path = &quot;$Env:ProgramFiles\\Git\\usr\\bin&quot;, $Env:Path -join &#039;;&#039;<\/code><\/pre>\n\n\n\n<h2>Stand-Alone function <code class=\"\" data-line=\"\">Compare-Directory<\/code><\/h2>\n\n\n\n<p>This is an isolated version of <code class=\"\" data-line=\"\">&lt;a href=&quot;https:\/\/github.com\/ninmonkey\/Ninmonkey.Console&quot;&gt;Ninmonkey.Console: Compare-Directory&lt;\/a&gt;<\/code> . I removed all dependencies except coloring is provided by the module <a href=\"https:\/\/github.com\/PoshCode\/Pansies\">PoshCode\/Pansies<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\" data-line=\"\">function Invoke-NativeCommand {\n    &lt;#\n    .synopsis\n        wrapper to both call &#039;Get-NativeCommand&#039; and invoke an argument list\n    .example\n        PS&gt; # Use the first &#039;python&#039; in path:\n        Invoke-NativeCommand &#039;python&#039; -Args &#039;--version&#039;\n    #&gt;\n\n    param(\n        # command name: &#039;python&#039; &#039;ping.exe&#039;, extension is optional\n        [Parameter(Mandatory, Position = 0)]\n        [string]$CommandName,\n\n        # Force error if multiple  binaries are found\n        [Parameter()][switch]$OneOrNone,\n\n        # native command argument list\n        [Alias(&#039;Args&#039;)]\n        [Parameter(Position = 1)]\n        [string[]]$ArgumentList\n    )\n\n    $binCommand = Get-NativeCommand $CommandName -OneOrNone:$OneOrNone -ea Stop\n    &amp; $binCommand @ArgumentList\n}\n\nfunction Compare-Directory {\n    &lt;#\n    .SYNOPSIS\n    Compare Two directories using &#039;diff&#039;\n    .EXAMPLE\n    Compare-Directory &#039;c:\\foo&#039; &#039;c:\\bar\\bat&#039;\n    #&gt;\n    [Alias(&#039;DiffDir&#039;)]\n    param(\n        # Path1\n        [Parameter(Mandatory, Position = 0)]\n        [string]$Path1,\n\n        # Path2\n        [Parameter(Mandatory, Position = 1)]\n        [string]$Path2,\n\n        # Output original raw text?\n        [Parameter()][switch]$OutputRaw\n    )\n\n    $Base1 = $Path1 | Get-Item -ea Stop\n    $Base2 = $Path2 | Get-Item -ea Stop\n    $Label1 = $Base1 | Split-Path -Leaf | New-Text -fg &#039;green&#039;\n    $Label2 = $Base2 | Split-Path -Leaf | New-Text -fg &#039;yellow&#039;\n\n    &quot;Comparing:\n        Path: $Path1\n        Path: $Path2\n    &quot; | Write-Information\n\n    $stdout = Invoke-NativeCommand &#039;diff&#039; -args @(\n        &#039;-q&#039;\n        $Base1\n        $Base2\n    )\n\n    $outColor = $stdout\n    $outColor = $outColor -replace [regex]::Escape($path1), $Label1\n    $outColor = $outColor -replace [regex]::Escape($path2), $Label2\n    $outColor = $outColor -replace &#039;Only in&#039;, (New-Text &#039;Only In&#039; -fg &#039;red&#039;)\n    $outColor = $outColor -replace &#039;Differ&#039;, (New-Text &#039;Differ&#039; -fg &#039;red&#039;)\n\n    if ($OutputRaw) {\n        h1 &#039;Raw&#039; | Write-Information\n        $stdout\n        return\n    }\n\n    $outColor\n}\n\nfunction Get-NativeCommand {\n    &lt;#\n    .synopsis\n        wrapper that returns Get-Item on a native command\n    .example\n        # if you want an error when multiple options are found\n        PS&gt; Get-NativeCommand python -OneOrNone\n    .example\n        # note: this is important, $cmdArgs to be an array not scalar for &#039;@&#039; usage\n        $binPy = Get-NativeCommand python\n        $cmdArgs = @(&#039;--version&#039;)\n        &amp; $binPy @cmdArgs\n    .example\n    #&gt;\n    [cmdletbinding()]\n    param(\n        # Name of Native .exe Application\n        [Parameter(Mandatory, Position = 0, ValueFromPipeline)]\n        [object]$CommandName,\n\n        # One or None: Raise errors when there are more than one match\n        [Parameter()][switch]$OneOrNone\n    )\n\n    process {\n        try {\n            $query = Get-Command -Name $CommandName -All -CommandType Application -ea Stop\n            | Sort-Object Name\n\n        } catch [CommandNotFoundException] {\n            Write-Error &quot;ZeroResults: &#039;$CommandName&#039;&quot;\n            return\n        }\n\n        if ($OneOrNone -and $query.Count -gt 1) {\n            $query | Format-Table -Wrap -AutoSize -Property Name, Version, Source\n            Write-Error &quot;OneOrNone: Multiple results for &#039;$CommandName&#039;&quot;\n            return\n        }\n\n        if ($query.Count -gt 1) {\n            $query = $query | Select-Object -First 1\n        }\n\n        Write-Debug &quot;Using Item: $($query.Source)&quot;\n        $query\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The output of diff -q path1 path2 is pretty verbose. This function Converts full paths to relative Differences are red. Missing &#8216;diff&#8217; ? If git is installed, you may need to update your %PATH% environment variable. Stand-Alone function Compare-Directory This is an isolated version of &lt;a href=&quot;https:\/\/github.com\/ninmonkey\/Ninmonkey.Console&quot;&gt;Ninmonkey.Console: Compare-Directory&lt;\/a&gt; . I removed all dependencies except coloring [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1272,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[66],"tags":[51,29,35,22],"_links":{"self":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/1271"}],"collection":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/comments?post=1271"}],"version-history":[{"count":6,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/1271\/revisions"}],"predecessor-version":[{"id":1278,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/1271\/revisions\/1278"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/media\/1272"}],"wp:attachment":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/media?parent=1271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/categories?post=1271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/tags?post=1271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}