Skip to content

Commit 4ae11cc

Browse files
author
H.Merijn Brand
committed
Show/grep log util
1 parent bb65f2b commit 4ae11cc

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

pm-log.pl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/pro/bin/perl
2+
3+
use 5.14.2;
4+
use warnings;
5+
use Term::ANSIColor;
6+
7+
my $pat = shift // ".";
8+
9+
my %conf;
10+
my $home = $ENV{HOME} || $ENV{USERPROFILE} || $ENV{HOMEPATH};
11+
foreach my $rcf (grep { -s }
12+
"$home/pm-cb.rc", "$home/.pm-cbrc", "$home/.config/pm-cb") {
13+
my $mode = (stat $rcf)[2];
14+
$mode & 022 and next;
15+
open my $fh, "<", $rcf or next;
16+
while (<$fh>) {
17+
m/^\s*[;#]/ and next;
18+
$mode & 044 && m/password/i and next;
19+
my ($k, $v) = (m/^\s*([-\w]+)\s*[:=]\s*(.*\S)/) or next;
20+
$conf{ lc $k
21+
=~ s{-}{_}gr
22+
=~ s{[-_]colou?r$}{_color}ir
23+
=~ s{background}{bg}ir
24+
=~ s{foreground}{fg}ir
25+
=~ s{^(?:unicode|utf-?8?)$}{utf8}ir
26+
=~ s{^use_}{}ir
27+
=~ s{font_size}{char_size}ir
28+
=~ s{font_family}{char_name}ir
29+
=~ s{show_time(?:stamps?)}{show_time}ir
30+
=~ s{copy_url$}{copy_link}ir
31+
} = $v
32+
=~ s{U\+?([0-9A-Fa-f]{2,7})}{chr hex $1}ger
33+
=~ s{^(?:no|false)$}{0}ir
34+
=~ s{^(?:yes|true)$}{1}ir;
35+
}
36+
}
37+
exists $conf{show_time} and $conf{no_time} = !delete $conf{show_time};
38+
$conf{font_name} =~ m/\s/ and $conf{font_name} = "{".$conf{font_name}."}";
39+
$conf{copy_link} =~ s{^<*(.*?)>*$}{<$1>};
40+
41+
my $ct = color ("grey15"); # color ($conf{time_color} || "darkcyan") || color ("grey15");
42+
my $ca = color ("bright_blue"); # color ($conf{author_color} || "blue") || color ("bright_blue");
43+
my $cu = color ("red"); # color ($conf{self_color} || "red") || color ("red");
44+
my $cr = color ("reset");
45+
46+
my $user = lc ($conf{username} || $ENV{logname});
47+
48+
if (my $hf = $conf{history_file}) {
49+
$hf =~ s/~/$ENV{HOME}/;
50+
if (open my $fh, '<:encoding(utf-8)', $hf) {
51+
local $/ = "\x{2028}";
52+
chomp (my @hist = <$fh>);
53+
for (grep m/$pat/i => @hist) {
54+
my ($time, $author, $msg) = split m/\x{2063}/ => $_;
55+
$msg =~ s/[\r\n\s]*\z//;
56+
my $dc = $user eq lc $author =~ s/^\W+//r =~ s/\W+$//r ? $cu : $ca;
57+
say $ct, $time, $dc, $author, $cr, $msg;
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)