-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash-eternal-history-search
executable file
·120 lines (96 loc) · 3.04 KB
/
bash-eternal-history-search
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
#!/usr/bin/env perl
# Search specially formatted bash history
use strict;
use warnings;
no warnings 'uninitialized';
use Getopt::Long;
use Cwd;
my $gray = "\x1b[38;5;243m";
my $reset_color = "\x1b[38;5;0m";
my $red = "\x1b[38;5;124m";
my $pipe_mode = !-t STDOUT;
if ($pipe_mode) {
$gray = $reset_color = $red = "";
}
my $opts = {
"a|all" => \my $show_all,
"e|everything" => \my $show_everything,
"existing-only" => \my $show_existing_only,
"skip-current-dir" => \my $skip_current,
"d|directories" => \my $show_dirs_only,
"files" => \my $find_files,
"s|search-directories" => \my $search_dirs,
"r|succsessful-result-only" => \my $show_successful,
"l|commands-here" => \my $show_local,
"c|count=i" => \my $count,
};
GetOptions(%$opts) or die "Usage:\n" . join( "\n", sort keys %$opts ) . "\n";
my @search = @ARGV;
my $wd = cwd;
# user 2011-08-20 21:02:47 19202 "dir" "0 1" cmd with options ...
# usr date time pid dir exit codes cmd
my $hist_regex = '^(.+?) (.+?) (.+?) (\d*?) "(.+?)" "([\d ]+?)" (.+)$';
my $h = $ENV{HISTFILE_ETERNAL};
open( F, "tac $h |" ) || die $!;
my %shown = ();
$count ||= 100;
my @to_show = ();
ENTRY: while (<F>) {
my (@all) = $_ =~ /$hist_regex/g;
my ( $user, $date, $time, $pid, $dir, $result, $cmd ) = @all;
my $was_successful = $result =~ /[0 ]+/g;
next if $show_successful && !$was_successful;
next if $dir ne $wd && $show_local;
my $to_match;
my $show;
if ($show_dirs_only) {
next if $show_existing_only && !-d $dir;
$to_match = $dir;
$show = $dir;
$show_everything = 0;
}
else {
$to_match = $cmd;
$show = $cmd;
$show = $red . $show . $reset_color if !$was_successful;
}
if ($search_dirs) {
$to_match .= " $dir";
}
if (@search) {
foreach my $search (@search) {
next ENTRY if $to_match !~ /$search/i;
my $found;
if ($find_files) {
foreach ( split( " ", $cmd ) ) {
if (/^\//) {
}
elsif (/^~/) {
s/^~/$ENV{HOME}/g;
}
else {
$_ = "$dir/$_";
}
next if $_ !~ /$search/i;
next if !-f $_;
$found = $_;
last;
}
next ENTRY if !$found;
$to_match = $found;
$show = $found;
}
}
}
next if exists $shown{$to_match};
$shown{$to_match} = 1;
$show .= "\n";
$show
.= $gray . " ("
. join( " ", @all[ 0 .. $#all - 1 ] ) . ")\n"
. $reset_color
if $show_everything;
push( @to_show, $show );
last if !$show_all && keys %shown == $count;
}
map { print $_ } reverse @to_show;