File Coverage

blib/lib/Lab/Moose/Countdown.pm
Criterion Covered Total %
statement 22 36 61.1
branch 2 8 25.0
condition 1 2 50.0
subroutine 7 7 100.0
pod 1 1 100.0
total 33 54 61.1


line stmt bran cond sub pod time code
1             package Lab::Moose::Countdown;
2             $Lab::Moose::Countdown::VERSION = '3.880';
3             #ABSTRACT: Verbose countdown/delay with pretty printing of remaining time
4              
5 6     6   134 use v5.20;
  6         23  
6              
7 6     6   60 use warnings;
  6         15  
  6         190  
8 6     6   36 use strict;
  6         16  
  6         164  
9              
10 6     6   36 use Exporter 'import';
  6         12  
  6         265  
11 6     6   37 use Time::HiRes qw/time sleep/;
  6         28  
  6         68  
12 6     6   3560 use Time::Seconds;
  6         7520  
  6         1728  
13              
14             our @EXPORT = qw/countdown/;
15              
16              
17              
18             sub countdown {
19              
20             # Do not use MooseX::Params::Validate for performance reasons.
21 354     354 1 727 my $delay = shift;
22 354   50     1324 my $prefix = shift // "Sleeping for ";
23              
24 354 50       947 if ( $delay < 0.5 ) {
25 354 50       1077 if ( $delay > 0 ) {
26 0         0 sleep $delay;
27             }
28 354         1178 return;
29             }
30              
31 0           my $t1 = time();
32              
33 0           my $autoflush = STDOUT->autoflush();
34              
35 0           while () {
36 0           my $remaining = $delay - ( time() - $t1 );
37 0 0         if ( $remaining < 0.5 ) {
38 0 0         if ( $remaining > 0 ) {
39 0           sleep $remaining;
40             }
41 0           last;
42             }
43 0           $remaining = Time::Seconds->new( int($remaining) + 1 );
44 0           sleep 0.1;
45 0           print $prefix, $remaining->pretty, " \r";
46             }
47 0           say " " x 80;
48 0           STDOUT->autoflush($autoflush);
49             }
50              
51             __END__
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             Lab::Moose::Countdown - Verbose countdown/delay with pretty printing of remaining time
60              
61             =head1 VERSION
62              
63             version 3.880
64              
65             =head1 SYNOPSIS
66              
67             use Lab::Moose::Countdown;
68              
69             # Sleep for 23.45678 seconds with pretty countdown
70             countdown(23.45678, "Getting ready, Remaining time is ");
71              
72             =head1 FUNCTIONS
73              
74             =head2 countdown
75              
76             my $delay = 2 # seconds
77             countdown($delay)
78              
79             my $prefix = "Some prefix text";
80             countdown($delay, $prefix);
81              
82             Replacement for C<Time::HiRes::sleep>. Pretty print the remaining
83             hours/minutes/seconds. If the argument is smaller than 0.5 seconds, no
84             countdown is printed and the function behaves exactly like C<Time::HiRes::sleep>.
85             Default C<$prefix> is C<"Sleeping for">.
86              
87             =head1 COPYRIGHT AND LICENSE
88              
89             This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
90              
91             Copyright 2018 Simon Reinhardt
92             2020 Andreas K. Huettel, Simon Reinhardt
93              
94              
95             This is free software; you can redistribute it and/or modify it under
96             the same terms as the Perl 5 programming language system itself.
97              
98             =cut