/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 840 - (show annotations)
Sun Dec 14 23:10:23 2008 UTC (15 years, 4 months ago) by dpavlin
File size: 2699 byte(s)
fix ' and " quoting, as well as patterns which begin with - and test it
1 package Frey::Shell::Grep;
2 use Moose;
3
4 extends 'Frey';
5 with 'Frey::Web';
6
7 use English;
8
9 has pattern => (
10 documentation => 'grep pattern',
11 is => 'rw',
12 isa => 'Str',
13 required => 1,
14 default => 'FIXME',
15 );
16
17 has path => (
18 documentation => 'path(s) to grep',
19 is => 'rw',
20 isa => 'Str',
21 required => 1,
22 default => 'lib/ t/ etc/',
23 );
24
25 has _grep_command => (
26 documentation => 'executed grep command',
27 is => 'rw',
28 isa => 'Str',
29 );
30
31 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 = qq|grep -rn $opt $patt | . $self->path;
51 warn "# $cmd";
52 $self->_grep_command( $cmd );
53
54 my @results;
55
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|
83 dt.p {
84 color: #888;
85 white-space: pre-wrap;
86 font-family: monospace;
87 margin-top: 1em;
88 }
89 dd a {
90 float: left;
91 width: 3em;
92 }
93 dd code b {
94 background-color: #ff8;
95 }
96 |);
97
98 my $html;
99 my $last_path = '';
100
101 foreach my $result ( @{ $self->results_as_data } ) {
102
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 }
123 }
124
125 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;
141 }
142
143 1;

  ViewVC Help
Powered by ViewVC 1.1.26