forked from mjpost/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
philog
executable file
·171 lines (156 loc) · 3.58 KB
/
philog
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
#!/usr/bin/perl -w
# Put these in your .bashrc
#
# # print the saved commands
# alias p="philog"
# alias pp="philog -pretty"
# alias ppd="philog -pretty -dir"
# alias pd="philog -dir"
# alias pr="philog -rerun"
#
# # edit the command cache
# alias pe="vi .philog.LOGFILE"
#
# # save the last command
# alias ph="history | philog -history"
use strict;
use warnings;
# Save logfile in directory instead of globally
my $logfile = "./.philog.LOGFILE";
my $DATE_FORMAT = "%Y-%m-%d %H:%M:%S";
chomp(my $date = `date +"$DATE_FORMAT"`);
my $pwd = `pwd`; chop($pwd);
my $pretty = 0;
my $curdir = 0;
my $clean = 0;
my $history = 0;
my $rerun = -1;
for (my $i = 0; $i < @ARGV; $i++) {
$_ = $ARGV[$i];
$pretty = 1 if /^-pretty$/;
$curdir = 1 if /^-dir$/;
$clean = 1 if /^-clean$/;
$history = 1 if /^-history$/;
$rerun = $ARGV[$i+1] || 0 if /^-rerun$/;
}
if ($history) {
my @cmd = <STDIN>;
my $last = $ENV{"SHELL"} eq "/bin/zsh" ? 0 : 1;
$last = $ARGV[1] if $#ARGV>0 && $ARGV[1] =~ /^\d+$/;
my $cmd = $cmd[$#cmd-$last];
$cmd =~ s/^\s*\d+\s*//;
$cmd =~ s/^[\d\-]+ [\d\:]+\s*//;
$cmd =~ s/^#\s*//; # remove comment
chomp($cmd);
&log($cmd);
}
elsif ($rerun >= 0) {
upgrade_log($logfile);
die "What do you mean, rerun the 0th command?" unless $rerun > 0;
open(LOG, $logfile);
while(<LOG>) {
if ($. == $rerun) {
/^(....-..-.. ..:..:..) (\S+) (.+)$/;
my ($time,$dir,$cmd) = ($1,$2,$3);
chomp($cmd);
print STDERR "Rerunning $cmd\n";
exit system($cmd);
}
}
print STDERR "There weren't $rerun commands in the history\n";
}
elsif ($#ARGV-$pretty-$clean == -1) {
upgrade_log($logfile);
open(LOG, $logfile);
while(<LOG>) {
/^(....-..-.. ..:..:..) (\S+) (.+)$/;
my ($time,$dir,$cmd) = ($1,$2,$3);
if ($pretty) {
print "$.. \e[32m$time \e[34m$dir ".($cmd=~/^[\#\^]/?"\e[30;1m":"\e[m").$cmd."\e[m\n";
}
elsif ($clean) {
print "$.. $cmd\n";
}
else {
print "$.. $time $dir $cmd\n";
}
}
close(LOG);
}
elsif ($curdir) {
upgrade_log($logfile);
if (! open(LOG,$logfile)) {
print "No logfile found in current directory.\n";
exit 0;
}
while(<LOG>) {
/^(....-..-.. ..:..:..) (\S+) (.+)$/;
my ($time,$dir,$cmd) = ($1,$2,$3);
if ($pretty) {
print "$.. \e[32m$time ".($cmd=~/^[\#\^]/?"\e[30;1m":"\e[m").$cmd."\e[m\n";
}
elsif ($clean) {
print "$.. $cmd\n";
}
else {
print "$.. $1 $3\n";
}
}
close(LOG);
}
else {
my $cmd;
foreach (@ARGV) { $cmd .= "$_ "; }
chop($cmd);
&log($cmd);
}
sub log {
my ($cmd) = @_;
open(LOG,">>$logfile");
print LOG "$date $pwd $cmd\n";
close(LOG);
}
sub same_dir {
my ($a,$b) = @_;
$a =~ s/nfs\/[^\/]+/nfs/;
$b =~ s/nfs\/[^\/]+/nfs/;
return $a eq $b;
}
sub upgrade_log {
my ($file) = @_;
if (! -e $file) {
return;
}
my %MONTHS = (
"Jan" => 1,
"Feb" => 2,
"Mar" => 3,
"Apr" => 4,
"May" => 5,
"Jun" => 6,
"Jul" => 7,
"Aug" => 8,
"Sep" => 9,
"Oct" => 10,
"Nov" => 11,
"Dec" => 12 );
my @entries = `cat $file`;
if ($entries[0] !~ /^\d{4}/) { # convert old format to new
print STDERR "Upgrading logfile $file\n";
system("cp $file $file.bak") unless -e "$file.bak";
open(LOG, ">$file");
foreach my $entry (@entries) {
chomp($entry);
# Thu Oct 3 15:01:32 EDT 2019 cd
$entry =~ /^... (...) (..) (..:..:..) ....? (20..) (\S+) (.+)$/;
my $month = sprintf("%02d", $MONTHS{$1});
my $date = sprintf("%02d", $2);
my $time = $3;
my $year = $4;
my $dir = $5;
my $cmd = $6;
print LOG "$year-$month-$date $time $dir $cmd\n";
}
close(LOG);
}
}