blib/lib/Webservice/Rosary/Stream.pm

filter: Statementstmt 67.6% 90 / 133 Branchbran 58.7% 54 / 92 Conditioncond 52.7% 19 / 36 MC/DCmcdc 29.1% 7 / 24 Subroutinesub 68.0% 17 / 25 Podpod 50.0% 6 / 12 totaltotal 59.9% 193 / 322 CC 60 SCAR 28.6
CC
60
Cov
62%
_confirm_quit
72.0
_enable_terminal_controls
30.5
new
21.5
CRAP 17.4
« bin/avemaria ↑ Summary blib/lib/Webservice/Rosary/API.pm »
1 package Webservice::Rosary::Stream;
2  
3 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Time::HiRes qw/sleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = "0.1.7";
10  
11 6 ▶sub new {
Subroutine new: called CC=17 SCAR=30.7
Pod documented
12 6 my $class = shift;
13 6 my %opts = @_;
14  
15 my $self = bless {
16 delay => defined $opts{delay} ? 0 + $opts{delay} : 0.04,
17 speed => defined $opts{speed} ? 0 + $opts{speed} : 1,
18 base_speed => defined $opts{speed} ? 0 + $opts{speed} : 1,
19 min_speed => defined $opts{min_speed} ? 0 + $opts{min_speed} : 0.25,
20 max_speed => defined $opts{max_speed} ? 0 + $opts{max_speed} : 8,
21 speed_step => defined $opts{speed_step} ? 0 + $opts{speed_step} : 1.25,
22 controls => exists $opts{controls} ? $opts{controls} : 1,
23 paused => 0,
24 quit_requested => 0,
25 confirm_quit => $opts{confirm_quit},
26 on_quit => $opts{on_quit},
27 read_key => $opts{read_key},
28 0 ▶ sleep_cb => $opts{sleep_cb} || sub { sleep $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 ▶ write_cb => $opts{write_cb} || sub { print STDOUT $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 6 ▶ terminal_active => 0,
Branch: 7 branches
BranchTrueFalseTotal
defined $opts{'delay'} ? : 5 1 6
defined $opts{'speed'} ? : 1 5 6
defined $opts{'speed'} ? : 1 5 6
defined $opts{'min_speed'} ? : 0 6 6
defined $opts{'max_speed'} ? : 0 6 6
defined $opts{'speed_step'} ? : 0 6 6
exists $opts{'controls'} ? : 6 0 6
Condition66%4/6
$opts{'sleep_cb'} || sub { &Webservice::Rosary::Stream::sleep($_[0]); }
l!l&&r!l&&!r
420
$opts{'write_cb'} || sub { print STDOUT $_[0]; }
l!l&&r!l&&!r
420
MC/DC0%0/2
$opts{'sleep_cb'} || sub { &Webservice::Rosary::Stream::sleep($_[0]); }
$opts{'sleep_cb'} sub { &Webservice::Rosary::Stream::sleep($_[0]); }
MC/DC0%0/2
$opts{'write_cb'} || sub { print STDOUT $_[0]; }
$opts{'write_cb'} sub { print STDOUT $_[0]; }
Truth table66%2/3
A || B
A= $opts{'sleep_cb'}B= sub { &Webservice::Rosary::Stream::sleep($_[0]); }
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $opts{'write_cb'}B= sub { print STDOUT $_[0]; }
ABresult
1X1
011
000
31 owns_reader => 0,
32 }, $class;
33  
34 6 ▶ die "stream delay must be zero or greater\n" if $self->{delay} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"delay"} < 0 1 5 6
35 5 ▶ die "stream speed must be greater than zero\n" if $self->{speed} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"speed"} <= 0 1 4 5
36 4 ▶ die "minimum stream speed must be greater than zero\n" if $self->{min_speed} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"min_speed"} <= 0 0 4 4
37 die "maximum stream speed must be at least minimum stream speed\n"
38 4 ▶ if $self->{max_speed} < $self->{min_speed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"max_speed"} < $self->{"min_speed"} 0 4 4
39 4 ▶ die "stream speed step must be greater than one\n" if $self->{speed_step} <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"speed_step"} <= 1 0 4 4
40  
41 4 ▶ if ($self->{read_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'read_key'}) { } 3 1 4
42 3 ▶ $self->{terminal_active} = $self->{controls} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'controls'} ? : 3 0 3
43 }
44 else {
45 1 $self->_enable_terminal_controls;
46 }
47  
48 4 return $self;
49 }
50  
51 0 ▶sub delay { return $_[0]->{delay}; }
Subroutine delay: not called CC=1 SCAR=6.9
Pod undocumented
52 3 ▶sub speed { return $_[0]->{speed}; }
Subroutine speed: called CC=1 SCAR=0.0
Pod undocumented
53 3 ▶sub paused { return $_[0]->{paused}; }
Subroutine paused: called CC=1 SCAR=0.0
Pod undocumented
54 0 ▶sub quit_requested { return $_[0]->{quit_requested}; }
Subroutine quit_requested: not called CC=1 SCAR=6.9
Pod undocumented
55 15 ▶sub interactive { return $_[0]->{controls} && $_[0]->{terminal_active}; }
Subroutine interactive: called CC=2 SCAR=7.2
Pod undocumented
Condition66%2/3
$_[0]{'controls'} && $_[0]{'terminal_active'}
!ll&&!rl&&r
708
MC/DC50%1/2
$_[0]{'controls'} && $_[0]{'terminal_active'}
$_[0]{'controls'} $_[0]{'terminal_active'}
Truth table66%2/3
A && B
A= $_[0]{'controls'}B= $_[0]{'terminal_active'}
ABresult
0X0
100
111
56  
57 1 ▶sub controls_help {
Subroutine controls_help: called CC=1 SCAR=0.0
Pod undocumented
58 1 return q{SPACE/p pause/resume, +/- speed, 0 reset, q quit};
59 }
60  
61 1 ▶sub _enable_terminal_controls {
Subroutine _enable_terminal_controls: called CC=6 SCAR=34.2
62 1 my $self = shift;
63 1 ▶ return 0 if not $self->{controls};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"controls"} 1 0 1
64 0 ▶ return 0 if $self->{read_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"read_key"} 0 0 0
65 0 ▶ return 0 if not -t STDIN or not -t STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless -t STDIN and -t STDOUT 0 0 0
Condition0%0/3
-t STDIN and -t STDOUT
!ll&&!rl&&r
000
MC/DC0%0/2
-t STDIN and -t STDOUT
-t STDIN -t STDOUT
Truth table0%0/3
A and B
A= -t STDINB= -t STDOUT
ABresult
0X0
100
111
66  
67 0 my $ok = eval {
68 0 require Term::ReadKey;
69 0 Term::ReadKey::ReadMode(q{cbreak});
70 0 1;
71 };
72 0 ▶ return 0 if not $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
73  
74 0 ▶ $self->{read_key} = sub { return Term::ReadKey::ReadKey(-1); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 $self->{terminal_active} = 1;
76 0 $self->{owns_reader} = 1;
77 0 return 1;
78 }
79  
80 6 ▶sub restore_terminal {
Subroutine restore_terminal: called CC=3 SCAR=16.8
Pod documented
81 6 my $self = shift;
82 6 ▶ if ($self->{owns_reader} and $self->{terminal_active}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"owns_reader"} and $self->{"terminal_active"}) 0 6 6
Condition33%1/3
$self->{"owns_reader"} and $self->{"terminal_active"}
!ll&&!rl&&r
600
MC/DC0%0/2
$self->{"owns_reader"} and $self->{"terminal_active"}
$self->{"owns_reader"} $self->{"terminal_active"}
Truth table33%1/3
A and B
A= $self->{"owns_reader"}B= $self->{"terminal_active"}
ABresult
0X0
100
111
83 0 eval { Term::ReadKey::ReadMode(q{restore}); 1 };
84 0 $self->{terminal_active} = 0;
85 0 $self->{read_key} = undef;
86 0 $self->{owns_reader} = 0;
87 }
88 6 return;
89 }
90  
91 0 ▶sub resume_terminal {
Subroutine resume_terminal: not called CC=1 SCAR=6.9
Pod documented
92 0 my $self = shift;
93 0 return $self->_enable_terminal_controls;
94 }
95  
96 16 ▶sub handle_key {
Subroutine handle_key: called CC=13 SCAR=25.9
Pod documented
97 16 my ($self, $key) = @_;
98 16 ▶ return q{} if not defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 5 11 16
99  
100 11 ▶ if ($key eq q{ } or lc($key) eq q{p}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq " " or lc $key eq "p") 4 7 11
Condition100%3/3
$key eq " " or lc $key eq "p"
l!l&&r!l&&!r
137
MC/DC100%2/2
$key eq " " or lc $key eq "p"
$key eq " " lc $key eq "p"
Truth table100%3/3
A or B
A= $key eq " "B= lc $key eq "p"
ABresult
1X1
011
000
101 4 $self->{paused} = not $self->{paused};
102 4 ▶ return $self->{paused} ? q{paused} : q{resumed};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'paused'} ? : 2 2 4
103 }
104 7 ▶ if ($key eq q{+} or $key eq q{=}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "+" or $key eq "=") 4 3 7
Condition66%2/3
$key eq "+" or $key eq "="
l!l&&r!l&&!r
403
MC/DC50%1/2
$key eq "+" or $key eq "="
$key eq "+" $key eq "="
Truth table66%2/3
A or B
A= $key eq "+"B= $key eq "="
ABresult
1X1
011
000
105 4 $self->{speed} *= $self->{speed_step};
106 4 ▶ $self->{speed} = $self->{max_speed} if $self->{speed} > $self->{max_speed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"speed"} > $self->{"max_speed"} 0 4 4
107 4 return q{faster};
108 }
109 3 ▶ if ($key eq q{-} or $key eq q{_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "-" or $key eq "_") 1 2 3
Condition66%2/3
$key eq "-" or $key eq "_"
l!l&&r!l&&!r
102
MC/DC50%1/2
$key eq "-" or $key eq "_"
$key eq "-" $key eq "_"
Truth table66%2/3
A or B
A= $key eq "-"B= $key eq "_"
ABresult
1X1
011
000
110 1 $self->{speed} /= $self->{speed_step};
111 1 ▶ $self->{speed} = $self->{min_speed} if $self->{speed} < $self->{min_speed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"speed"} < $self->{"min_speed"} 0 1 1
112 1 return q{slower};
113 }
114 2 ▶ if ($key eq q{0}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 0) 1 1 2
115 1 $self->{speed} = $self->{base_speed};
116 1 return q{reset};
117 }
118 1 ▶ if (lc($key) eq q{q}) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $key eq "q") 0 1 1
119 0 return q{quit};
120 }
121  
122 1 return q{};
123 }
124  
125 0 ▶sub _confirm_quit {
Subroutine _confirm_quit: not called CC=8 SCAR=42.8
126 0 my $self = shift;
127 0 ▶ return 1 if $self->{quit_requested};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quit_requested"} 0 0 0
128  
129 # A blocking y/N prompt cannot safely run while the terminal is in cbreak
130 # mode. Restore it first; if the user declines, resume live controls.
131 0 $self->restore_terminal;
132  
133 0 my $confirmed;
134 0 ▶ if ($self->{confirm_quit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'confirm_quit'}) { } 0 0 0
135 0 ▶ $confirmed = $self->{confirm_quit}->() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'confirm_quit'}->() ? : 0 0 0
136 }
137 else {
138 0 print STDOUT "\nQuit y/N? ";
139 0 my $answer = <STDIN>;
140 0 ▶ $confirmed = defined($answer) && $answer =~ m/^\s*y(?:es)?\s*$/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $answer && $answer =~ /^\s*y(?:es)?\s*$/i ? : 0 0 0
Condition0%0/3
defined $answer and $answer =~ /^\s*y(?:es)?\s*$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $answer and $answer =~ /^\s*y(?:es)?\s*$/i
defined $answer $answer =~ /^\s*y(?:es)?\s*$/i
Truth table0%0/3
A and B
A= defined $answerB= $answer =~ /^\s*y(?:es)?\s*$/i
ABresult
0X0
100
111
141 }
142  
143 0 ▶ if ($confirmed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($confirmed) 0 0 0
144 0 $self->{quit_requested} = 1;
145 0 $self->{paused} = 0;
146 0 ▶ $self->{on_quit}->() if $self->{on_quit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_quit"} 0 0 0
147 0 return 1;
148 }
149  
150 0 $self->resume_terminal;
151 0 return 0;
152 }
153  
154 14 ▶sub _poll_control {
Subroutine _poll_control: called CC=3 SCAR=11.0
155 14 my $self = shift;
156 14 ▶ return q{} if not $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 6 8 14
157 8 my $key = $self->{read_key}->();
158 8 my $action = $self->handle_key($key);
159 8 ▶ $self->_confirm_quit if $action eq q{quit};
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "quit" 0 8 8
160 8 return $action;
161 }
162  
163 12 ▶sub _wait_while_paused {
Subroutine _wait_while_paused: called CC=3 SCAR=11.0
164 12 my $self = shift;
165 12 ▶ while ($self->{paused} and not $self->{quit_requested}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"paused"} and not $self->{"quit_requested"}) 2 12 14
Condition66%2/3
$self->{"paused"} and not $self->{"quit_requested"}
!ll&&!rl&&r
1202
MC/DC50%1/2
$self->{"paused"} and not $self->{"quit_requested"}
$self->{"paused"} not $self->{"quit_requested"}
Truth table66%2/3
A and not(B)
A= $self->{"paused"}B= not $self->{"quit_requested"}
ABresult
0X0
100
111
166 2 $self->{sleep_cb}->(0.05);
167 2 $self->_poll_control;
168 }
169 12 return;
170 }
171  
172 5 ▶sub _sleep_with_controls {
Subroutine _sleep_with_controls: called CC=7 SCAR=19.6
173 5 my ($self, $seconds) = @_;
174 5 ▶ return if not defined $seconds or $seconds <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $seconds or $seconds <= 0 1 4 5
Condition66%2/3
not defined $seconds or $seconds <= 0
l!l&&r!l&&!r
014
MC/DC50%1/2
not defined $seconds or $seconds <= 0
not defined $seconds $seconds <= 0
Truth table66%2/3
not(A) or B
A= not defined $secondsB= $seconds <= 0
ABresult
1X1
011
000
175  
176 4 my $remaining = $seconds;
177 4 ▶ while ($remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining > 0) 7 4 11
178 7 $self->_poll_control;
179 7 ▶ return q{quit} if $self->{quit_requested};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quit_requested"} 0 7 7
180 7 $self->_wait_while_paused;
181 7 ▶ return q{quit} if $self->{quit_requested};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quit_requested"} 0 7 7
182  
183 7 ▶ my $slice = $remaining > 0.025 ? 0.025 : $remaining;
Branch: 1 branch
BranchTrueFalseTotal
$remaining > 0.025 ? : 3 4 7
184 7 $self->{sleep_cb}->($slice);
185 7 $remaining -= $slice;
186 }
187 4 return;
188 }
189  
190 3 ▶sub stream {
Subroutine stream: called CC=7 SCAR=20.6
Pod documented
191 3 my ($self, $text) = @_;
192 3 ▶ return if not defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 3 3
193  
194 3 foreach my $char (split //, $text) {
195 5 $self->_poll_control;
196 5 ▶ return q{quit} if $self->{quit_requested};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quit_requested"} 0 5 5
197 5 $self->_wait_while_paused;
198 5 ▶ return q{quit} if $self->{quit_requested};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quit_requested"} 0 5 5
199 5 $self->{write_cb}->($char);
200 5 my $delay = $self->{delay} / $self->{speed};
201 5 my $status = $self->_sleep_with_controls($delay);
202 5 ▶ return q{quit} if defined $status and $status eq q{quit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $status and $status eq "quit" 0 5 5
Condition33%1/3
defined $status and $status eq "quit"
!ll&&!rl&&r
500
MC/DC0%0/2
defined $status and $status eq "quit"
defined $status $status eq "quit"
Truth table33%1/3
A and B
A= defined $statusB= $status eq "quit"
ABresult
0X0
100
111
203 }
204 3 return q{};
205 }
206  
207 0 ▶sub wait {
Subroutine wait: not called CC=1 SCAR=6.9
Pod documented
208 0 my ($self, $seconds) = @_;
209 0 return $self->_sleep_with_controls($seconds);
210 }
211  
212 6 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
213 6 my $self = shift;
214 6 $self->restore_terminal;
215 6 return;
216 }
217  
218 2 1;
219  
220 __END__
« bin/avemaria ↑ Summary blib/lib/Webservice/Rosary/API.pm »