-
Notifications
You must be signed in to change notification settings - Fork 4
/
Find-String.psm1
311 lines (270 loc) · 10.7 KB
/
Find-String.psm1
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
if (Get-Module -Name Find-String) { return }
<#
Find-String is a PowerShell script whose purpose is to emulate grep and/or ack.
PowerShell already has the built-in Select-String cmdlet, but this script wraps
provides match highlighting on top of the searching capabilities.
It currently highlights matches in a similar style to ack.
#>
#requires -version 2
function Find-String {
[CmdletBinding(DefaultParameterSetName = "Filter")]
param (
[Parameter(Position = 0, Mandatory = $true)]
[regex] $pattern,
[Parameter(Position = 1, ParameterSetName = "Filter")]
[string] $filter = "*.*",
[Parameter(Position = 1, ParameterSetName = "Include")]
[string[]] $include = @(),
[string[]] $excludeFiles = @(),
[string[]] $excludeDirectories = @(),
[string[]] $path = @(),
[switch] $recurse = $true,
[switch] $caseSensitive = $false,
[int[]] $context = 0,
[switch] $passThru = $false,
[switch] $pipeOutput = $false,
[switch] $listMatchesOnly = $false
)
if ((-not $caseSensitive) -and (-not $pattern.Options -match "IgnoreCase")) {
$pattern = New-Object -TypeName regex -Property $pattern.ToString(), @($pattern.Options, "IgnoreCase")
}
function directoriesToExclude {
if ($excludeDirectories.Length -gt 0) {
return $excludeDirectories
}
else {
'bin', 'obj', '.git', '.hg', '.svn', '_ReSharper\.'
if ($global:FindStringDirectoriesToExclude -ne $null) {
$global:FindStringDirectoriesToExclude
}
}
}
function filesToExclude {
if ($excludeFiles.Length -gt 0) {
return $excludeFiles
}
else {
'*exe', '*pdb', '*dll', '*.gif', '*.jpg', '*.doc', '*.pdf'
if ($global:FindStringFileTypesToExclude -ne $null) {
$global:FindStringFileTypesToExclude
}
}
}
function shouldFilterDirectory {
param (
[Parameter(Mandatory = $true)]
$item
)
$directoriesToExclude = directoriesToExclude | ForEach-Object { "\\$_" }
if ((Select-String -Pattern $directoriesToExclude -input $item.DirectoryName) -ne $null) {
Write-Debug -Message "Excluding results from $item"
return $true
}
else {
return $false
}
}
function filterExcludes {
param (
[Parameter(Mandatory = $true)]
$item
)
if (-not ($item -is [System.IO.FileInfo])) { return $false }
if (shouldFilterDirectory $item) { return $false }
return $true
}
switch ($PsCmdlet.ParameterSetName) {
'Filter' {
if ($passThru) {
Get-ChildItem -recurse:$recurse -filter:$filter -path $path -exclude (& filesToExclude) |
Where-Object { filterExcludes $_ } |
Select-String -caseSensitive:$caseSensitive -pattern:$pattern -AllMatches -context $context
}
elseif ($listMatchesOnly) {
Get-ChildItem -recurse:$recurse -filter:$filter -path $path -exclude (& filesToExclude) |
Where-Object { filterExcludes $_ } |
Select-String -caseSensitive:$caseSensitive -pattern:$pattern -List |
Select-Object -ExpandProperty Path
}
else {
Get-ChildItem -recurse:$recurse -filter:$filter -path $path -exclude (& filesToExclude) |
Where-Object { filterExcludes $_ } |
Select-String -caseSensitive:$caseSensitive -pattern:$pattern -AllMatches -context $context |
Out-ColorMatchInfo -pipeOutput:$pipeOutput
}
}
'Include' {
if ($passThru) {
Get-ChildItem -recurse:$recurse -include:$include -path $path -exclude (& filesToExclude) |
Where-Object { filterExcludes $_ } |
Select-String -caseSensitive:$caseSensitive -pattern:$pattern -AllMatches -context $context
}
elseif ($listMatchesOnly) {
Get-ChildItem -recurse:$recurse -include:$include -path $path -exclude (& filesToExclude) |
Where-Object { filterExcludes $_ } |
Select-String -caseSensitive:$caseSensitive -pattern:$pattern -AllMatches -context $context |
Select-Object -ExpandProperty Path
}
else {
Get-ChildItem -recurse:$recurse -include:$include -path $path -exclude (& filesToExclude) |
Where-Object { filterExcludes $_ } |
Select-String -caseSensitive:$caseSensitive -pattern:$pattern -AllMatches -context $context |
Out-ColorMatchInfo -pipeOutput:$pipeOutput
}
}
}
<#
.Synopsis
Searches text files by pattern and displays the results.
.Description
Searches text files by pattern and displays the results.
.Parameter Pattern
Specifies the text to find. Type a string or regular expression.
.Parameter Filter
Specifies the file types to search in. The default is all file types (*.*).
.Parameter Include
Specifies the file types to search in. This allows you to search across
multiple file types (i.e. *.ps1,*.psm1).
.Parameter ExcludeFiles
Specifies the file types to exclude from searches. If set, this overrides
any global defaults or configuration.
.Parameter ExcludeDirectories
Specifies the directories to exclude from searches. It really only makes
sense for recursive searches. If set, this overrides any global defaults
or configuration.
.Parameter Path
Specifies the path to the files to be searched. Wildcards are permitted.
The default location is the local directory.
.Parameter Recurse
Gets the items in the specified path and in all child directies. This is
the default.
.Parameter CaseSensitive
Makes matches case-sensitive. By default, matches are not case-sensitive.
.Parameter Context
Captures the specified number of lines before and after the line with the
match. This allows you to view the match in context.
.Parameter PassThru
Passes the literal MatchInfo object representing the found match to the
pipeline. By default, this cmdlet does not send anything through the
object pipeline.
.Parameter PipeOutput
Sends all output along the object pipeline. By default, this command uses
color to help with readability; however, this prevents the output from being
piped to another command. If you wish to pipe the output of this command to
something else, be sure to use this parameter.
.Parameter ListMatchesOnly
Returns all files that have matches existing in them, but doesn't display
any of the matches themselves.
#>
}
function Out-ColorMatchInfo {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.PowerShell.Commands.MatchInfo]
$match,
[switch]
$onlyShowMatches = $false,
[switch]
$pipeOutput = $false
)
begin {
$script:priorPath = ''
$script:hasContext = $false
$script:buffer = New-Object -TypeName System.Text.StringBuilder
}
process {
function output {
param (
[string]
$str = '',
$foregroundColor = $host.ui.RawUI.ForegroundColor,
$backgroundColor = $host.ui.RawUI.BackgroundColor,
[switch]
$noNewLine = $false
)
if ($pipeOutput) {
if ($noNewLine) {
$script:buffer.Append($str) | out-null
}
else {
$script:buffer.AppendLine($str) | out-null
}
}
else {
$args = @{ Object = $str; NoNewLine = $NoNewLine }
if (-not($foregroundColor -lt 0)) {
$args.Add('ForegroundColor', $foregroundColor)
}
if (-not($backgroundColor -lt 0)) {
$args.Add('BackgroundColor', $backgroundColor)
}
Write-Host @args
}
}
function Get-RelativePath([string] $path = '') {
$path = $path.Replace($pwd.Path, '')
if ($path.StartsWith('\') -and (-not $path.StartsWith('\\'))) {
$path = $path.Substring(1)
}
$path
}
function Write-PathOrSeparator($match) {
if ($script:priorPath -ne $match.Path) {
output ''
output (Get-RelativePath $match.Path) -foregroundColor Green
$script:priorPath = $match.Path
}
else {
if ($script:hasContext) {
output '--'
}
}
}
function Write-HighlightedMatch($match) {
if (-not $onlyShowMatches) {
output "$($match.LineNumber):" -nonewline
}
$index = 0
foreach ($m in $match.Matches) {
output $match.Line.SubString($index, $m.Index - $index) -nonewline
output $m.Value -ForegroundColor Black -BackgroundColor Yellow -nonewline
$index = $m.Index + $m.Length
}
if ($index -lt $match.Line.Length) {
output $match.Line.SubString($index) -nonewline
}
output ''
}
function Write-Context {
param (
$context = '',
$contextLines = ''
)
if ($context.length -eq $null) {return}
$script:hasContext = $true
for ($i = 0; $i -lt $context.length; $i++) {
"$($contextLines[$i])- $($context[$i])"
}
}
if (-not $onlyShowMatches) {
Write-PathOrSeparator $match
}
$lines = ($match.LineNumber - $match.Context.DisplayPreContext.Length)..($match.LineNumber - 1)
Write-Context $match.Context.DisplayPreContext $lines
Write-HighlightedMatch $match
$lines = ($match.LineNumber + 1)..($match.LineNumber + $match.Context.DisplayPostContext.Length)
Write-Context $match.Context.DisplayPostContext $lines
if ($script:buffer.Length -gt 0) {
$script:buffer.ToString()
}
}
end {}
<#
.Synopsis
Highlights MatchInfo objects similar to the output from ack.
.Description
Highlights MatchInfo objects similar to the output from ack.
#>
}
Export-ModuleMember -Function Find-String
Export-ModuleMember -Function Out-ColorMatchInfo