-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtack
executable file
·194 lines (162 loc) · 3.68 KB
/
tack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env perl
#
# tack: run ack, parse output, convert to HTML, link lines back to TextMate
#
# Original version by Andy Armstrong (https://metacpan.org/author/ANDYA)
# available here: http://api.metacpan.org/source/ANDYA/TextMate-JumpTo-0.07/examples/tack
#
# Pedro Melo (https://metacpan.org/author/MELO) adapted to changes
# of ack output
#
use strict;
use 5.014;
use TextMate::JumpTo qw( tm_location );
use HTML::Tiny;
use File::Temp qw( tempfile );
use Data::Dump 'pp';
$| = 1;
my ($t, @args) = _args();
my ($w, $h) = _html_output();
$w->(
$h->open('html'),
$h->head(
$h->meta(
{'http-equiv' => 'Content-Type', content => 'text/html; charset=utf-8'}
),
$h->title("Search results for $t"),
$h->style(
do { local $/; <DATA> }
),
$h->script(
{ src =>
'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'
}
),
),
$h->open('body'),
);
open my $ack, '-|', 'ack', @args or die "Can't run ack ($!)";
my $state = 'file';
my $cfile;
LINE:
while (defined(my $line = <$ack>)) {
chomp $line;
given ($state) {
when ('file') {
next if /^\s*$/;
$w->(
$h->h1($h->a({href => tm_location(file => $line, line => 1)}, $line))
);
$w->($h->open('div'));
$state = 'in_block';
$cfile = $line;
next LINE;
}
when ('in_block') {
if (my ($ln, $f, $info) = $line =~ m/^(\d+)([:-])(.*)$/) {
my $matched = $f eq ':';
my $url = tm_location(file => $cfile, line => $ln);
$info =~ s/\t/ /g;
$info =~ s/&/</g;
$info =~ s/</</g;
$info =~ s/>/>/g;
$w->(
$h->p(
{class => ($matched && 'matched')},
[ $h->a(
{href => $url},
[ $h->span({class => 'ln'}, $ln),
$h->span({class => 'code'}, $info)
]
),
]
)
);
}
elsif (my ($sep) = $line =~ m/^(--)?\s*$/) {
$w->($h->close('div'));
$w->($h->open('div')) if $sep;
$state = 'file' unless $sep;
}
}
}
}
close $ack;
my $hover_js = <<'EOS';
$(function () {
$('.matched').hover(function () { $(this).toggleClass('hover'); });
});
EOS
$w->(
$h->close('div'), $h->script([$hover_js]),
$h->close('body'), $h->close('html'),
);
_open($w->(), 0);
sub _open {
my ($url, $bg) = @_;
my @cmd = ('/usr/bin/open', ($bg ? ('-g') : ()), $url);
system @cmd and die "Can't open $url ($?)";
}
sub _args {
my @args =
grep { $_ ne '--color' && $_ ne '--noheading' && $_ ne '--nobreak' }
@ARGV;
my $title = join(' ', @args);
unshift @args, '--nocolor' unless grep { $_ eq '--nocolor' } @args;
unshift @args, '--heading' unless grep { $_ eq '--heading' } @args;
unshift @args, '--break' unless grep { $_ eq '--break' } @args;
unshift @args, '--context=3'
unless grep { $_ =~ /^(--context=\d+)|(-C)$/ } @args;
return ($title, @args);
}
sub _html_output {
my $h = HTML::Tiny->new;
my ($fh, $filename) = tempfile(SUFFIX => '.html');
my $w = sub {
return $fh->print(@_) if @_;
close($fh);
return $filename;
};
return ($w, $h);
}
__DATA__
html, body {
font-family: monospace;
background: black;
}
a {
text-decoration: none;
width: 100%;
}
h1 {
text-align: left;
border-bottom: 1px solid #222;
color: #ccc;
padding-top: 10px;
padding-bottom: 3px;
margin-bottom: 3px;
}
p {
color: #eee;
margin: 0px;
padding-left: 0 10px 0 10px;
}
.matched {
background: #111;
color: #fff;
}
.ln {
color: #444;
width: 5em;
text-align: right;
padding-right: 1em;
}
.code {
color: #eee;
}
.matched .code {
color: #fff;
}
p.hover {
background: #555;
}