-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfancy2
More file actions
executable file
·118 lines (110 loc) · 2.8 KB
/
Copy pathfancy2
File metadata and controls
executable file
·118 lines (110 loc) · 2.8 KB
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
#!/usr/bin/env perl
use warnings;
use strict;
use Getopt::Long qw( GetOptions );
my $Delay = undef;
my $Timestamp = 0;
my $Count = 1;
my $Width = `tput cols`;
my $Height = `tput lines`;
my $Winch = undef;
my @Buffer = ();
my $Date_Fmt1 = qr{\w+ +\d+ \d\d:\d\d:\d\d};
my $Date_Fmt2 = qr{\d{4}-\d\d-\d\d \d\d:\d\d:\d\d \S+};
my $Date_Fmt3 = qr{\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:[.]\d+)?-\d\d:\d\d};
my $Date_Re = qr{$Date_Fmt1|$Date_Fmt2|$Date_Fmt3};
local $SIG{WINCH} = sub { $Winch = 1 };
my $r = GetOptions( 'n=i' => \$Delay );
die "Usage: fancy2 [ -n <seconds> ]\n" if ( not $r );
my $fh = *STDIN;
$Delay //= 0;
draw($fh);
sub draw {
my $fh = shift;
while ( my $line = <$fh> ) {
if ($Delay) {
buffer($line);
}
else {
fancify($line);
}
}
return;
}
sub buffer {
my $line = shift;
my $flush = undef;
my $now = time;
if ($Winch) {
$Winch = undef;
$Width = `tput cols`;
$Height = `tput lines`;
}
my $span = int( length($line) / $Width + 1 );
$Count += $span;
if ( $now - $Timestamp > $Delay or $Count >= $Height ) {
$flush = $now - $Timestamp;
}
if ($flush) {
$Count = $span;
$Timestamp = $now;
fancify($_) for (@Buffer);
@Buffer = ();
}
else {
push @Buffer, $line;
}
return;
}
sub fancify {
my $line = shift;
$line =~ s{#033\[}{\e[}g;
$line =~ s{\\x2d}{-}g;
chomp $line;
if ( $line =~ m{^($Date_Re) (\S+) (.*?:) (.*)$} ) {
my ( $date, $host, $process, $msg ) = ( $1, $2, $3, $4 );
my $color = get_color( \$msg );
printf "\e[95m%s \e[96m%s \e[94m%s $color%s\e[0m\n",
$date, $host, $process, $msg;
}
elsif ( $line =~ m{^($Date_Re) (\S+) (.*)$} ) {
my ( $date, $host, $msg ) = ( $1, $2, $3 );
my $color = get_color( \$msg );
printf "\e[95m%s \e[96m%s $color%s\e[0m\n", $date, $host, $msg;
}
else {
my $color = get_color( \$line );
print $color, $line, "\e[0m\n";
}
return;
}
sub get_color {
my $line = shift;
my $color = undef;
if ( $$line =~ m{ERROR:} ) {
#$color = "\e[30;101m";
$color = "\e[91m";
}
elsif ( $$line =~ m{WARNING:} ) {
#$color = "\e[30;103m";
$color = "\e[93m";
}
elsif ( $$line =~ m{INFO:|DEBUG:|systemd[[]} ) {
$color = "\e[90m";
}
elsif ( $$line =~ m{auth|ssh|sftp}i ) {
$color = "\e[92m";
$$line
=~ s{\b((?:user|login by|publickey for)[ :=]+)([^\s\]]+)}{$1\e[91m$2$color}i;
}
elsif ( $$line =~ m{fail|error}i ) {
$color = "\e[91m";
}
elsif ( $$line =~ m{warning}i ) {
$color = "\e[93m";
}
else {
$color = "\e[0m";
}
return $color;
}