-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfth.psm1
34 lines (31 loc) · 1.54 KB
/
fth.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
function fth {
[CmdletBinding(DefaultParameterSetName = 'in')]
param(
[Parameter(Position=0, ParameterSetName='in')][Parameter(Position=0, ParameterSetName='pin')][Parameter(Position=0, ParameterSetName='lpin')][string]$RE,
[Parameter(Position=1, ValueFromPipeline=$true, ParameterSetName='in')][string]$InputObject,
[Parameter(ParameterSetName='pin')][string[]]$Paths,
[Parameter(ParameterSetName='lpin')][string[]]$LiteralPaths
)
if ($PSCmdlet.MyInvocation.BoundParameters.Count -eq 0) {
echo "fth <RE> [-InputObject <InputObject> | -Paths <Paths> | -LiteralPaths <LiteralPaths>]"
echo '<InputObject> and <file> cannot be both empty and cannot be both designated.'
return
}
if (-not $RE) { write-error '$RE cannot be empty.'; return }
if (-not $InputObject -and -not $Paths -and -not $LiteralPaths) { write-error '$InputObject, $Paths and $LiteralPaths cannot be all empty.'; return }
$foreach_block = {
$line = $_
$ret = sls $RE -inp $line -a
$startPos = 0
foreach ($match in $ret.Matches) {
$substr = $line.Substring($startPos, $match.Index - $startPos)
Write-Host $substr -n
Write-Host $match.Value -n -f Yellow
$startPos = $match.Index + $match.Length
}
Write-Host $line.Substring($startPos)
}
if ($InputObject) { $InputObject | % -proc $foreach_block }
elseif ($Paths) { cat $Paths | % -proc $foreach_block }
else { cat -li $LiteralPaths | % -proc $foreach_block }
}