blib/lib/Term/Spinner/Lite.pm

filter: Statementstmt 94.2% 82 / 87 Branchbran 84.6% 22 / 26 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 90.0% 18 / 20 Podpod 100% 7 / 7 totaltotal 90.9% 130 / 143 CC 15 SCAR 8.9
CC
15
Cov
91%
spin_chars
4.0
output_handle
4.0
next
4.0
CRAP 2.4
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Term::Spinner::Lite;
4 {
5 2 $Term::Spinner::Lite::VERSION = '0.03';
6 }
7  
8 # ABSTRACT: A spinner without so much Moose in it
9  
10 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 1 my ($class, %args) = @_;
19  
20 1 my $self = bless \%args, $class;
21  
22 1 $self->_setup_signal_handler;
23  
24 1 return $self;
25 }
26  
27 # We need to restore the cursor if the user hits Contol-C or a TERM signal
28 1 ▶sub _setup_signal_handler {
Subroutine _setup_signal_handler: called CC=1 SCAR=1.7
29 1 my $self = shift;
30  
31 0 ▶ $SIG{INT} = sub { $self->done; exit 0; };
Subroutine __ANON__: not called CC=1 SCAR=2.6
32 0 ▶ $SIG{TERM} = sub { $self->done; exit 0; };
Subroutine __ANON__: not called CC=1 SCAR=2.6
33 }
34  
35  
36 25 ▶sub output_handle {
Subroutine output_handle: called CC=4 SCAR=13.9
Pod documented
37 25 my $self = shift;
38 25 my $handle = shift;
39 25 ▶ my $encoding = shift || ":utf8";
Condition50%1/2
shift() || ':utf8'
l!l
025
MC/DC0%0/1
shift() || ':utf8'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
40  
41 25 ▶ if ( not $handle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle) 25 0 25
42 25 ▶ if ( exists $self->{'output_handle'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"output_handle"}) 24 1 25
43 24 return $self->{'output_handle'};
44 }
45 1 $handle = \*STDERR;
46 }
47  
48 1 binmode($handle, $encoding);
49 1 $handle->autoflush(1);
50  
51 1 $self->{'output_handle'} = $handle;
52 }
53  
54  
55 13 ▶sub spin_chars {
Subroutine spin_chars: called CC=4 SCAR=14.0
Pod documented
56 13 my $self = shift;
57 13 my $aref = shift;
58  
59 13 ▶ if ( not $aref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aref) 12 1 13
60 12 ▶ if ( exists $self->{'spin_chars'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"spin_chars"}) 11 1 12
61 11 return $self->{'spin_chars'};
62 }
63 1 $aref = [ qw(- \ | /) ];
64 }
65
66 2 ▶ if ( ref($aref) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $aref ne "ARRAY") 0 2 2
67 0 croak "spin_chars must be an array ref";
68 }
69  
70 2 $self->{'spin_chars'} = $aref;
71 }
72  
73  
74 22 ▶sub delay {
Subroutine delay: called CC=3 SCAR=11.0
Pod documented
75 22 my $self = shift;
76 22 my $microseconds = shift;
77  
78 22 ▶ if ( not $microseconds ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($microseconds) 21 1 22
79 21 ▶ if ( exists $self->{'delay'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"delay"}) 20 1 21
80 20 return $self->{'delay'};
81 }
82 1 $microseconds = 0;
83 }
84  
85 2 $self->{'delay'} = $microseconds;
86 }
87  
88 10 ▶sub _clear {
Subroutine _clear: called CC=1 SCAR=0.0
89 10 my $self = shift;
90  
91 10 print {$self->output_handle()} "\010 \010";
92 }
93  
94 # https://github.com/verigak/progress/blob/master/progress/helpers.py#L19
95 1 ▶sub _show_cursor {
Subroutine _show_cursor: called CC=1 SCAR=0.0
96 1 my $self = shift;
97  
98 1 print {$self->output_handle()} "\x1b[?25h";
99 }
100  
101 # https://github.com/verigak/progress/blob/master/progress/helpers.py#L18
102 4 ▶sub _hide_cursor {
Subroutine _hide_cursor: called CC=1 SCAR=0.0
103 4 my $self = shift;
104  
105 4 print {$self->output_handle()} "\x1b[?25l";
106 }
107  
108 11 ▶sub _spin_char_size {
Subroutine _spin_char_size: called CC=2 SCAR=6.9
109 11 my $self = shift;
110  
111 11 ▶ if ( exists $self->{'spin_char_size'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"spin_char_size"}) 10 1 11
112 10 return $self->{'spin_char_size'};
113 }
114  
115 1 $self->{'spin_char_size'} = scalar @{ $self->spin_chars() };
116 }
117  
118  
119 12 ▶sub count {
Subroutine count: called CC=2 SCAR=6.9
Pod documented
120 12 my $self = shift;
121  
122 12 ▶ return 0 if not exists $self->{'count'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"count"} 2 10 12
123  
124 10 return $self->{'count'};
125 }
126  
127  
128 10 ▶sub next {
Subroutine next: called CC=4 SCAR=13.9
Pod documented
129 10 my $self = shift;
130  
131 10 state $pos = 0;
132  
133 10 ▶ $self->_clear if $self->count;
Branch: 1 branch
BranchTrueFalseTotal
if $self->count 9 1 10
134 10 ▶ $self->_hide_cursor if not $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos 4 6 10
135 10 print {$self->output_handle()} "${$self->spin_chars()}[$pos]";
136 10 $pos = ($self->{'count'}++) % $self->_spin_char_size();
137 10 ▶ usleep($self->delay) if $self->delay;
Branch: 1 branch
BranchTrueFalseTotal
if $self->delay 10 0 10
138 }
139  
140  
141 1 ▶sub done {
Subroutine done: called CC=2 SCAR=7.0
Pod documented
142 1 my $self = shift;
143  
144 1 $self->_clear;
145 1 $self->_show_cursor;
146 1 ▶ print "\n" if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 1 1
147 }
148  
149 2 1;
150  
151 __END__
↑ Summary