Last active
April 19, 2018 10:53
-
-
Save mlampret/14f7a1e076e1e23fdf3b811e5c11799f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Data::Dumper; | |
use Term::ANSIColor; | |
{ | |
# args | |
my $action = ''; | |
my @args = map { $_ =~ s!::!/!g; $_ } @ARGV; | |
if ($ARGV[-1] =~ m/^(\d+|!|\?|[a-z])$/i) { | |
$action = $ARGV[-1]; | |
# fix ? terminal conversion | |
$action = '?' if $action =~ m/^[a-z]$/i; | |
pop @args; | |
} | |
# cmd | |
my $cmd = | |
join ' | ', | |
map { | |
my $p = $_; | |
my $invert = ($p =~ m/^\-/) ? '-v' : ''; | |
$p =~ s/^\-// if $invert; | |
my $ignore_case = lc $p eq $p ? '-i' : ''; | |
"ack $invert $ignore_case \"$p\""; | |
} | |
@args; | |
$cmd =~ s/^ack /ack -g /; | |
$cmd =~ s/\s+/ /g; | |
# search | |
my @files = grep { -f } sort split /\n/, `$cmd`; | |
$action = 1 if (scalar @files) == 1 && $action ne '?'; | |
# open | |
if (scalar @files && $action ne '?') { | |
if ($action =~ m/^\d+$/) { | |
system('joe', $files[$action - 1]); | |
} elsif (scalar @files <= 1 || $action eq '!') { | |
system('joe', @files); | |
} | |
} | |
# output | |
print "\n$cmd\n\n"; | |
if (scalar @files) { | |
print "Found:\n"; | |
my $i = 1; | |
for my $file (@files) { | |
no warnings; | |
my $selected = ($i == $action || $action eq '!') ? '>' : ' '; | |
use warnings; | |
for my $search (@args) { | |
$file =~ s/($search)/color('GREEN').$1.color('RESET')/gie; | |
} | |
color('BRIGHT_BLACK') | |
."$selected" | |
.sprintf("%2d", $i) | |
.color('RESET') | |
." $file\n"; | |
$i++; | |
} | |
} else { | |
print "No files found\n"; | |
} | |
print "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment