-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path1095.ps1
More file actions
51 lines (44 loc) · 1.23 KB
/
Copy path1095.ps1
File metadata and controls
51 lines (44 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<#
.Synopsis
Highlights MatchInfo objects similar to the output from grep.
.Description
Highlights MatchInfo objects similar to the output from grep.
#>
#requires -version 2
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[Microsoft.PowerShell.Commands.MatchInfo] $match
)
begin {}
process {
function Get-RelativePath([string] $path) {
$path = $path.Replace($pwd.Path, '')
if ($path.StartsWith('\') -and (-not $path.StartsWith('\\'))) {
$path = $path.Substring(1)
}
$path
}
function Write-PathAndLine($match) {
Write-Host (Get-RelativePath $match.Path) -foregroundColor DarkMagenta -nonewline
Write-Host ':' -foregroundColor Cyan -nonewline
Write-Host $match.LineNumber -foregroundColor DarkYellow
}
function Write-HighlightedMatch($match) {
$index = 0
foreach ($m in $match.Matches) {
Write-Host $match.Line.SubString($index, $m.Index - $index) -nonewline
Write-Host $m.Value -ForegroundColor Red -nonewline
$index = $m.Index + $m.Length
}
if ($index -lt $match.Line.Length) {
Write-Host $match.Line.SubString($index) -nonewline
}
''
}
Write-PathAndLine $match
$match.Context.DisplayPreContext
Write-HighlightedMatch $match
$match.Context.DisplayPostContext
''
}
end {}