/[Frey]/trunk/lib/Frey/Shell/Grep.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Frey/Shell/Grep.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 638 by dpavlin, Sun Nov 30 14:52:01 2008 UTC revision 840 by dpavlin, Sun Dec 14 23:10:23 2008 UTC
# Line 3  use Moose; Line 3  use Moose;
3    
4  extends 'Frey';  extends 'Frey';
5  with 'Frey::Web';  with 'Frey::Web';
6  #with 'Frey::Storage';  
7    use English;
8    
9  has pattern => (  has pattern => (
10          documentation => 'grep for pattern',          documentation => 'grep pattern',
11          is => 'rw',          is => 'rw',
12          isa => 'Str',          isa => 'Str',
13          required => 1,          required => 1,
14          default => 'FIXME',          default => 'FIXME',
15  );  );
16    
17  sub as_markup {  has path => (
18          my ($self) = @_;          documentation => 'path(s) to grep',
19            is => 'rw',
20            isa => 'Str',
21            required => 1,
22            default => 'lib/ t/ etc/',
23    );
24    
25          my $patt = $self->pattern || 'FIXME';  has _grep_command => (
26            documentation => 'executed grep command',
27            is => 'rw',
28            isa => 'Str',
29    );
30    
31          $self->title( $patt );  has results_as_data => (
32            is => 'rw',
33            isa => 'ArrayRef[HashRef]',
34            lazy => 1,
35            default =>
36    sub {
37    
38            my ($self) = @_;
39    
40            my $patt = $self->pattern;
41            my $opt = '';
42            $opt = '--' if $patt =~ m{^-};
43    
44            if ( $patt =~ m{'} ) {
45                    $patt = qq|"$patt"|;
46            } else {
47                    $patt = qq|'$patt'|;
48            }
49    
50          my $cmd = 'grep -rn ' . $patt . ' lib/ t/';          my $cmd = qq|grep -rn $opt $patt | . $self->path;
51          warn "# $cmd";          warn "# $cmd";
52            $self->_grep_command( $cmd );
53    
54          my $html = qq|<h1>$patt</h1><dl>|;          my @results;
55          my $last_path = '';  
56            open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!";
57            while(<$fh>) {
58                    my ( $path, $line, $text ) = split(/:/,$_,3);
59                    if ( ! $line ) {
60                            push @results, { text => $path };
61                    } else {
62                            push @results, { path => $path, line => $line, text => $text };
63                    }
64            }
65    
66            if ( $INPUT_LINE_NUMBER > 0 ) { # closing pipe grep output results in error
67                    close($fh) || die "can't close pipe to $cmd $!";
68            }
69    
70    #       warn "# ", $self->dump( @results );
71    
72            return \@results;
73    });
74    
75    sub as_markup {
76            my $self = shift;
77            my $callback = {@_};
78    
79            my $patt = $self->pattern;
80            $self->title( $patt );
81    
82          $self->add_css(qq|          $self->add_css(qq|
83                    dt.p {
84                            color: #888;
85                            white-space: pre-wrap;
86                            font-family: monospace;
87                            margin-top: 1em;
88                    }
89                  dd a {                  dd a {
90                          float: left;                          float: left;
91                          width: 3em;                          width: 3em;
# Line 36  sub as_markup { Line 95  sub as_markup {
95                  }                  }
96          |);          |);
97    
98          open(my $fh, '-|', $cmd) or die "ack: $@";          my $html;
99          while(<$fh>) {          my $last_path = '';
100                  my ( $path, $line, $text ) = split(/:/,$_,3);  
101                  if ( $path ne $last_path ) {          foreach my $result ( @{ $self->results_as_data } ) {
102                          $html .= qq|<dt>$path</dt>|;  
103                    warn $self->dump( $result ) if $self->debug;
104    
105                    my $text = $result->{text} || die "no text";
106    
107                    $text = $self->html_escape( $text );
108                    if ( my $path = $result->{path} ) {
109                            my $line = $result->{line} || die "no line";
110                            if ( $path ne $last_path ) {
111                                    $html .= qq|<dt class="p">$path</dt>|;
112                            }
113                            if ( my $dd = $callback->{dd} ) {
114                                    $html .= $dd->( $patt, $path, $line, $text );
115                            } else {
116                                    $text =~ s{(\Q$patt\E)}{<b>$1</b>};
117                                    $html .= qq|<dd><a target="editor" href="/editor+$path+$line">$line</a> <code>$text</code>|;
118                            }
119                            $last_path = $path;
120                    } else {
121                            $html .= qq|<dt>$text</dt>|;
122                  }                  }
                 $text =~ s{(\Q$patt\E)}{<b>$1</b>};  
                 $html .= qq|<dd><a target="editor" href="/editor+$path+$line">$line</a> <code>$text</code>|;  
                 $last_path = $path;  
123          }          }
         close($fh) || die $!;  
124    
125          $html .= qq|</dl>|;          if ( $html ) {
126                    my $command = $self->_grep_command;
127                    $html = qq|
128                            <h1>$patt</h1>
129                            <pre>$command</pre>
130                            <dl>$html</dl>
131                    |;
132            } else {
133                    $html = $self->error( "No results for $patt\n" );
134            }
135    
136            $self->add_head(qq|
137                    <link rel="search" type="application/opensearchdescription+xml" title="Frey::Shell::Grep" href="/Frey::OpenSearch/grep_as_markup">
138            |);
139    
140          return $html;          return $html;
141  }  }

Legend:
Removed from v.638  
changed lines
  Added in v.840

  ViewVC Help
Powered by ViewVC 1.1.26