forked from woodpeck/osm-revert-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgress.pm
108 lines (91 loc) · 2.51 KB
/
Progress.pm
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
#!/usr/bin/perl
# Progress.pm
# -----------
#
# Simple progress bar that doesn't require a fuckton of modules.
#
# Part of the "osmtools" suite of programs
# Originally written by Frederik Ramm <[email protected]>; public domain
package Progress;
use strict;
use warnings;
# number of equal signs to use in the 100% progress bar
# (complete display line will be ~20 chars longer)
our $width = 50;
# no user servicable parts below
our $start_time = 0;
our $total = 0;
our $current;
our $last_update = 0;
our $last_bars = 0;
our $numwidth = 0;
BEGIN
{
# this requires unbuffered output
$| = 1;
}
# -----------------------------------------------------------------------------
# initialises progress bar
# Parameters: total number of operations (what is 100%)
sub init($)
{
$start_time = time();
$total = shift;
$numwidth = length($total);
}
# -----------------------------------------------------------------------------
# updates progress bar
# Parameters: current number (-1 = 100%)
sub update($)
{
$current = shift;
$current = $total if ($current<0);
my $now = time();
my $bars = int($current * $width / $total);
my $eta = "(wait)";
my $elapsed = ($now - $start_time);
if ($elapsed > 20)
{
my $remain = $elapsed / $current * $total - $elapsed;
$eta = "";
$eta = sprintf "%d:", $remain/3600 if ($remain>=3600);
$eta .= sprintf "%02d:%02d ", ($remain%3600) / 60, $remain % 60;
# need this to clean up stray characters after hour goes away
$eta .= " " unless ($remain>=3600);
}
if (($now - $last_update >= 1) || ($bars > $last_bars))
{
printf "[%*d/%*d] [%*s] ETA %s\r",
$numwidth, $current,
$numwidth, $total,
-$width-1, '=' x $bars . '>',
$eta;
}
$last_bars = $bars;
$last_update = $now;
}
# -----------------------------------------------------------------------------
# outputs a log message without breaking the progress bar
# Parameters: message
sub log($)
{
my $msg = shift;
if (!defined($current))
{
print STDERR "$msg\n";
return;
}
printf STDERR "%*s\n", -(20 + 2 * $numwidth + $width), $msg;
$last_update = 0;
update($current);
}
# -----------------------------------------------------------------------------
# outputs a log message without breaking the progress bar, then ends the program
# Parameters: message
sub die($)
{
my $msg = shift;
print STDERR "\n" if (defined($current));
die($msg);
}
1;