--- branches/no-pager/lib/Frey/Shell/Grep.pm 2008/12/06 14:35:15 737 +++ branches/no-pager/lib/Frey/Shell/Grep.pm 2008/12/06 15:29:10 738 @@ -3,7 +3,6 @@ extends 'Frey'; with 'Frey::Web'; -#with 'Frey::Storage'; use English; @@ -15,19 +14,70 @@ default => 'FIXME', ); -sub as_markup { +has path => ( + documentation => 'path(s) to grep', + is => 'rw', + isa => 'Str', + required => 1, + default => 'lib/ t/ etc/', +); + +has _grep_command => ( + documentation => 'executed grep command', + is => 'rw', + isa => 'Str', +); + +has results_as_data => ( + is => 'rw', + isa => 'ArrayRef[HashRef]', + lazy => 1, + default => +sub { + my ($self) = @_; my $patt = $self->pattern; - $self->title( $patt ); - - my $cmd = 'grep -rn ' . $patt . ' lib/ t/'; + my $cmd = 'grep -rn ' . $patt . ' ' . $self->path; + $self->_grep_command( $cmd ); warn "# $cmd"; - 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 = {@_}; + + 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,30 +88,41 @@ |); my $html; + my $last_path = ''; + + foreach my $result ( @{ $self->results_as_data } ) { + + warn $self->dump( $result ); + + my $text = $result->{text} || die "no text"; - open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!"; - while(<$fh>) { - my ( $path, $line, $text ) = split(/:/,$_,3); $text = $self->html_escape( $text ); - if ( $path ne $last_path ) { - $html .= qq|
$path
|; + 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 =~ s{(\Q$patt\E)}{$1}; + $html .= qq|
$line $text|; + } + $last_path = $path; + } else { + $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" ); - } return $html;