-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight.ps1
38 lines (36 loc) · 1.12 KB
/
highlight.ps1
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
# Find-String.ps1
# Wrapper around dir | select-string which will highlight the pattern in the results
# Write the line with the pattern highlighted in red
function Highlight()
{
param ( [string] $pattern = ""
, [string] $filter = "*.*"
, [switch] $recurse = $false
, [switch] $caseSensitive = $false);
begin {
if ($pattern -eq $null -or $pattern -eq "") { Write-Error "Please provide a search pattern!" ; return }
$regexPattern = $pattern
if($caseSensitive -eq $false) { $regexPattern = "(?i)$regexPattern" }
$regex = New-Object System.Text.RegularExpressions.Regex $regexPattern
}
process {
$inputText = $_;
$index = 0
while($index -lt $inputText.Length)
{
$match = $regex.Match($inputText, $index)
if($match.Success -and $match.Length -gt 0)
{
Write-Host $inputText.SubString($index, $match.Index - $index) -nonewline
Write-Host $match.Value.ToString() -ForegroundColor Red -nonewline
$index = $match.Index + $match.Length
}
else
{
Write-Host $inputText.SubString($index) -nonewline
$index = $inputText.Length
}
}
Write-Host ""
}
}