/[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 661 by dpavlin, Mon Dec 1 22:44:41 2008 UTC revision 842 by dpavlin, Sun Dec 14 23:22:15 2008 UTC
# Line 3  use Moose; Line 3  use Moose;
3    
4  extends 'Frey';  extends 'Frey';
5  with 'Frey::Web';  with 'Frey::Web';
 #with 'Frey::Storage';  
6    
7  use English;  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 $cmd = 'grep -rn ' . $patt . ' lib/ t/';          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 = 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            warn "# callbacks: ",$self->dump( $callback ) if $callback;
80    
81            my $patt = $self->pattern;
82            $self->title( $patt );
83    
84          $self->add_css(qq|          $self->add_css(qq|
85                    dt.p {
86                            color: #888;
87                            white-space: pre-wrap;
88                            font-family: monospace;
89                            margin-top: 1em;
90                    }
91                  dd a {                  dd a {
92                          float: left;                          float: left;
93                          width: 3em;                          width: 3em;
# Line 38  sub as_markup { Line 97  sub as_markup {
97                  }                  }
98          |);          |);
99    
100          open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!";          my $html;
101          while(<$fh>) {          my $last_path = '';
102                  my ( $path, $line, $text ) = split(/:/,$_,3);  
103                  if ( $path ne $last_path ) {          foreach my $result ( @{ $self->results_as_data } ) {
104                          $html .= qq|<dt>$path</dt>|;  
105                    warn $self->dump( $result ) if $self->debug;
106    
107                    my $text = $result->{text} || die "no text";
108    
109                    if ( my $path = $result->{path} ) {
110                            my $line = $result->{line} || die "no line";
111                            if ( $path ne $last_path ) {
112                                    $html .= qq|<dt class="p">$path</dt>|;
113                            }
114                            if ( my $dd = $callback->{dd} ) {
115                                    $html .= $dd->( $patt, $path, $line, $text );
116                            } else {
117                                    $text = $self->html_escape( $text );
118                                    $text =~ s{(\Q$patt\E)}{<b>$1</b>};
119                                    $html .= qq|<dd><a target="editor" href="/editor+$path+$line">$line</a> <code>$text</code>|;
120                            }
121                            $last_path = $path;
122                    } else {
123                            $text = $self->html_escape( $text );
124                            $html .= qq|<dt>$text</dt>|;
125                  }                  }
                 $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;  
126          }          }
127          if ( $INPUT_LINE_NUMBER > 0 ) { # closing pipe grep output results in error  
128                  close($fh) || die "can't close pipe to $cmd $!";          if ( $html ) {
129                    my $command = $self->_grep_command;
130                    $html = qq|
131                            <h1>$patt</h1>
132                            <pre>$command</pre>
133                            <dl>$html</dl>
134                    |;
135            } else {
136                    $html = $self->error( "No results for $patt\n" );
137          }          }
138    
139          $html .= qq|</dl>|;          $self->add_head(qq|
140                    <link rel="search" type="application/opensearchdescription+xml" title="Frey::Shell::Grep" href="/Frey::OpenSearch/grep_as_markup">
141            |);
142    
143          return $html;          return $html;
144  }  }

Legend:
Removed from v.661  
changed lines
  Added in v.842

  ViewVC Help
Powered by ViewVC 1.1.26