blib/lib/Sweat.pm

filter: Statementstmt 60.7% 201 / 331 Branchbran 38.0% 35 / 92 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 80.7% 46 / 57 Podpod 0.0% 0 / 15 totaltotal 54.9% 285 / 519 CC 63 SCAR 26.0
CC
63
Cov
55%
_build_refocus_program
56.0
countdown
54.7
__ANON__
30.0
CRAP 13.4
« blib/lib/Sweat/Article.pm ↑ Summary blib/lib/Sweat/Group.pm »
1 package Sweat;
2  
3 3 our $VERSION = 202004250;
4  
5 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Types::Standard qw(Int ArrayRef Maybe Str Bool HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use File::Temp qw(tmpnam);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Web::NewsAPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use MediaWiki::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use utf8::all;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use POSIX qw(uname);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶use Sweat::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Sweat::Article;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use Sweat::ArticleHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 BEGIN {
31 3 ▶ binmode STDOUT, ":utf8";
Subroutine BEGIN: called CC=1 SCAR=0.0
32 }
33  
34 sub DEMOLISH {
35 0 ▶ \&clean_up;
Subroutine DEMOLISH: not called CC=1 SCAR=6.9
Pod undocumented
36 }
37  
38 3 has 'groups' => (
39 is => 'lazy',
40 isa => ArrayRef,
41 );
42  
43 has 'group_config' => (
44 is => 'rw',
45 isa => ArrayRef,
46 2 ▶ default => sub { _default_group_config( @_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 3 );
48  
49 3 has 'drills' => (
50 is => 'lazy',
51 isa => ArrayRef,
52 );
53  
54 3 has 'drill_count' => (
55 is => 'rw',
56 isa => Int,
57 required => 1,
58 default => 12,
59 );
60  
61 3 has 'drill_counter' => (
62 is => 'rw',
63 isa => Int,
64 default => 0,
65 );
66  
67 3 has 'config' => (
68 is => 'ro',
69 isa => Maybe[Str],
70 );
71  
72 3 has 'shuffle' => (
73 is => 'rw',
74 default => 0,
75 isa => Bool,
76 );
77  
78 3 has 'entertainment' => (
79 is => 'rw',
80 default => 1,
81 isa => Bool,
82 );
83  
84 3 has 'chair' => (
85 is => 'rw',
86 default => 1,
87 isa => Bool,
88 );
89  
90 3 has 'jumping' => (
91 is => 'rw',
92 default => 1,
93 isa => Bool,
94 );
95  
96 3 has 'drill_length' => (
97 is => 'rw',
98 isa => Int,
99 default => 30,
100 );
101  
102 3 has 'rest_length' => (
103 is => 'rw',
104 isa => Int,
105 default => 10,
106 );
107  
108 3 has 'drill_prep_length' => (
109 is => 'rw',
110 isa => Int,
111 default => 1,
112 );
113  
114 3 has 'side_switch_length' => (
115 is => 'rw',
116 isa => Int,
117 default => 4,
118 );
119  
120 has 'speech_program' => (
121 is => 'rw',
122 isa => Str,
123 0 ▶ default => sub { (uname())[0] eq 'Darwin'? 'say' : 'espeak' },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
(uname())[0] eq 'Darwin' ? : 0 0 0
124 3 );
125  
126 has 'url_program' => (
127 is => 'rw',
128 isa => Maybe[Str],
129 2 ▶ default => sub { (uname())[0] eq 'Darwin'? 'open' : undef },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
(uname())[0] eq 'Darwin' ? : 0 2 2
130 3 );
131  
132 3 has 'fortune_program' => (
133 is => 'rw',
134 isa => Maybe[Str],
135 default => 'fortune',
136 );
137  
138 3 has 'refocus_program' => (
139 is => 'lazy',
140 isa => Maybe[Str],
141 );
142  
143 3 has 'refocus' => (
144 is => 'rw',
145 isa => Bool,
146 default => 0,
147 );
148  
149 3 has 'newsapi_key' => (
150 is => 'rw',
151 isa => Maybe[Str],
152 );
153  
154 3 has 'country' => (
155 is => 'rw',
156 isa => Str,
157 default => 'us',
158 );
159  
160 3 has 'language' => (
161 is => 'rw',
162 isa => Str,
163 default => 'en',
164 );
165  
166 has 'article_handler' => (
167 is => 'ro',
168 2 ▶ isa => sub { $_[0]->isa('Sweat::ArticleHandler') },
Subroutine __ANON__: called CC=1 SCAR=0.0
169 2 ▶ default => sub { Sweat::ArticleHandler->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
170 3 handles => [ qw( add_article next_article ) ],
171 );
172  
173 3 has 'weather' => (
174 is => 'lazy',
175 isa => Str,
176 );
177  
178 3 has 'speaker_pid' => (
179 is => 'rw',
180 isa => Int,
181 );
182  
183 around BUILDARGS => sub {
184 2 ▶ my ( $orig, $class, %args ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
185  
186 2 _mangle_args( \%args );
187  
188 2 return $class->$orig(%args);
189  
190 3 };
191  
192 sub BUILD {
193 2 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=11 SCAR=27.2
Pod undocumented
194  
195 2 my $config;
196  
197 try {
198 2 ▶ $config = Load( $self->config );
Subroutine __ANON__: called CC=1 SCAR=0.0
199 }
200 catch {
201 0 ▶ die "Can't load the config file! Here's the error from the YAML parser: "
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 . $_
203 . "\n";
204 2 };
205  
206 2 foreach ( $args, $config ) {
207 4 _mangle_args( $_ );
208 }
209  
210 2 for my $method( qw(shuffle entertainment chair jumping refocus)) {
211 10 ▶ next if defined $args->{$method};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$method} 2 8 10
212 8 ▶ if ( defined $config->{$method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{$method}) 0 8 8
213 0 ▶ my $value = $config->{$method} // 0;
Condition0%0/2
$config->{$method} // 0
l!l
00
MC/DC0%0/1
$config->{$method} // 0
$config->{$method}
Truth table0%0/2
A
A= $config->{$method}
Aresult
11
00
214 0 $self->$method($value);
215 }
216 }
217  
218 2 for my $method (
219 qw(
220 newsapi_key country fortune_program speech_program url_program
221 rest_length drill_length drill_count
222 )
223 ) {
224 16 ▶ next if defined $args->{$method};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$method} 8 8 16
225 8 ▶ if ( defined $config->{$method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{$method}) 0 8 8
226 0 $self->$method($config->{$method});
227 }
228 }
229  
230 2 ▶ if ( my $group_data = $config->{groups} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group_data = $config->{"groups"}) 0 2 2
231 0 $self->group_config( $group_data );
232 }
233  
234 2 ▶ if ( $config->{no_news} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"no_news"}) 0 2 2
235 0 $self->newsapi_key( undef );
236 }
237  
238 2 $Sweat::Article::language = $self->language;
239 $Sweat::Article::mw->{config}->{api_url} =
240 2 "https://"
241 . $self->language
242 . ".wikipedia.org/w/api.php";
243  
244 2 $self->_check_resources;
245 }
246  
247 sub _check_resources {
248 2 ▶ my $self = shift;
Subroutine _check_resources: called CC=6 SCAR=28.6
249  
250 2 my $speech_program = $self->speech_program;
251 2 my $bare_speech_program = (split /\s+/, $speech_program)[0];
252  
253 2 ▶ unless ( which( $bare_speech_program) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (which($bare_speech_program)) 0 2 2
254 0 die "ERROR: Sweat's 'speech-program' configuration is set to "
255 . "'$speech_program', but there doesn't seem to be a program "
256 . "there. I can't run without a speech program... sorry!\n";
257 }
258  
259 2 ▶ if ( $self->entertainment ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->entertainment) 0 2 2
260 0 foreach (qw (url fortune) ) {
261 0 my $method = "${_}_program";
262 0 my $program = $self->$method;
263 0 ▶ next unless defined $program;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $program 0 0 0
264 0 my $bare_program = (split /\s+/, $program)[0];
265 0 ▶ unless ( which( $bare_program ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (which($bare_program)) 0 0 0
266 0 $self->$method( undef );
267 0 warn "WARNING: Sweat's '$_-program' configuration is set to "
268 . "'$program', but there doesn't seem to be a program there. "
269 . "Going ahead without $_-opening.\n";
270 }
271 }
272 }
273 }
274  
275  
276 sub _load_entertainment {
277 2 ▶ my $self = shift;
Subroutine _load_entertainment: called CC=2 SCAR=11.6
278 2 ▶ if ( $self->entertainment ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->entertainment) 0 2 2
279 0 local $| = 1;
280 0 say "Loading entertainment, please wait...";
281 0 $self->_load_articles;
282 0 $self->weather;
283 0 say "...done.";
284 }
285  
286 }
287  
288 sub _load_articles {
289 0 ▶ my $self = shift;
Subroutine _load_articles: not called CC=1 SCAR=6.9
290  
291 try {
292 0 ▶ if ( $self->newsapi_key ) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
if ($self->newsapi_key) { } 0 0 0
293 0 my $newsapi = Web::NewsAPI->new(
294 api_key => $self->newsapi_key,
295 );
296 0 my $result = $newsapi->top_headlines(
297 country => $self->country,
298 pageSize => $self->drill_count,
299 );
300 0 foreach ( $result->articles ) {
301 0 $self->add_article(
302 Sweat::Article->new_from_newsapi_article($_)
303 );
304 }
305 }
306 else {
307 0 my $article = Sweat::Article->new_from_random_wikipedia_article;
308 0 ▶ unless ( fork ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (fork) 0 0 0
309 0 $self->add_article( $article );
310 0 for (1..$self->drill_count) {
311 0 $article = Sweat::Article->
312 new_from_linked_wikipedia_article($article);
313 0 $self->add_article( $article );
314 }
315 0 exit;
316 }
317 }
318 }
319 catch {
320 0 ▶ die "Sweat ran into a problem fetching articles. Check your internet\n"
Subroutine __ANON__: not called CC=1 SCAR=6.9
321 . "connection, or run with --no-entertainment to use sweat without\n"
322 . "loading any online distractions.\n";
323 }
324 0 }
325  
326 3 my $temp_file = tmpnam();
327  
328 sub sweat {
329 2 ▶ my $self = shift;
Subroutine sweat: called CC=2 SCAR=6.9
Pod undocumented
330  
331 2 ReadMode 3;
332  
333 2 $self->_load_entertainment;
334  
335 2 for my $drill (@{ $self->drills }) {
336 14 $self->order($drill);
337 }
338  
339 1 $self->cool_down;
340 1 $self->clean_up;
341 }
342  
343 sub order {
344 14 ▶ my ( $self, $drill ) = @_;
Subroutine order: called CC=9 SCAR=25.2
Pod undocumented
345  
346 14 $self->drill_counter( $self->drill_counter + 1 );
347  
348 14 $self->rudely_speak(
349 'Prepare for '
350 . $drill->name
351 . '. Drill '
352 . $self->drill_counter
353 . '.'
354 );
355 13 $self->countdown($self->rest_length);
356  
357 13 my ($extra_text, $url, $article) = $self->entertainment_for_drill( $drill );
358 13 ▶ $extra_text //= q{};
Condition50%1/2
$extra_text //= ""
l!l
013
MC/DC0%0/1
$extra_text //= ""
$extra_text
Truth table50%1/2
A
A= $extra_text
Aresult
11
00
359  
360 # XXX Set the binmode to raw temporarily if we're side-switching (and thus
361 # reporting weather.) This is a hack to get around a strange encoding
362 # issue in this case only.
363 13 ▶ binmode STDOUT, ':raw' if $drill->requires_side_switching;
Branch: 1 branch
BranchTrueFalseTotal
if $drill->requires_side_switching 1 12 13
364 13 $self->rudely_speak( "Start now. $extra_text");
365 13 ▶ binmode STDOUT, ':utf8' if $drill->requires_side_switching;
Branch: 1 branch
BranchTrueFalseTotal
if $drill->requires_side_switching 1 12 13
366  
367 13 my $url_tempfile;
368 13 ▶ if ( defined $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) 0 13 13
369 0 ▶ if ( $url =~ m{\Wyoutube.com/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[\Wyoutube.com/]u) 0 0 0
370 0 $url_tempfile = $self->mangle_youtube_url( $article );
371 0 $url = "file://$url_tempfile";
372 }
373 0 ▶ if ( defined $self->url_program ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->url_program) 0 0 0
374 0 system( split (/\s+/, $self->url_program), $url );
375 0 ▶ if ( defined $self->refocus_program ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->refocus_program) 0 0 0
376 0 system $self->refocus_program;
377 }
378 }
379 }
380  
381 13 ▶ if ( $drill->requires_side_switching ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($drill->requires_side_switching) { } 1 12 13
382 1 $self->countdown( $self->drill_length / 2 );
383 1 $self->speak( 'Switch sides.');
384 1 $self->countdown( $self->side_switch_length );
385 1 $self->speak( 'Resume.' );
386 1 $self->countdown( $self->drill_length / 2 );
387 }
388 else {
389 12 $self->countdown( $self->drill_length );
390 }
391  
392 13 $self->rudely_speak( 'Rest.' );
393 13 sleep $self->drill_prep_length;
394 }
395  
396 sub countdown {
397 28 ▶ my ($self, $seconds) = @_;
Subroutine countdown: called CC=8 SCAR=40.0
Pod undocumented
398 28 ▶ return unless $seconds;
Branch: 1 branch
BranchTrueFalseTotal
unless $seconds 28 0 28
399  
400 0 my $seconds_label_cutoff = 10;
401 0 my @spoken_seconds = (20, 15, 10, 5, 4, 3, 2, 1);
402 0 my $label = 'seconds left';
403  
404 0 for my $current_second (reverse(0..$seconds)) {
405 0 my $keystroke = ReadKey (1);
406 0 ▶ if ( $keystroke ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keystroke) 0 0 0
407 0 $self->pause;
408 }
409 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ == $current_second;} @spoken_seconds or $current_second and not $current_second % 10) 0 0 0
Condition0%0/3
grep {$_ == $current_second;} @spoken_seconds or $current_second and not $current_second % 10
l!l&&r!l&&!r
000
MC/DC0%0/2
grep {$_ == $current_second;} @spoken_seconds or $current_second and not $current_second % 10
grep {$_ == $current_second;} @spoken_seconds $current_second and not $current_second % 10
Truth table0%0/3
A or B
A= grep {$_ == $current_second;} @spoken_secondsB= $current_second and not $current_second % 10
ABresult
1X1
011
000
410 0 ▶ ( grep {$_ == $current_second} @spoken_seconds )
Condition0%0/3
$current_second and not $current_second % 10
!ll&&!rl&&r
000
MC/DC0%0/2
$current_second and not $current_second % 10
$current_second not $current_second % 10
Truth table0%0/3
A and not(B)
A= $current_secondB= not $current_second % 10
ABresult
0X0
100
111
411 || ( $current_second && not ( $current_second % 10 ) )
412 ) {
413 0 ▶ if ( $current_second >= $seconds_label_cutoff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_second >= $seconds_label_cutoff) { } 0 0 0
414 0 $self->speak( "$current_second $label." );
415 0 $label = 'seconds';
416 }
417 else {
418 0 $self->shut_up; # Final countdown, so interrupt any chattiness
419 0 $self->speak( $current_second );
420 }
421 }
422 }
423 }
424  
425 sub shut_up {
426 40 ▶ my $self = shift;
Subroutine shut_up: called CC=3 SCAR=15.7
Pod undocumented
427  
428 # We check for drill-length here for the sake of testing, which usually
429 # sets this to 0, and also runs under the `prove` program... which will
430 # get killed by the code below, oops. Something to improve later.
431 40 ▶ if ( -e $temp_file && $self->drill_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $temp_file and $self->drill_length) 0 40 40
Condition66%2/3
-e $temp_file and $self->drill_length
!ll&&!rl&&r
2380
MC/DC0%0/2
-e $temp_file and $self->drill_length
-e $temp_file $self->drill_length
Truth table66%2/3
A and B
A= -e $temp_fileB= $self->drill_length
ABresult
0X0
100
111
432 0 my $group = getpgrp;
433 0 unlink $temp_file;
434 0 $SIG{TERM} = 'IGNORE';
435 0 kill ('TERM', -$group);
436 0 $SIG{TERM} = 'DEFAULT';
437 }
438 }
439  
440 sub pause {
441 0 ▶ my $self = shift;
Subroutine pause: not called CC=1 SCAR=6.9
Pod undocumented
442  
443 0 $self->shut_up;
444 0 say "***PAUSED*** Press any key to resume.";
445 0 $self->leisurely_speak( 'Paused.' );
446 0 ReadKey (0);
447 0 say "Resuming...";
448 0 $self->leisurely_speak( 'Resuming.' );
449 }
450  
451 sub entertainment_for_drill {
452 13 ▶ my ( $self, $drill ) = @_;
Subroutine entertainment_for_drill: called CC=4 SCAR=24.7
Pod undocumented
453  
454 13 ▶ unless ($self->entertainment) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->entertainment) 13 0 13
455 13 return (undef, undef);
456 }
457  
458 0 my $text;
459 my $url;
460 0 my $article;
461  
462 0 ▶ if ( $drill->requires_side_switching ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($drill->requires_side_switching) { } 0 0 0
463 0 $text = $self->weather;
464 }
465 else {
466 0 $article = $self->next_article;
467 0 ▶ if ( $article ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($article) { } 0 0 0
468 0 $text = $article->text;
469 0 $url = $article->url;
470 }
471 else {
472 0 $text = "Couldn't fetch article, so have this instead: "
473 . $self->fortune;
474 }
475 }
476  
477 0 return ( $text, $url, $article );
478 }
479  
480  
481 sub fortune {
482 1 ▶ my $self = shift;
Subroutine fortune: called CC=4 SCAR=18.5
Pod undocumented
483  
484 1 ▶ return q{} unless $self->entertainment;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->entertainment 1 0 1
485  
486 0 my $exec = $self->fortune_program;
487  
488 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
489 0 ▶ my $text = `$exec` // '';
Condition0%0/2
`$exec` // ""
l!l
00
MC/DC0%0/1
`$exec` // ""
`$exec`
Truth table0%0/2
A
A= `$exec`
Aresult
11
00
490 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
491  
492 0 ▶ unless (length $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $text) 0 0 0
493 0 warn "Sweat tried to fetch a fortune by running `$exec`, but didn't "
494 . "get any output. Sorry!\n";
495 }
496  
497 0 return $text;
498 }
499  
500 sub _build_weather {
501 0 ▶ my $self = shift;
Subroutine _build_weather: not called CC=2 SCAR=17.9
502  
503 0 my $weather;
504 0 my $ua = LWP::UserAgent->new;
505 0 my $response = $ua->get('http://wttr.in?format=3');
506 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
507 0 $weather = 'The current weather in ' . $response->content;
508 }
509 else {
510 0 $weather = 'Could not fetch the current weather. Sorry about that.';
511 }
512 0 return $weather;
513 }
514  
515 sub cool_down {
516 1 ▶ my $self = shift;
Subroutine cool_down: called CC=1 SCAR=0.0
Pod undocumented
517  
518 1 $self->speak(
519 'Workout complete. ' . $self->fortune
520 );
521 }
522  
523 sub speak {
524 43 ▶ my ( $self, $message ) = @_;
Subroutine speak: called CC=3 SCAR=11.0
Pod undocumented
525  
526 43 say $message;
527  
528 43 ▶ return if -e $temp_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $temp_file 41 2 43
529  
530 2 my $pid = fork;
531 2 ▶ if ( $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 1 1 2
532 1 $self->speaker_pid( $pid );
533 }
534 else {
535 1 open my $fh, '>', $temp_file;
536 1 print $fh $pid;
537 1 system ( split (/\s+/, $self->speech_program), $message );
538 1 unlink $temp_file;
539 1 exit;
540 }
541 }
542  
543 sub leisurely_speak {
544 0 ▶ my ( $self, $message ) = @_;
Subroutine leisurely_speak: not called CC=1 SCAR=6.9
Pod undocumented
545  
546 0 system ( split (/\s+/, $self->speech_program), $message );
547 }
548  
549 sub rudely_speak {
550 40 ▶ my ( $self, $message ) = @_;
Subroutine rudely_speak: called CC=1 SCAR=0.0
Pod undocumented
551  
552 40 $self->shut_up;
553 40 $self->speak( $message );
554 }
555  
556 # mangle_youtube_url: create a local file that just embeds the youtube
557 # video, preventing autoplay.
558 sub mangle_youtube_url {
559 0 ▶ my ( $self, $article ) = @_;
Subroutine mangle_youtube_url: not called CC=1 SCAR=6.9
Pod undocumented
560 0 my $tempfile = File::Temp->new( SUFFIX => '.html' );
561 0 binmode $tempfile, ":utf8";
562 0 my $title = $article->title;
563 0 my $description = $article->text;
564 0 my $url = $article->url;
565  
566 0 my ($video_id) = $url =~ m{v=(\w+)};
567  
568 0 print $tempfile qq{<html><head><title>$title</title></head>}
569 . qq{<body><h1>$title</h1>}
570 . qq{<p>$description</p>}
571 . qq{<div><iframe src="https://www.youtube.com/embed/$video_id" }
572 . q{frameborder="0" allow="accelerometer; autoplay; }
573 . q{encrypted-media; gyroscope; picture-in-picture" }
574 . q{allowfullscreen></iframe></div>}
575 . q{<p><em>This article was mangled by Sweat so that its video }
576 . q{didn&#8217;t autoplay. You&#8217;re welcome.</em></p>}
577 . qq{</body></html>\n};
578  
579 0 return $tempfile;
580 }
581  
582 sub _build_drills {
583 2 ▶ my $self = shift;
Subroutine _build_drills: called CC=6 SCAR=18.0
584  
585 2 my @final_drills;
586  
587 2 ▶ while (@final_drills < $self->drill_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (@final_drills < $self->drill_count) 8 2 10
588 8 for my $group ( @{ $self->groups } ) {
589 26 my @drills = $group->unused_drills;
590  
591 26 ▶ unless ( @drills ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@drills) 2 24 26
592 2 $group->reset_drills;
593 2 @drills = $group->unused_drills;
594 }
595  
596 26 ▶ if ( $self->shuffle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->shuffle) 0 26 26
597 0 @drills = shuffle(@drills);
598 }
599  
600 26 $drills[0]->is_used( 1 );
601 26 push @final_drills, $drills[0];
602  
603 26 ▶ last if @final_drills == $self->drill_count;
Branch: 1 branch
BranchTrueFalseTotal
if @final_drills == $self->drill_count 2 24 26
604 }
605 }
606  
607 2 return \@final_drills;
608 }
609  
610 sub clean_up {
611 1 ▶ ReadMode 0;
Subroutine clean_up: called CC=1 SCAR=0.0
Pod undocumented
612 }
613  
614 sub _build_groups {
615 2 ▶ my $self = shift;
Subroutine _build_groups: called CC=1 SCAR=0.0
616  
617 2 return [ Sweat::Group->new_from_config_data( $self, $self->group_config ) ];
618  
619 }
620  
621 sub _default_group_config {
622 2 ▶ my $self = shift;
Subroutine _default_group_config: called CC=1 SCAR=0.0
623 2 return Load(<<END);
624 - name: aerobic
625 drills:
626 - name: jumping jacks
627 requires_jumping: 1
628 - name: high knees
629 requires_jumping: 1
630 - name: step-ups
631 requires_a_chair: 1
632 - name: lower-body
633 drills:
634 - name: wall sit
635 - name: squats
636 - name: knee lunges
637 - name: upper-body
638 drills:
639 - name: push-ups
640 - name: tricep dips
641 requires_a_chair: 1
642 - name: rotational push-ups
643 - name: core
644 drills:
645 - name: abdominal crunches
646 - name: plank
647 - name: side plank
648 requires_side_switching: 1
649 END
650 }
651  
652 sub _mangle_args {
653 6 ▶ my ( $args ) = @_;
Subroutine _mangle_args: called CC=3 SCAR=16.0
654  
655 6 for my $key (keys %$args) {
656 28 ▶ if ( $key =~ /-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /-/u) 0 28 28
657 0 my $new_key = $key;
658 0 my $value = $$args{$key};
659 0 $new_key =~ s/-/_/g;
660 0 $$args{$new_key} = $value;
661 0 delete $$args{$key};
662 }
663 }
664 }
665  
666 sub _build_refocus_program {
667 0 ▶ my $self = shift;
Subroutine _build_refocus_program: not called CC=7 SCAR=40.3
668  
669 0 ▶ return unless $self->refocus;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->refocus 0 0 0
670  
671 0 my $pstree = `pstree -p $$`;
672  
673 0 ▶ if ( (uname())[0] eq 'Darwin') {
Branch: 1 branch
BranchTrueFalseTotal
if ((uname())[0] eq 'Darwin') { } 0 0 0
674 0 ▶ unless ( which( 'pstree' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (which("pstree")) 0 0 0
675 0 warn "The refocus feature requires the 'pstree' program to be "
676 . "installed. I can't find it, so I won't be able to "
677 . "refocus the window during the workout. Sorry...\n";
678 0 return;
679 }
680  
681 0 my ($app) = $pstree =~ /(\S+Applications\S+)/;
682 0 ▶ if ($app) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app) { } 0 0 0
683 0 return "open -a $app";
684 }
685 else {
686 0 warn "I can't figure out what terminal I'm running in?! "
687 . "I won't be able to refocus the window during the "
688 . "workout. Sorry...\n";
689 0 return;
690 }
691 }
692 else {
693 0 ▶ unless ( which( 'xdotool' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (which("xdotool")) 0 0 0
694 0 warn "The refocus feature requires the 'xdotool' program to be "
695 . "installed. I can't find it, so I won't be able to "
696 . "refocus the window during the workout. Sorry...\n";
697 0 return;
698 }
699  
700 0 my $window_id = `xdotool getactivewindow`;
701 0 ▶ if ( $window_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($window_id) { } 0 0 0
702 0 return "xdotool windowactivate $window_id";
703 }
704 else {
705 0 warn "I can't figure out what window I'm running in?! "
706 . "I won't be able to refocus the window during the "
707 . "workout. Sorry...\n";
708 0 return;
709 }
710 }
711  
712 }
713  
714 3 1;
715  
716 =head1 NAME
717  
718 Sweat - Library for the `sweat` command-line program
719  
720 =head1 DESCRIPTION
721  
722 This library is intended for internal use by the L<sweat> command-line program,
723 and as such offers no publicly documented methods.
724  
725 =head1 SEE ALSO
726  
727 L<sweat>
728  
729 =head1 AUTHOR
730  
731 Jason McIntosh <jmac@jmac.org>
« blib/lib/Sweat/Article.pm ↑ Summary blib/lib/Sweat/Group.pm »