blob: f2c8d79e89fc1f1964bdea9d90783e4dba6ce0b0 [file] [log] [blame]
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00001#
2# Copyright 1999, 2000, 2001 Patrik Stridvall
3#
4# This library is free software; you can redistribute it and/or
5# modify it under the terms of the GNU Lesser General Public
6# License as published by the Free Software Foundation; either
7# version 2.1 of the License, or (at your option) any later version.
8#
9# This library is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12# Lesser General Public License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public
15# License along with this library; if not, write to the Free Software
Jonathan Ernst360a3f92006-05-18 14:49:52 +020016# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000017#
18
Patrik Stridvall6a012e22001-06-13 19:38:29 +000019package output;
20
21use strict;
22
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +000023use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
24require Exporter;
Patrik Stridvall6a012e22001-06-13 19:38:29 +000025
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +000026@ISA = qw(Exporter);
27@EXPORT = qw();
28@EXPORT_OK = qw($output);
Patrik Stridvall6a012e22001-06-13 19:38:29 +000029
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +000030use vars qw($output);
Patrik Stridvall6a012e22001-06-13 19:38:29 +000031
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +000032$output = '_output'->new;
Patrik Stridvall6a012e22001-06-13 19:38:29 +000033
34package _output;
35
36use strict;
37
Patrik Stridvallbda50742001-07-14 00:48:41 +000038my $stdout_isatty = -t STDOUT;
39my $stderr_isatty = -t STDERR;
40
Francois Gougetbac042c2004-10-05 18:08:57 +000041sub new($) {
Patrik Stridvall6a012e22001-06-13 19:38:29 +000042 my $proto = shift;
43 my $class = ref($proto) || $proto;
44 my $self = {};
45 bless ($self, $class);
46
Patrik Stridvall67f0a702001-07-26 21:42:12 +000047 my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
Patrik Stridvall6a012e22001-06-13 19:38:29 +000048 my $progress = \${$self->{PROGRESS}};
49 my $last_progress = \${$self->{LAST_PROGRESS}};
Patrik Stridvalld07a6462001-07-18 20:09:12 +000050 my $last_time = \${$self->{LAST_TIME}};
Patrik Stridvall6a012e22001-06-13 19:38:29 +000051 my $progress_count = \${$self->{PROGRESS_COUNT}};
52 my $prefix = \${$self->{PREFIX}};
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +000053 my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
Patrik Stridvall6a012e22001-06-13 19:38:29 +000054
Patrik Stridvall67f0a702001-07-26 21:42:12 +000055 $$progress_enabled = 1;
Patrik Stridvall6a012e22001-06-13 19:38:29 +000056 $$progress = "";
57 $$last_progress = "";
Patrik Stridvalld07a6462001-07-18 20:09:12 +000058 $$last_time = 0;
Patrik Stridvall6a012e22001-06-13 19:38:29 +000059 $$progress_count = 0;
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +000060 $$prefix = undef;
61 $$prefix_callback = undef;
Patrik Stridvall6a012e22001-06-13 19:38:29 +000062
63 return $self;
64}
65
Patrik Stridvall67f0a702001-07-26 21:42:12 +000066sub DESTROY {
67 my $self = shift;
68
69 $self->hide_progress;
70}
71
Francois Gougetbac042c2004-10-05 18:08:57 +000072sub enable_progress($) {
Patrik Stridvall67f0a702001-07-26 21:42:12 +000073 my $self = shift;
74 my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
75
76 $$progress_enabled = 1;
77}
78
Francois Gougetbac042c2004-10-05 18:08:57 +000079sub disable_progress($) {
Patrik Stridvall67f0a702001-07-26 21:42:12 +000080 my $self = shift;
81 my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
82
83 $$progress_enabled = 0;
84}
85
Francois Gougetbac042c2004-10-05 18:08:57 +000086sub show_progress($) {
Patrik Stridvall6a012e22001-06-13 19:38:29 +000087 my $self = shift;
Patrik Stridvall67f0a702001-07-26 21:42:12 +000088 my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
Francois Gougetbac042c2004-10-05 18:08:57 +000089 my $progress = ${$self->{PROGRESS}};
Patrik Stridvall6a012e22001-06-13 19:38:29 +000090 my $last_progress = \${$self->{LAST_PROGRESS}};
91 my $progress_count = \${$self->{PROGRESS_COUNT}};
92
93 $$progress_count++;
94
Patrik Stridvall67f0a702001-07-26 21:42:12 +000095 if($$progress_enabled) {
96 if($$progress_count > 0 && $$progress && $stderr_isatty) {
Francois Gougetbac042c2004-10-05 18:08:57 +000097 # If progress has more than $columns characters the xterm will
98 # scroll to the next line and our ^H characters will fail to
99 # erase it.
100 my $columns=$ENV{COLUMNS} || 80;
101 $progress = substr $progress,0,($columns-1);
102 print STDERR $progress;
103 $$last_progress = $progress;
Patrik Stridvall67f0a702001-07-26 21:42:12 +0000104 }
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000105 }
106}
107
Francois Gougetbac042c2004-10-05 18:08:57 +0000108sub hide_progress($) {
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000109 my $self = shift;
Patrik Stridvall67f0a702001-07-26 21:42:12 +0000110 my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000111 my $progress = \${$self->{PROGRESS}};
112 my $last_progress = \${$self->{LAST_PROGRESS}};
113 my $progress_count = \${$self->{PROGRESS_COUNT}};
114
115 $$progress_count--;
116
Patrik Stridvall67f0a702001-07-26 21:42:12 +0000117 if($$progress_enabled) {
118 if($$last_progress && $stderr_isatty) {
Francois Gougetbac042c2004-10-05 18:08:57 +0000119 my $message=" " x length($$last_progress);
Patrik Stridvall67f0a702001-07-26 21:42:12 +0000120 print STDERR $message;
121 undef $$last_progress;
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000122 }
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000123 }
124}
125
Francois Gougetbac042c2004-10-05 18:08:57 +0000126sub update_progress($) {
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000127 my $self = shift;
Patrik Stridvall67f0a702001-07-26 21:42:12 +0000128 my $progress_enabled = \${$self->{PROGRESS_ENABLED}};
Francois Gougetbac042c2004-10-05 18:08:57 +0000129 my $progress = ${$self->{PROGRESS}};
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000130 my $last_progress = \${$self->{LAST_PROGRESS}};
Alexandre Julliard7cae5582002-06-01 02:55:48 +0000131
Patrik Stridvall67f0a702001-07-26 21:42:12 +0000132 if($$progress_enabled) {
Francois Gougetbac042c2004-10-05 18:08:57 +0000133 # If progress has more than $columns characters the xterm will
134 # scroll to the next line and our ^H characters will fail to
135 # erase it.
136 my $columns=$ENV{COLUMNS} || 80;
137 $progress = substr $progress,0,($columns-1);
138
Patrik Stridvall67f0a702001-07-26 21:42:12 +0000139 my $prefix = "";
140 my $suffix = "";
141 if($$last_progress) {
Francois Gougetbac042c2004-10-05 18:08:57 +0000142 $prefix = "" x length($$last_progress);
Alexandre Julliard7cae5582002-06-01 02:55:48 +0000143
Francois Gougetbac042c2004-10-05 18:08:57 +0000144 my $diff = length($$last_progress)-length($progress);
Patrik Stridvall67f0a702001-07-26 21:42:12 +0000145 if($diff > 0) {
Francois Gougetbac042c2004-10-05 18:08:57 +0000146 $suffix = (" " x $diff) . ("" x $diff);
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000147 }
148 }
Francois Gougetbac042c2004-10-05 18:08:57 +0000149 print STDERR $prefix, $progress, $suffix;
150 $$last_progress = $progress;
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000151 }
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000152}
153
Francois Gougetbac042c2004-10-05 18:08:57 +0000154sub progress($$) {
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000155 my $self = shift;
156 my $progress = \${$self->{PROGRESS}};
Patrik Stridvalld07a6462001-07-18 20:09:12 +0000157 my $last_time = \${$self->{LAST_TIME}};
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000158
Patrik Stridvalld171a552001-09-10 23:16:05 +0000159 my $new_progress = shift;
160 if(defined($new_progress)) {
161 if(!defined($$progress) || $new_progress ne $$progress) {
162 $$progress = $new_progress;
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000163
Patrik Stridvalld171a552001-09-10 23:16:05 +0000164 $self->update_progress;
165 $$last_time = 0;
166 }
167 } else {
168 return $$progress;
169 }
Patrik Stridvalld07a6462001-07-18 20:09:12 +0000170}
171
Francois Gougetbac042c2004-10-05 18:08:57 +0000172sub lazy_progress($$) {
Patrik Stridvalld07a6462001-07-18 20:09:12 +0000173 my $self = shift;
174 my $progress = \${$self->{PROGRESS}};
175 my $last_time = \${$self->{LAST_TIME}};
176
177 $$progress = shift;
178
179 my $time = time();
180 if($time - $$last_time > 0) {
181 $self->update_progress;
182 $$last_time = $time;
183 }
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000184}
185
Francois Gougetbac042c2004-10-05 18:08:57 +0000186sub prefix($$) {
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000187 my $self = shift;
188 my $prefix = \${$self->{PREFIX}};
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +0000189 my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000190
Patrik Stridvallaeb023f2001-08-22 18:09:15 +0000191 my $new_prefix = shift;
192 if(defined($new_prefix)) {
Patrik Stridvalld171a552001-09-10 23:16:05 +0000193 if(!defined($$prefix) || $new_prefix ne $$prefix) {
194 $$prefix = $new_prefix;
195 $$prefix_callback = undef;
196 }
Patrik Stridvallaeb023f2001-08-22 18:09:15 +0000197 } else {
198 return $$prefix;
199 }
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +0000200}
201
Francois Gougetbac042c2004-10-05 18:08:57 +0000202sub prefix_callback($) {
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +0000203 my $self = shift;
204
205 my $prefix = \${$self->{PREFIX}};
206 my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
207
208 $$prefix = undef;
209 $$prefix_callback = shift;
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000210}
211
Francois Gougetbac042c2004-10-05 18:08:57 +0000212sub write($$) {
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000213 my $self = shift;
214
215 my $message = shift;
216
217 my $prefix = \${$self->{PREFIX}};
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +0000218 my $prefix_callback = \${$self->{PREFIX_CALLBACK}};
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000219
Patrik Stridvallbda50742001-07-14 00:48:41 +0000220 $self->hide_progress if $stdout_isatty;
Patrik Stridvall1c61b3b2001-07-23 23:20:56 +0000221 if(defined($$prefix)) {
222 print $$prefix . $message;
223 } elsif(defined($$prefix_callback)) {
224 print &{$$prefix_callback}() . $message;
225 } else {
226 print $message;
227 }
Patrik Stridvallbda50742001-07-14 00:48:41 +0000228 $self->show_progress if $stdout_isatty;
Patrik Stridvall6a012e22001-06-13 19:38:29 +0000229}
230
2311;