Skip to content

Instantly share code, notes, and snippets.

@disco0
Forked from lzybkr/MatchInfo.format.ps1xml
Created March 5, 2018 05:34
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 disco0/d520f0ea2f2b7fffd68f6a32e448b665 to your computer and use it in GitHub Desktop.
Save disco0/d520f0ea2f2b7fffd68f6a32e448b665 to your computer and use it in GitHub Desktop.
<Configuration>
<ViewDefinitions>
<View>
<Name>MatchInfo</Name>
<ViewSelectedBy>
<TypeName>Microsoft.PowerShell.Commands.MatchInfo</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>
[string]$curDir = $pwd.Path
if (!$host.UI.SupportsVirtualTerminal)
{
$_.ToString($curDir)
return
}
function FormatLine($matchInfo, [string]$line, [int]$lineNumber, [string]$displayPath, [string]$prefix, [switch]$isMatchLine)
{
if ($isMatchLine)
{
$esc = [char]0x1b
$matchStart = $matchInfo.Matches[0].Index
$matchLength = $matchInfo.Matches[0].Length
$matchEnd = $matchStart + $matchLength
$line = $line.Substring(0, $matchStart) + "${esc}[93m" + $line.Substring($matchStart, $matchLength) + "${esc}[0m" + $line.Substring($matchEnd)
}
if ($matchInfo.Path -ne 'InputStream')
{
"{0}{1}:{2}:{3}" -f $prefix, $displayPath, $lineNumber, $line
}
else
{
"{0}{1}" -f $prefix, $line
}
}
$displayPath = if ('' -eq $curDir) { $_.Path } else { $_.RelativePath($curDir) }
if ($null -eq $_.Context)
{
FormatLine -MatchInfo $_ -Line $_.Line -LineNumber $_.LineNumber -DisplayPath $displayPath -Prefix "" -IsMatchLine
}
else
{
$lines = . {
$displayLineNumber = $_.LineNumber - $_.Context.DisplayPreContext.Length;
foreach ($contextLine in $_.Context.DisplayPreContext)
{
FormatLine -MatchInfo $_ -Line $contextLine -LineNumber $displayLineNumber -DisplayPath $displayPath -Prefix " "
$displayLineNumber += 1
}
FormatLine -MatchInfo $_ -Line $_.Line -LineNumber $displayLineNumber -DisplayPath $displayPath -Prefix "> " -IsMatchLine
$displayLineNumber += 1
foreach ($contextLine in $_.Context.DisplayPostContext)
{
FormatLine -MatchInfo $_ -Line $contextLine -LineNumber $displayLineNumber -DisplayPath $displayPath -Prefix " "
$displayLineNumber += 1
}
}
$lines -join ([Environment]::Newline)
}
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
</ViewDefinitions>
</Configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment