1 |
package Frey::Shell::Grep; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
with 'Frey::Web'; |
6 |
#with 'Frey::Storage'; |
7 |
|
8 |
use English; |
9 |
|
10 |
has pattern => ( |
11 |
documentation => 'grep for pattern', |
12 |
is => 'rw', |
13 |
isa => 'Str', |
14 |
required => 1, |
15 |
default => 'FIXME', |
16 |
); |
17 |
|
18 |
sub as_markup { |
19 |
my ($self) = @_; |
20 |
|
21 |
my $patt = $self->pattern || 'FIXME'; |
22 |
|
23 |
$self->title( $patt ); |
24 |
|
25 |
my $cmd = 'grep -rn ' . $patt . ' lib/ t/'; |
26 |
warn "# $cmd"; |
27 |
|
28 |
my $html = qq|<h1>$patt</h1><dl>|; |
29 |
my $last_path = ''; |
30 |
|
31 |
$self->add_css(qq| |
32 |
dd a { |
33 |
float: left; |
34 |
width: 3em; |
35 |
} |
36 |
dd code b { |
37 |
background-color: #ff8; |
38 |
} |
39 |
|); |
40 |
|
41 |
open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!"; |
42 |
while(<$fh>) { |
43 |
my ( $path, $line, $text ) = split(/:/,$_,3); |
44 |
if ( $path ne $last_path ) { |
45 |
$html .= qq|<dt>$path</dt>|; |
46 |
} |
47 |
$text =~ s{(\Q$patt\E)}{<b>$1</b>}; |
48 |
$html .= qq|<dd><a target="editor" href="/editor+$path+$line">$line</a> <code>$text</code>|; |
49 |
$last_path = $path; |
50 |
} |
51 |
if ( $INPUT_LINE_NUMBER > 0 ) { # closing pipe grep output results in error |
52 |
close($fh) || die "can't close pipe to $cmd $!"; |
53 |
} |
54 |
|
55 |
$html .= qq|</dl>|; |
56 |
|
57 |
return $html; |
58 |
} |
59 |
|
60 |
1; |