-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathf-opener
executable file
·281 lines (233 loc) · 6.97 KB
/
f-opener
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/perl
use utf8;
use strict;
use Data::Dumper;
use POSIX;
use Term::ANSIColor;
use Time::HiRes qw (sleep);
use Term::ReadKey;
binmode(STDIN, ":encoding(utf8)");
binmode STDOUT, ":utf8";
# Colors see http://misc.flogisoft.com/bash/tip_colors_and_formatting
my @lines = <STDIN>;
unshift @lines, '--';
my @matches;
for my $i (0 .. $#lines) {
my $line = $lines[$i];
if ($line =~ /^(.*?)\:(\d+)\:(.*)$/){
push @matches, [$i, $1, $2 ];
}
}
my $state;
my $grep = $ARGV[0];
$state->{'grep'} = $grep;
$state->{'x'} = 0;
$state->{'y'} = 0;
$state->{'lines'} = @lines;
$state->{'draw'} = 0;
$state->{'cursor'} = 0; # which match do we focus on?
$state->{'matches'} = \@matches;
# print Dumper($state);
# exit;
sub draw {
my ($st) = @_;
my @size = Term::ReadKey::GetTerminalSize STDOUT;
my @matches = @{ $st->{matches} };
print $st->{cursor};
my @cursor = @{
$matches[$st->{cursor}]
};
my $focus = $cursor[0];
$st->{maxx} = $size[0];
$st->{maxy} = $size[1];
# Reset the window, don't do it the first time
if ($st->{draw}++ > 0){
print "\e[H"; # reset
# print "\e[J"; # clear
}
# Draw a sticky file header
# Draw the lines we want
my $height = $st->{maxy} - 1;
$st->{h} = $height;
my $y1 = $st->{y};
my $y2 = $y1 + $height - 0;
my $width = $st->{maxx} - 1;
$st->{w} = $width;
my $x1 = $st->{x};
my $x2 = $x1 + $width - 5;
my $file = '';
my $lnum = -1;
my $text = '';
my $mode = '';
my $grep = $st->{grep};
for (my $y = $y1; $y < $y2; $y++){
my $line = $lines[$y];
chomp $line;
if ($line eq '--'){
my $line = $lines[$y+1];
$line =~ /^(.*?)\-(\d+)\-(.*)$/;
print
""
. "\e[36m" # cyan fg
. $1
# . sprintf('%-'.($width).'s', $1)
. " "
. "\e[90m" # dark grey
# . "\e[100m" # gray bg
. ( '-' x ($width - length($1) ))
. "\e[39m" # default fg
# . "\e[49m" # default bg
. "\n";
next;
}
$file = '';
$lnum = -1;
$text = 'crap';
$mode = '?';
if ($line =~ /^(.*?)\-(\d+)\-(.*)$/){
$file = $1;
$lnum = $2;
$text = $3;
$mode = '-';
} elsif ($line =~ /^(.*?)\:(\d+)\:(.*)$/){
$file = $1;
$lnum = $2;
$text = $3;
$mode = ':';
}
my $start = '';
if ($y == $focus){
$start .= ''
. "\e[1m" # bold fg
. "\e[44m" # blue bg
. ' '
. $lnum
. ' '
# . "\e[0m" # not bold
# . "\e[49m" # default bg
. "\e[100m" # gray bg
;
} else {
$start .=
"\e[32m" # green fg
. ' '
. $lnum
. "\e[36m" # cyan fg
. $mode
. "\e[39m" # default fg
;
}
my $sx = length($lnum)+2;
my $wx = $width - $sx;
my $sub = sprintf("%-".$wx."s", substr($text, $x1, $wx) );
my $on;
my $of;
my $end;
if ($y == $focus){
$on .= "\e[41m"; # red bg
# $of .= "\e[49m"; # default bg
$of .= "\e[100m"; # default bg
$end .= "\e[21m"; # not bold
$end .= "\e[49m"; # default bg
} else {
$on .= "\e[91m"; # red fg
$on .= "\e[1m"; # bold
$of .= "\e[21m"; # not bold
$of .= "\e[39m"; # default fg
}
$sub =~ s/($grep)/$on$1$of/g;
print "$start$sub$end\n";
}
# If the number of rows is more than the screen height then
# Draw a position and status bar
# print Dumper($st);
# search terms
# print $state->{grep};
print "\e[7m"; # invert
print " lines ".($y1+1)."-$y2 / ". ($#lines+1). ' ';
printf "%d% ", 100 * ($y2 / ($#lines+1)); # per %
printf "Found %d out of %d", $st->{cursor}, $#matches, $st->{cursor};
print "\e[27m"; # reset invert
print "\e[?25l "; # no blinking cursor
# print "crap\n";
}
draw($state);
open my $TTY, '<', '/dev/tty';
ReadMode('cbreak', $TTY);
$SIG{INT} = sub {
ReadMode('normal', $TTY);
print "\n";
print "\e[?25h "; # reset blinking cursor
exit;
};
my $buffer = '';
while (1) {
my $char = ReadKey(-1, $TTY);
if (!defined $char){
if ($buffer){
my $per;
my $lh = $#lines - $state->{h} + 1;
if ( $buffer == '27' # esc
|| $buffer == '113') { # q
last;
} elsif($buffer == '10') { # up
$state->{selected} = 1;
last;
} elsif($buffer == '279165') { # up
# $state->{'y'}--;
$state->{'cursor'}--;
} elsif($buffer == '279166') { # down
# $state->{'y'}++;
$state->{'cursor'}++;
} elsif($buffer == '279168') { # left
$state->{'x'}-= 4;
} elsif($buffer == '279167') { # right
$state->{'x'}+= 4;
} elsif($buffer == '279153126') { # page up
$state->{'y'}-= $state->{h};
} elsif($buffer == '279154126' # page down
|| $buffer == '32') { # space
$state->{'y'}+= $state->{h};
} elsif ($buffer >= '48' and $buffer <= '57'){ # 1 = 10%, 9 = 90%
$per = $buffer*1 - 48;
if ($per == 0){ $per = 10; }
$per--;
$state->{'y'} = floor($per * $lh / 9);
}
if ($state->{cursor} < 0 ){ $state->{cursor} = 0; }
if ($state->{cursor} > $#matches ){ $state->{cursor} = $#matches; }
# If cursor gets near top or bottom then jump
# TODO
# Don't go out of bounds
if ($state->{x} < 0 ){ $state->{x} = 0; }
if ($state->{y} < 0 ){ $state->{y} = 0; }
if ($state->{y} > $lh){ $state->{y} = $lh; }
draw($state);
print "buffer: '$buffer' $per";
}
$buffer = '';
sleep (0.005);
next;
}
$buffer .= ord($char);
}
print "\n";
ReadMode('normal', $TTY);
print "\e[?25h"; # reset blinking cursor
print "\e[21m"; # not bold
print "\e[39m"; # default fg
print "\e[49m"; # default bg
close $TTY;
if ($state->{selected} == 1){
my @sel = @{ $matches[ $state->{cursor} ] };
my $cmd = "vim +$sel[2] $sel[1] < /dev/tty";
# print $cmd;
print "\e[H"; # reset
print "\e[J"; # clear
exec ($cmd);
}
# TODO
# can we insert the search query into the vim search history so you can just press b to jump to it?
# If less lines than terminal height don't enter interactive mode
# If more lines than say 1000 then stop reading sdtin, flag that there is more, only keep reading if we scroll down that far
# show clean error when no results