summaryrefslogtreecommitdiff
path: root/support/autolatex/pm/AutoLaTeX/Core/Progress.pm
blob: 1e7b27ed9ee038962ff1fb02c3bf0950a421c4fc (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# autolatex - Progress.pm
# Copyright (C) 2013  Stephane Galland <galland@arakhne.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

=pod

=head1 NAME

Progress.pm - Implementation of a progress indicator

=head1 DESCRIPTION

Provides a tool to show the progress of the tasks.

To use this library, type C<use AutoLaTeX::Core::Progress;>.

=head1 GETTING STARTED

=head2 Initialization

To create a progress tool, say something like this:

    use AutoLaTeX::Core::Progress;

    my $max = 100;
    my $progress = AutoLaTeX::Core::Progress->new($max) ;

...or something similar.

=head1 METHOD DESCRIPTIONS

This section contains only the methods in Progress.pm itself.

=over

=cut
package AutoLaTeX::Core::Progress;

our @ISA = qw( Exporter );
our @EXPORT = qw( );
our @EXPORT_OK = qw();

require 5.014;
use strict;
use utf8;
use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
use Exporter;
use Carp;
use AutoLaTeX::Core::IntUtils;
use AutoLaTeX::Core::Util qw($INTERNAL_MESSAGE_PREFIX);

our $VERSION = '4.0';

#------------------------------------------------------
#
# Constructor
#
#------------------------------------------------------

sub new(;$) : method {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $parent = ref($proto) && $proto ;
	my $max = $_[0];
	if (!defined($max) || $max<0) {
		$max = 100;
	}
	my $self;
	if ( $parent ) {
		%{$self} = %{$parent} ;
	}
	else {
		$self = {
			'child' => undef,
			'max' => $max,
			'value' => 0,
			'parent' => undef,
			'bar-width' => 30,
			'comment' => '',
			'comment-to-display' => '',
			'previous-message-size' => 0,
			'carriage-return' => 1,
		};
	}
	bless( $self, $class );

	return $self;
}

sub _newChild($$$) : method {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $parent = shift;
	my $min = shift;
	my $max = shift;
	my $self = {
		'child' => undef,
		'value' => 0,
		'parent' => $parent,
		'min-in-parent' => $min,
		'max-in-parent' => $max,
		'max' => 0,
		'comment' => '',
	};
	bless( $self, $class );
	return $self;
}

=pod

=item * setCarriageReturn($)

Enable or disable the use of the carraige-return character
C<\r> at the end of the lines. If the carriage-return
character is not used, the new-line character C<\n> is
used.

=over 4

=item B<use_carriage_return>

=over

=cut
sub setCarriageReturn($) : method {
	my $self = shift;
	$self->{'carriage-return'} = shift;
}

=pod

=item * getCarriageReturn()

Replies if the carriage-return character is used at the end
of the output lines.

=cut
sub getCarriageReturn() : method {
	my $self = shift;
	return $self->{'carriage-return'};
}

=pod

=item * setBarWidth($)

Set the number of characters for rendering the progress bar.

=over 4

=item B<width> is the number of characters of the bar.

=over

=cut
sub setBarWidth($) : method {
	my $self = shift;
	my $width = shift;
	if ($self->{'parent'}) {
		$self->{'parent'}->setBarWidth($width);
	}
	else {
		$self->{'bar-width'} = $width;
	}
}

=pod

=item * getBarWidth()

Replies the number of characters for rendering the progress bar.

=cut
sub getBarWidth() : method {
	my $self = shift;
	if ($self->{'parent'}) {
		return $self->{'parent'}->getBarWidth();
	}
	else {
		return $self->{'bar-width'};
	}
}

=pod

=item * setComment($)

Set the comment associated to the progress process.

=over 4

=item B<label> is the comment text.

=over

=cut
sub setComment($) : method {
	my $self = shift;
	my $label = shift || '';
	$self->{'comment'} = $label || '';
	my $c = '';
	my $p = $self;
	while ($p->{'parent'}) {
		if (!$c && $p->{'comment'}) {
			$c = $p->{'comment'};
		}
		$p = $p->{'parent'};
	}
	if ($p && $p->{'comment-to-display'} ne $c) {
		$p->{'comment-to-display'} = $c;
		$p->_report();
		return 1;
	}
	return 0;
}

=pod

=item * getComment()

Replies the comment in the progress bar.

=cut
sub getComment() : method {
	my $self = shift;
	if ($self->{'parent'}) {
		return $self->{'parent'}->getComment();
	}
	else {
		return $self->{'comment-to-display'};
	}
}

=pod

=item * setMax()

Set the maximal value. It must be greater than the previous
value of max. This function does not output on the console.

=cut
sub setMax($) : method {
	my $self = shift;
	my $max = shift;
	if ($max>$self->{'max'}) {
		$self->{'max'} = $max;
	}
}

=pod

=item * getMax()

Replies the maximal value.

=cut
sub getMax() : method {
	my $self = shift;
	return $self->{'max'};
}

=pod

=item * getValue()

Replies the current value.

=cut
sub getValue() : method {
	my $self = shift;
	return $self->{'value'};
}

=pod

=item * setValue($)

Change the progress value and display the progress message.

=over 4

=item B<value> is the current value of the progress indicator.

=back

Returns a boolean value that indicates if something was setValueed.

=cut
sub setValue($;$) : method {
	my $self = shift;
	my $value = shift;
	my $comment = shift;
	my $max = $self->getMax();
	my $currentValue = $self->getValue();
	my $reported = undef;
	confess('undef $value') unless (defined($value));
	if ($value>$currentValue) {
		$self->_set_value($value);
		$currentValue = $self->getValue();
		# Close any child progress
		if ($self->{'child'}) {
			my $mip = $self->{'child'}{'max-in-parent'};
			if ($currentValue>=$mip) {
				$reported = $self->_disconnectChildProgress();
			}
		}
		# Notify parent
		if ($self->{'parent'}) {
			my $range = $self->{'max-in-parent'} - $self->{'min-in-parent'};
			my $parent_value = ($value * $range) / $max;
			$parent_value += $self->{'min-in-parent'};
			if (!defined($comment) && $self->{'comment'}) {
				$comment = $self->{'comment'};
			}
			my $reported2 = $self->{'parent'}->setValue($parent_value, $comment);
			$reported = $reported || $reported2;
		}
		# Change the comment to be displayed
		elsif (defined($comment) && $comment ne $self->{'comment-to-display'}) {
			$self->{'comment-to-display'} = $comment;
			$reported = undef;
		}
		# Force reporting
		if (!$reported) {
			$reported = $self->_report();
		}
	}
	return $reported ;
}

sub _set_value($) : method {
	my $self = shift;
	my $value = shift;
	if ($value>=$self->{'max'}) {
		$self->{'value'} = $self->{'max'};
	}
	else {
		$self->{'value'} = $value;
	}
}

sub _report() : method {
	my $self = shift;
	my $value = $self->getValue();
	my $max = $self->getMax();
	if (!$self->{'parent'}) {
		my $message = "[".$self->_formatPercent($value, $max)."] ".$self->_formatBar($value, $max);
		if ($self->{'comment-to-display'}) {
			$message .= ' '.$self->{'comment-to-display'};
		}
		my $l = length($message);
		my $tmp_l = $l;
		while ($tmp_l<$self->{'previous-message-size'}) {
			$message .= ' ';
			$tmp_l++;
		}
		$self->{'previous-message-size'} = $l;
		if (!$self->{'carriage-return'} || ($value>=$max)) {
			print STDOUT "$message\n";
		}
		else {
			print STDOUT "$message\r";
		}
		$INTERNAL_MESSAGE_PREFIX = "\n";
		return 1;
	}
	return undef;
}

sub _formatPercent($$) : method {
	my $self = shift;
	my $value = shift;
	my $max = shift;
	my $percent = int(($value * 100) / $max);
	while (length($percent)<3) {
		$percent = " $percent";
	}
	return "$percent\%";
}

sub _formatBar($$) : method {
	my $self = shift;
	my $value = shift;
	my $max = shift;
	my $bar_width = $self->getBarWidth();
	my $nchars = int(($value * $bar_width) / $max);
	my $i = 0;
	my $bar = '';
	while ($i<$nchars) {
		$bar .= '#';
		$i++;
	}
	while ($i<$bar_width) {
		$bar .= '.';
		$i++;
	}
	return $bar;
}

sub _disconnectChildProgress() : method {
	my $self = shift;
	if ($self->{'child'}) {
		my $max_in_parent = $self->{'child'}{'max-in-parent'};
		$self->{'child'} = undef;
		return $self->setValue($max_in_parent);
	}
	return undef;
}

=pod

=item * subProgress($$)

Create a subtask.

=over 4

=item B<size> (optional) is the size of the subtask in this progress. If not given, the rest of the parent task is covered by the sub task.

=back

Replies the subtask progress object.

=cut
sub subProgress(;$) : method {
	my $self = shift;
	my $size = shift;

	my $parent_max = $self->getMax();
	my $min_in_parent = $self->getValue();

	if (!defined($size) || $size<0) {
		$size = $parent_max - $min_in_parent;
	}

	my $max_in_parent = $min_in_parent + $size;
	if ($max_in_parent>$parent_max) {
		$max_in_parent = $parent_max;
	}

	$self->{'child'} = AutoLaTeX::Core::Progress->_newChild(
				$self,
				$min_in_parent,
				$max_in_parent);

	return $self->{'child'};
}

=pod

=item * increment(;$)

Increment the current value by the given amount, or by 1 if the amount is not given or invalid.

=cut
sub increment(;$) : method {
	my $self = shift;
	my $inc = shift;
	if (!defined($inc) || $inc<=0) {
		$inc = 1;
	}
	my $value = $self->getValue();
	$self->setValue($value+$inc);
}

=pod

=item * stop()

Stop the progress.

=cut
sub stop() : method {
	my $self = shift;
	my $max = $self->getMax();
	$self->setValue($max);
}

=pod

=item * debug()

Output the state of this progress.

=cut
sub debug(;$) : method {
	my $self = shift;
	my $level = shift || 0;
	printf STDERR ("%s[\%3d] value: 0<=\%f<=\%f\n", $INTERNAL_MESSAGE_PREFIX, $level, $self->{'value'}, $self->{'max'});
	$INTERNAL_MESSAGE_PREFIX = '';
	if ($self->{'parent'}) {
		my $parent_value = $self->{'parent'}->getValue();
		printf STDERR ("[\%3d] in-parent: \%f<=%f<=\%f\n", $level, $self->{'min-in-parent'}, $parent_value, $self->{'max-in-parent'});
	}
	else {
		printf STDERR ("[\%3d] comment: \%s\n", $level, $self->{'comment'});
	}
	if ($self->{'child'}) {
		$self->{'child'}->debug($level+1);
	}
}

1;
__END__
=back

=head1 BUG REPORT AND FEEDBACK

To report bug, provide feedback, suggest new features, etc. visit the AutoLaTeX Project management page at <http://www.arakhne.org/autolatex/> or send email to the author at L<galland@arakhne.org>.

=head1 LICENSE

S<GNU Public License (GPL)>

=head1 COPYRIGHT

S<Copyright (c) 2013 Stéphane Galland E<lt>galland@arakhne.orgE<gt>>

=head1 SEE ALSO

L<autolatex-dev>