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