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; |
22 |
|
23 |
$self->title( $patt ); |
24 |
|
25 |
my $cmd = 'grep -rn ' . $patt . ' lib/ t/'; |
26 |
warn "# $cmd"; |
27 |
|
28 |
my $last_path = ''; |
29 |
|
30 |
$self->add_css(qq| |
31 |
dd a { |
32 |
float: left; |
33 |
width: 3em; |
34 |
} |
35 |
dd code b { |
36 |
background-color: #ff8; |
37 |
} |
38 |
|); |
39 |
|
40 |
my $html; |
41 |
|
42 |
open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!"; |
43 |
while(<$fh>) { |
44 |
my ( $path, $line, $text ) = split(/:/,$_,3); |
45 |
if ( $path ne $last_path ) { |
46 |
$html .= qq|<dt>$path</dt>|; |
47 |
} |
48 |
$text =~ s{(\Q$patt\E)}{<b>$1</b>}; |
49 |
$html .= qq|<dd><a target="editor" href="/editor+$path+$line">$line</a> <code>$text</code>|; |
50 |
$last_path = $path; |
51 |
} |
52 |
if ( $INPUT_LINE_NUMBER > 0 ) { # closing pipe grep output results in error |
53 |
close($fh) || die "can't close pipe to $cmd $!"; |
54 |
|
55 |
$html = qq| |
56 |
<h1>$patt</h1> |
57 |
<dl>$html</dl> |
58 |
|; |
59 |
|
60 |
} else { |
61 |
|
62 |
$html = $self->error( "No results for $patt\n" ); |
63 |
|
64 |
} |
65 |
|
66 |
return $html; |
67 |
} |
68 |
|
69 |
1; |