line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Term::Pulse; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21789
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
102
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Term::Pulse - show pulsed progress bar in terminal |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.05 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
17
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
18
|
|
|
|
|
|
|
our @EXPORT = qw(pulse_start pulse_stop); |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
2486
|
use Time::HiRes qw(usleep time); |
|
1
|
|
|
|
|
1930
|
|
|
1
|
|
|
|
|
4
|
|
21
|
|
|
|
|
|
|
require Exporter; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 SYNOPSIS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use Term::Pulse; |
26
|
|
|
|
|
|
|
pulse_start( name => 'Checking', rotate => 0, time => 1 ); # start the pulse |
27
|
|
|
|
|
|
|
sleep 3; |
28
|
|
|
|
|
|
|
pulse_stop() # stop it |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 EXPORT |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
The following functions are exported by default. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 pulse_start() |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 pulse_stop() |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 FUNCTIONS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 pulse_start() |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Use this functions to start the pulse. Accept the following arguments: |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=over |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item name |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
A simple message displayed before the pulse. The default value is 'Working'. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=item rotate |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Boolean. Rotate the pulse if set to 1. Turn off by default. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item time |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Boolean. Display the elapsed time if set to 1. Turn off by default. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item size |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Set the pulse size. The default value is 16. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=back |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $pid; |
67
|
|
|
|
|
|
|
my $global_name; |
68
|
|
|
|
|
|
|
my $global_start_time; |
69
|
|
|
|
|
|
|
my @mark = qw(- \ | / - \ | /); |
70
|
|
|
|
|
|
|
$| = 1; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub pulse_start { |
73
|
0
|
|
|
0
|
1
|
0
|
my %args = @_; |
74
|
0
|
0
|
|
|
|
0
|
my $name = defined $args{name} ? $args{name} : 'Working'; |
75
|
0
|
0
|
|
|
|
0
|
my $rotate = defined $args{rotate} ? $args{rotate} : 0; |
76
|
0
|
0
|
|
|
|
0
|
my $size = defined $args{size} ? $args{size} : 16; |
77
|
0
|
0
|
|
|
|
0
|
my $time = defined $args{time} ? $args{time} : 0; |
78
|
0
|
|
|
|
|
0
|
my $start = time; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
0
|
$global_start_time = $start; |
81
|
0
|
|
|
|
|
0
|
$global_name = $name; |
82
|
0
|
0
|
|
|
|
0
|
$pid = fork and return; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
0
|
while (1) { |
85
|
|
|
|
|
|
|
# forward |
86
|
0
|
|
|
|
|
0
|
foreach my $index (1..$size) { |
87
|
0
|
0
|
|
|
|
0
|
my $mark = $rotate ? $mark[$index % 8] : q{=}; |
88
|
0
|
|
|
|
|
0
|
printf "$name...[%s%s%s]", q{ } x ($index - 1), $mark, q{ } x ($size - $index); |
89
|
0
|
0
|
|
|
|
0
|
printf " (%f sec elapsed)", (time - $start) if $time; |
90
|
0
|
|
|
|
|
0
|
printf "\r"; |
91
|
0
|
|
|
|
|
0
|
usleep 200000; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# backward |
95
|
0
|
|
|
|
|
0
|
foreach my $index (1..$size) { |
96
|
0
|
0
|
|
|
|
0
|
my $mark = $rotate ? $mark[($index % 8) * -1] : q{=}; |
97
|
0
|
|
|
|
|
0
|
printf "$name...[%s%s%s]", q{ } x ($size - $index), $mark, q{ } x ($index - 1); |
98
|
0
|
0
|
|
|
|
0
|
printf " (%f sec elapsed)" , (time - $start ) if $time; |
99
|
0
|
|
|
|
|
0
|
printf "\r"; |
100
|
0
|
|
|
|
|
0
|
usleep 200000; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 pulse_stop() |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Stop the pulse and return elapsed time |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub pulse_stop { |
112
|
2
|
|
|
2
|
1
|
42
|
kill 9 => $pid; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
my $length = length($global_name); |
115
|
0
|
|
|
|
|
|
printf "$global_name%sDone%s\n", q{.} x (35 - $length), q{ } x 43; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my $elapsed_time = time - $global_start_time; |
118
|
0
|
|
|
|
|
|
return $elapsed_time; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
$SIG{__DIE__} = sub { pulse_stop() }; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 KNOWN PROBLEMS |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Not thread safe. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 AUTHOR |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Yen-Liang Chen, C<< >> |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 BUGS |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 SUPPORT |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
perldoc Term::Pulse |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
You can also look for information at: |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=over 4 |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
L |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
L |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * CPAN Ratings |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
L |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * Search CPAN |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
L |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=back |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Copyright 2008 Alec Chen, all rights reserved. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
170
|
|
|
|
|
|
|
under the same terms as Perl itself. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=cut |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; # End of Term::Pulse |