2002-03-10 00:29:33 +01:00
|
|
|
|
#
|
|
|
|
|
# Copyright 1999, 2000, 2001 Patrik Stridvall
|
|
|
|
|
#
|
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
# License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-03-10 00:29:33 +01:00
|
|
|
|
#
|
|
|
|
|
|
2001-06-13 21:38:29 +02:00
|
|
|
|
package output;
|
|
|
|
|
|
|
|
|
|
use strict;
|
2020-07-22 00:10:10 +02:00
|
|
|
|
use warnings 'all';
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
2001-07-24 01:20:56 +02:00
|
|
|
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
|
|
|
|
|
require Exporter;
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
2001-07-24 01:20:56 +02:00
|
|
|
|
@ISA = qw(Exporter);
|
|
|
|
|
@EXPORT = qw();
|
|
|
|
|
@EXPORT_OK = qw($output);
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
2001-07-24 01:20:56 +02:00
|
|
|
|
use vars qw($output);
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
2001-07-24 01:20:56 +02:00
|
|
|
|
$output = '_output'->new;
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
|
|
|
|
package _output;
|
|
|
|
|
|
|
|
|
|
use strict;
|
2020-07-22 00:10:10 +02:00
|
|
|
|
use warnings 'all';
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
2001-07-14 02:48:41 +02:00
|
|
|
|
my $stdout_isatty = -t STDOUT;
|
|
|
|
|
my $stderr_isatty = -t STDERR;
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub new($) {
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $proto = shift;
|
|
|
|
|
my $class = ref($proto) || $proto;
|
|
|
|
|
my $self = {};
|
|
|
|
|
bless ($self, $class);
|
|
|
|
|
|
2001-07-26 23:42:12 +02:00
|
|
|
|
my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $progress = \${$self->{PROGRESS}};
|
|
|
|
|
my $last_progress = \${$self->{LAST_PROGRESS}};
|
2001-07-18 22:09:12 +02:00
|
|
|
|
my $last_time = \${$self->{LAST_TIME}};
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $progress_count = \${$self->{PROGRESS_COUNT}};
|
|
|
|
|
my $prefix = \${$self->{PREFIX}};
|
2001-07-24 01:20:56 +02:00
|
|
|
|
my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
2001-07-26 23:42:12 +02:00
|
|
|
|
$$progress_enabled = 1;
|
2001-06-13 21:38:29 +02:00
|
|
|
|
$$progress = "";
|
|
|
|
|
$$last_progress = "";
|
2001-07-18 22:09:12 +02:00
|
|
|
|
$$last_time = 0;
|
2001-06-13 21:38:29 +02:00
|
|
|
|
$$progress_count = 0;
|
2001-07-24 01:20:56 +02:00
|
|
|
|
$$prefix = undef;
|
|
|
|
|
$$prefix_callback = undef;
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
|
}
|
|
|
|
|
|
2001-07-26 23:42:12 +02:00
|
|
|
|
sub DESTROY {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
$self->hide_progress;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub enable_progress($) {
|
2001-07-26 23:42:12 +02:00
|
|
|
|
my $self = shift;
|
|
|
|
|
my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
|
|
|
|
|
|
|
|
|
|
$$progress_enabled = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub disable_progress($) {
|
2001-07-26 23:42:12 +02:00
|
|
|
|
my $self = shift;
|
|
|
|
|
my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
|
|
|
|
|
|
|
|
|
|
$$progress_enabled = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub show_progress($) {
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $self = shift;
|
2001-07-26 23:42:12 +02:00
|
|
|
|
my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
|
2004-10-05 20:08:57 +02:00
|
|
|
|
my $progress = ${$self->{PROGRESS}};
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $last_progress = \${$self->{LAST_PROGRESS}};
|
|
|
|
|
my $progress_count = \${$self->{PROGRESS_COUNT}};
|
|
|
|
|
|
|
|
|
|
$$progress_count++;
|
|
|
|
|
|
2001-07-26 23:42:12 +02:00
|
|
|
|
if($$progress_enabled) {
|
|
|
|
|
if($$progress_count > 0 && $$progress && $stderr_isatty) {
|
2004-10-05 20:08:57 +02:00
|
|
|
|
# If progress has more than $columns characters the xterm will
|
|
|
|
|
# scroll to the next line and our ^H characters will fail to
|
|
|
|
|
# erase it.
|
|
|
|
|
my $columns=$ENV{COLUMNS} || 80;
|
|
|
|
|
$progress = substr $progress,0,($columns-1);
|
|
|
|
|
print STDERR $progress;
|
|
|
|
|
$$last_progress = $progress;
|
2001-07-26 23:42:12 +02:00
|
|
|
|
}
|
2001-06-13 21:38:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub hide_progress($) {
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $self = shift;
|
2001-07-26 23:42:12 +02:00
|
|
|
|
my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $progress = \${$self->{PROGRESS}};
|
|
|
|
|
my $last_progress = \${$self->{LAST_PROGRESS}};
|
|
|
|
|
my $progress_count = \${$self->{PROGRESS_COUNT}};
|
|
|
|
|
|
|
|
|
|
$$progress_count--;
|
|
|
|
|
|
2001-07-26 23:42:12 +02:00
|
|
|
|
if($$progress_enabled) {
|
|
|
|
|
if($$last_progress && $stderr_isatty) {
|
2004-10-05 20:08:57 +02:00
|
|
|
|
my $message=" " x length($$last_progress);
|
2001-07-26 23:42:12 +02:00
|
|
|
|
print STDERR $message;
|
|
|
|
|
undef $$last_progress;
|
2001-06-13 21:38:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub update_progress($) {
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $self = shift;
|
2001-07-26 23:42:12 +02:00
|
|
|
|
my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
|
2004-10-05 20:08:57 +02:00
|
|
|
|
my $progress = ${$self->{PROGRESS}};
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $last_progress = \${$self->{LAST_PROGRESS}};
|
2002-06-01 04:55:48 +02:00
|
|
|
|
|
2001-07-26 23:42:12 +02:00
|
|
|
|
if($$progress_enabled) {
|
2004-10-05 20:08:57 +02:00
|
|
|
|
# If progress has more than $columns characters the xterm will
|
|
|
|
|
# scroll to the next line and our ^H characters will fail to
|
|
|
|
|
# erase it.
|
|
|
|
|
my $columns=$ENV{COLUMNS} || 80;
|
|
|
|
|
$progress = substr $progress,0,($columns-1);
|
|
|
|
|
|
2001-07-26 23:42:12 +02:00
|
|
|
|
my $prefix = "";
|
|
|
|
|
my $suffix = "";
|
|
|
|
|
if($$last_progress) {
|
2004-10-05 20:08:57 +02:00
|
|
|
|
$prefix = "" x length($$last_progress);
|
2002-06-01 04:55:48 +02:00
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
my $diff = length($$last_progress)-length($progress);
|
2001-07-26 23:42:12 +02:00
|
|
|
|
if($diff > 0) {
|
2004-10-05 20:08:57 +02:00
|
|
|
|
$suffix = (" " x $diff) . ("" x $diff);
|
2001-06-13 21:38:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-10-05 20:08:57 +02:00
|
|
|
|
print STDERR $prefix, $progress, $suffix;
|
|
|
|
|
$$last_progress = $progress;
|
2001-06-13 21:38:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub progress($$) {
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $self = shift;
|
|
|
|
|
my $progress = \${$self->{PROGRESS}};
|
2001-07-18 22:09:12 +02:00
|
|
|
|
my $last_time = \${$self->{LAST_TIME}};
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
2001-09-11 01:16:05 +02:00
|
|
|
|
my $new_progress = shift;
|
|
|
|
|
if(defined($new_progress)) {
|
|
|
|
|
if(!defined($$progress) || $new_progress ne $$progress) {
|
|
|
|
|
$$progress = $new_progress;
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
2001-09-11 01:16:05 +02:00
|
|
|
|
$self->update_progress;
|
|
|
|
|
$$last_time = 0;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return $$progress;
|
|
|
|
|
}
|
2001-07-18 22:09:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub lazy_progress($$) {
|
2001-07-18 22:09:12 +02:00
|
|
|
|
my $self = shift;
|
|
|
|
|
my $progress = \${$self->{PROGRESS}};
|
|
|
|
|
my $last_time = \${$self->{LAST_TIME}};
|
|
|
|
|
|
|
|
|
|
$$progress = shift;
|
|
|
|
|
|
|
|
|
|
my $time = time();
|
|
|
|
|
if($time - $$last_time > 0) {
|
|
|
|
|
$self->update_progress;
|
|
|
|
|
$$last_time = $time;
|
|
|
|
|
}
|
2001-06-13 21:38:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub prefix($$) {
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $self = shift;
|
|
|
|
|
my $prefix = \${$self->{PREFIX}};
|
2001-07-24 01:20:56 +02:00
|
|
|
|
my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
2001-08-22 20:09:15 +02:00
|
|
|
|
my $new_prefix = shift;
|
|
|
|
|
if(defined($new_prefix)) {
|
2001-09-11 01:16:05 +02:00
|
|
|
|
if(!defined($$prefix) || $new_prefix ne $$prefix) {
|
|
|
|
|
$$prefix = $new_prefix;
|
|
|
|
|
$$prefix_callback = undef;
|
|
|
|
|
}
|
2001-08-22 20:09:15 +02:00
|
|
|
|
} else {
|
|
|
|
|
return $$prefix;
|
|
|
|
|
}
|
2001-07-24 01:20:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub prefix_callback($) {
|
2001-07-24 01:20:56 +02:00
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
my $prefix = \${$self->{PREFIX}};
|
|
|
|
|
my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
|
|
|
|
|
|
|
|
|
|
$$prefix = undef;
|
|
|
|
|
$$prefix_callback = shift;
|
2001-06-13 21:38:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2004-10-05 20:08:57 +02:00
|
|
|
|
sub write($$) {
|
2001-06-13 21:38:29 +02:00
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
my $message = shift;
|
|
|
|
|
|
|
|
|
|
my $prefix = \${$self->{PREFIX}};
|
2001-07-24 01:20:56 +02:00
|
|
|
|
my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
2001-07-14 02:48:41 +02:00
|
|
|
|
$self->hide_progress if $stdout_isatty;
|
2001-07-24 01:20:56 +02:00
|
|
|
|
if(defined($$prefix)) {
|
|
|
|
|
print $$prefix . $message;
|
|
|
|
|
} elsif(defined($$prefix_callback)) {
|
|
|
|
|
print &{$$prefix_callback}() . $message;
|
|
|
|
|
} else {
|
|
|
|
|
print $message;
|
|
|
|
|
}
|
2001-07-14 02:48:41 +02:00
|
|
|
|
$self->show_progress if $stdout_isatty;
|
2001-06-13 21:38:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
1;
|