#!/usr/bin/perl
use
Term::ANSIColor
qw[ color :constants ]
;
$Term::ANSIColor::AUTORESET
= 1 ;
sub
sigint () ;
sub
dtfmtlocal () ;
sub
choreOpt () ;
sub
mainCore() ;
sub
y_init () ;
sub
y_filter ($) ;
sub
cyc_rep () ;
my
$printOut
;
my
$prevline
;
my
$count
= 1 ;
my
@y_ranges
;
my
$cyc_len
=
$o
{
'@'
} // 1e5 ;
my
(
$time0
,
$time00
) = (
time
) x 2 ;
y_init ;
choreOpt ;
mainCore ;
sub
mainCore () {
$SIG
{INT} = \
&sigint
;
chomp
(
$prevline
= <> ) ;
$count
= 1 ;
while
( <> ) {
cyc_rep
if
$cyc_len
&& $. %
$cyc_len
== 0 ;
chomp
;
if
(
$prevline
eq
$_
&&
$count
!=
$o
{
'.'
} ) {
$count
++ ;
next
;
}
$printOut
-> ()
if
y_filter (
$count
) ;
$count
= 1 ;
$prevline
=
$_
;
}
$printOut
-> ()
if
y_filter (
$count
) ;
}
sub
choreOpt () {
$| = 1
if
$o
{
'!'
} ;
$o
{
'.'
} //= 0 ;
$o
{
','
} =
do
{
$o
{
','
}//=
"\t"
;
eval
qq[qq[$o{','}]
] } ;
$printOut
=
$o
{0} ?
sub
{
print
"$prevline$/"
} :
$o
{1} ?
sub
{
print
"$count$/"
} :
$o
{
'~'
} ?
sub
{
print
"$prevline$o{','}$count$/"
} :
sub
{
print
"$count$o{','}$prevline$/"
} ;
}
sub
sigint () {
alarm
0 ;
print
STDERR $/ , color(
'yellow'
) ,
"$count+\t$prevline\t"
, dtfmtlocal ,
"($Script)"
,
"$/"
, color(
'reset'
) ;
$SIG
{INT} =
sub
{ sigint ;
die
"`$Script' stopped because of double `Ctrl-C' signals within 1 second.\n"
} ;
alarm
1 ;
$SIG
{ALRM} =
sub
{
$SIG
{ INT } = \
&sigint
} ;
}
sub
dtfmtlocal () {
my
@f
= @{[
localtime
]}[5,4,3,2,1,0] ;
$f
[0] += 1900 ;
$f
[1] += 1 ;
return
sprintf
(
"%04u-%02u-%02u %02u:%02u:%02u"
,
@f
) ;
}
sub
y_init ( ) {
$o
{y} //=
''
;
my
@ranges
=
split
/,/ ,
$o
{y} , -1 ;
grep
{
$_
=
$_
.
".."
.
$_
unless
m/\.\./ }
@ranges
;
for
(
@ranges
) {
m/^(\d*)\.\.(\d*)/ ;
push
@y_ranges
, [ ( $1 || 1 ) , ( $2 ||
"Inf"
) ] ;
}
}
sub
y_filter ( $ ) {
return
not 0
unless
@y_ranges
;
for
(
@y_ranges
) {
return
not 0
if
$_
->[0] <=
$_
[0] &&
$_
[0] <=
$_
->[1] ;
}
return
not 1 ;
}
sub
cyc_rep ( ) {
$| = 1 ;
my
$num
= $. ;
$num
=~ s/(?<=\d)(?=(\d\d\d)+($|\D))/,/g ;
print
STDERR GREEN
$num
,
":\t"
,
sprintf
"%02d:%02d:%02d"
, (
localtime
)[2,1,0] ;
print
STDERR
"\t"
, GREEN
time
-
$time0
,
" sec.\t($Script)"
;
$time0
=
time
;
print
STDERR
"\n"
;
}
sub
VERSION_MESSAGE {}
sub
HELP_MESSAGE {
$ARGV
[1] //=
''
;
open
my
$FH
,
'<'
, $0 ;
while
(<
$FH
>){
s/\$0/
$Script
/g ;
print
$_
if
s/^=head1// .. s/^=cut// and
$ARGV
[1] =~ /^o(p(t(i(o(ns?)?)?)?)?)?$/i ? m/^\s+\-/ : 1;
}
close
$FH
;
exit
0 ;
}