teetime: Added --factor and --maxwait.

This commit is contained in:
Ole Tange 2022-11-08 18:10:16 +01:00
parent eeca10eda9
commit c61c402612

View file

@ -11,7 +11,7 @@ teetime - Save stdin including timing
... | teetime [-a] I<file> | ... ... | teetime [-a] I<file> | ...
teetime -i I<file> teetime [-f <factor>] [-m <maxwait>] -i I<file>
=head1 DESCRIPTION =head1 DESCRIPTION
@ -26,6 +26,20 @@ back with the same pauses.
=over 4 =over 4
=item B<--append>
=item B<-a>
Instead of overwriting I<file>, append to I<file>.
=item B<--factor> I<factor>
=item B<-f> I<factor>
Play back I<factor> times faster. 1.0 = actual speed.
=item B<--input> =item B<--input>
=item B<-i> =item B<-i>
@ -33,11 +47,12 @@ back with the same pauses.
Read I<file> as input. Read I<file> as input.
=item B<--append> =item B<--maxwait> I<maxwait>
=item B<-a> =item B<-m> I<maxwait>
Wait at most I<maxwait> seconds.
Instead of overwriting I<file>, append to I<file>.
=back =back
@ -133,13 +148,28 @@ sub readstdin {
close $fh; close $fh;
} }
sub min(@) {
# Returns:
# Minimum value of array
my $min;
for (@_) {
# Skip undefs
defined $_ or next;
defined $min or do { $min = $_; next; }; # Set $_ to the first non-undef
$min = ($min < $_) ? $min : $_;
}
return $min;
}
sub readfile { sub readfile {
my $file = shift; my $file = shift;
open(my $fh, "<", $file) || die; open(my $fh, "<", $file) || die;
while(1) { while(1) {
my $in; my $in;
if(sysread($fh,$in,8)) { if(sysread($fh,$in,8)) {
# time in ms, length in bytes
my ($delta,$length) = unpack("L*",$in); my ($delta,$length) = unpack("L*",$in);
$delta = min($delta/$opt::factor,$opt::maxwait*1000);
select(undef,undef,undef,$delta/1000); select(undef,undef,undef,$delta/1000);
sysread($fh,$in,$length); sysread($fh,$in,$length);
print $in; print $in;
@ -173,10 +203,12 @@ sub help() {
"Usage:", "Usage:",
"", "",
"... | teetime [-a] file | ...", "... | teetime [-a] file | ...",
"teetime -i file", "teetime [-m max] [-f factor] -i file",
"", "",
"-a append to file", "-a append to file",
"-f playback speed",
"-i read from file", "-i read from file",
"-m max wait seconds",
"", "",
"See 'man $Global::progname' for details", "See 'man $Global::progname' for details",
"",); "",);
@ -193,10 +225,12 @@ sub debug {
$|=1; $|=1;
$Global::progname = "teetime"; $Global::progname = "teetime";
$Global::version = "20200721"; $Global::version = "20221108";
if(GetOptions("debug|D=s" => \$opt::D, if(GetOptions("debug|D=s" => \$opt::D,
"append|a" => \$opt::append, "append|a" => \$opt::append,
"factor|f=s" => \$opt::factor,
"input|i" => \$opt::input, "input|i" => \$opt::input,
"maxwait|m=s" => \$opt::maxwait,
"help|h" => \$opt::help, "help|h" => \$opt::help,
"version|V" => \$opt::version)) { "version|V" => \$opt::version)) {
if($opt::help) { if($opt::help) {
@ -208,6 +242,8 @@ if(GetOptions("debug|D=s" => \$opt::D,
exit(1); exit(1);
} }
if($opt::input) { if($opt::input) {
$opt::maxwait ||= 1000000;
$opt::factor ||= 1;
readfile(@ARGV); readfile(@ARGV);
} else { } else {
readstdin(@ARGV); readstdin(@ARGV);