--- trunk/lib/Frey/Shell/Grep.pm 2008/12/01 22:44:41 661 +++ trunk/lib/Frey/Shell/Grep.pm 2009/01/04 23:06:36 916 @@ -3,32 +3,91 @@ extends 'Frey'; with 'Frey::Web'; -#with 'Frey::Storage'; use English; has pattern => ( - documentation => 'grep for pattern', + documentation => 'grep pattern', is => 'rw', isa => 'Str', required => 1, default => 'FIXME', ); -sub as_markup { - my ($self) = @_; +has path => ( + documentation => 'path(s) to grep', + is => 'rw', + isa => 'Str', + required => 1, + default => '*.[Pp][MmLl] lib/ t/ etc/', +); - my $patt = $self->pattern || 'FIXME'; +has _grep_command => ( + documentation => 'executed grep command', + is => 'rw', + isa => 'Str', +); - $self->title( $patt ); +has results_as_data => ( + is => 'rw', + isa => 'ArrayRef[HashRef]', + lazy => 1, + default => +sub { - my $cmd = 'grep -rn ' . $patt . ' lib/ t/'; + my ($self) = @_; + + my $patt = $self->pattern; + my $opt = ''; + $opt = '--' if $patt =~ m{^-}; + + if ( $patt =~ m{'} ) { + $patt = qq|"$patt"|; + } else { + $patt = qq|'$patt'|; + } + + my $cmd = qq|grep -rn $opt $patt | . $self->path; warn "# $cmd"; + $self->_grep_command( $cmd ); - my $html = qq|

$patt

|; - my $last_path = ''; + my @results; + + open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!"; + while(<$fh>) { + my ( $path, $line, $text ) = split(/:/,$_,3); + if ( ! $line ) { + push @results, { text => $path }; + } else { + push @results, { path => $path, line => $line, text => $text }; + } + } + + if ( $INPUT_LINE_NUMBER > 0 ) { # closing pipe grep output results in error + close($fh) || die "can't close pipe to $cmd $!"; + } + +# warn "# ", $self->dump( @results ); + + return \@results; +}); + +sub as_markup { + my $self = shift; + my $callback = {@_}; + + warn "# callbacks: ",$self->dump( $callback ) if $callback; + + my $patt = $self->pattern; + $self->title( $patt ); $self->add_css(qq| + dt.p { + color: #888; + white-space: pre-wrap; + font-family: monospace; + margin-top: 1em; + } dd a { float: left; width: 3em; @@ -38,21 +97,48 @@ } |); - open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!"; - while(<$fh>) { - my ( $path, $line, $text ) = split(/:/,$_,3); - if ( $path ne $last_path ) { - $html .= qq|
$path
|; + my $html; + my $last_path = ''; + + foreach my $result ( @{ $self->results_as_data } ) { + + warn $self->dump( $result ) if $self->debug; + + my $text = $result->{text} || die "no text"; + + if ( my $path = $result->{path} ) { + my $line = $result->{line} || die "no line"; + if ( $path ne $last_path ) { + $html .= qq|
$path
|; + } + if ( my $dd = $callback->{dd} ) { + $html .= $dd->( $patt, $path, $line, $text ); + } else { + $text = $self->html_escape( $text ); + $text =~ s{(\Q$patt\E)}{$1}; + $html .= qq|
$line $text|; + } + $last_path = $path; + } else { + $text = $self->html_escape( $text ); + $html .= qq|
$text
|; } - $text =~ s{(\Q$patt\E)}{$1}; - $html .= qq|
$line $text|; - $last_path = $path; } - if ( $INPUT_LINE_NUMBER > 0 ) { # closing pipe grep output results in error - close($fh) || die "can't close pipe to $cmd $!"; + + if ( $html ) { + my $command = $self->_grep_command; + $html = qq| +

$patt

+
$command
+
$html
+ |; + } else { + $html = $self->error( "No results for $patt\n" ); } - $html .= qq|
|; + $self->add_head(qq| + + |); return $html; }