bin/speak

filter: Statementstmt 59.7% 43 / 72 Branchbran 23.0% 6 / 26 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 66.6% 10 / 15 Podpod n/a totaltotal 43.3% 59 / 136 CC 2 SCAR 6.3
CC
2
Cov
43%
error
6.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.9
« blib/lib/Speak.pm ↑ Summary
1 #!/usr/bin/perl
2  
3 =pod
4  
5 =head1 NAME speak / say
6  
7 Simply speaks the passed in message, clipboard or file (invoked as speak or say)
8  
9 =head1 VERSION
10  
11 =over
12  
13 =item Author
14  
15 Andrew DeFaria <Andrew@DeFaria.com>
16  
17 =item Created:
18  
19 Wed 24 Feb 2021 12:01:12 PM PST
20  
21 =back
22  
23 =head1 SYNOPSIS
24  
25 Usage: speak | say [-usa|ge] [-h|elp] [-v|erbose] [-de|bug]
26 [-c|lipboard] [-f|ile <filename>] [-p|ersona <name>]
27 [-a|pp <name>] [-l|astmsg | -r|epeatmsg]
28 [-o|utput <filename>] ["message"]
29  
30 Where:
31 -usa|ge: Print this usage
32 -h|elp: Detailed help
33 -c|lipboard: Speak the contents of the clipboard
34 -f|ile <filename>: Speak the contents of <filename>
35 -p|ersona <name>: Use a specific persona voice
36 -a|pp <name>: Specify application name to tag in the log
37 -l|astmsg | -repeatmsg: Repeat the last spoken message from the log
38 -o|utput <file>: Save output speech to specified audio file
39 "message": Speak the message
40  
41 =head1 DESCRIPTION
42  
43 This script speaks the contents of the passed in message, clipboard or file.
44 Can be called as either 'speak' or 'say'.
45  
46 =cut
47  
48 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use lib "$FindBin::Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 ▶use Speak qw(speak last_message is_persona);
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use Clipboard;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 0 ▶sub error ($msg, $exit_code = 1) {
Subroutine error: not called CC=2 SCAR=17.9
62 0 print STDERR "ERROR: $msg\n";
63 0 exit $exit_code;
64 }
65  
66 1 my $verbose = 0;
67 1 my $debug = 0;
68  
69 my %opts = (
70 0 ▶ usage => sub {pod2usage},
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 ▶ help => sub {pod2usage (-verbose => 2)},
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 ## no critic (Variables::RequireLocalizedPunctuationVars)
73 0 ▶ verbose => sub {$verbose = 1; $ENV{VERBOSE} = 1},
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 ▶ debug => sub {$debug = 1; $ENV{DEBUG} = 1},
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 1 );
76  
77 ## Main
78 1 ▶GetOptions (
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opts, "usage", "help", "verbose", "debug", "clipboard", "file=s", "persona=s", "output=s", "app=s", "lastmsg|repeatmsg|repeat") or pod2usage() 1 0 1
79 \%opts,
80 'usage',
81 'help',
82 'verbose',
83 'debug',
84 'clipboard',
85 'file=s',
86 'persona=s',
87 'output=s',
88 'app=s',
89 'lastmsg|repeatmsg|repeat',
90 ) || pod2usage;
91  
92 1 my $msg = join ' ', @ARGV;
93  
94 1 ▶if ($opts{lastmsg}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opts{'lastmsg'}) { } 0 1 1
elsif ($opts{'clipboard'}) { } 0 1 1
elsif ($opts{'file'}) { } 1 0 1
95 0 ▶ if ($opts{clipboard} || $opts{file} || $msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"clipboard"} or $opts{"file"} or $msg) 0 0 0
Condition0%0/6
$opts{"clipboard"} or $opts{"file"} or $msg
l!l&&r!l&&!r
000
$opts{"clipboard"} or $opts{"file"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts{"clipboard"} or $opts{"file"} or $msg
$opts{"clipboard"} $opts{"file"} $msg
Truth table0%0/4
A or B or C
A= $opts{"clipboard"}B= $opts{"file"}C= $msg
ABCresult
1XX1
01X1
0011
0000
96 0 error 'Cannot specify -lastmsg/-repeatmsg with other message sources', 1;
97 }
98 0 my ($last, $tag) = last_message ();
99 0 ▶ error 'No previous spoken message found in log', 1 unless defined $last;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last 0 0 0
100 0 $msg = $last;
101 0 ▶ if (defined $tag && length $tag && !defined $opts{persona}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag and length $tag and not defined $opts{"persona"}) 0 0 0
Condition0%0/6
defined $tag and length $tag and not defined $opts{"persona"}
!ll&&!rl&&r
000
defined $tag and length $tag
!ll&&!rl&&r
000
MC/DC0%0/3
defined $tag and length $tag and not defined $opts{"persona"}
defined $tag length $tag not defined $opts{"persona"}
Truth table0%0/4
A and B and not(C)
A= defined $tagB= length $tagC= not defined $opts{"persona"}
ABCresult
0XX0
10X0
1100
1111
102 0 ▶ if (is_persona ($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_persona($tag)) 0 0 0
103 0 $opts{persona} = $tag;
104 }
105 }
106 } elsif ($opts{clipboard}) {
107 0 ▶ if ($opts{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'file'}) { } 0 0 0
elsif ($msg) { } 0 0 0
108 0 error 'Cannot specify both -clipboard and -file', 1;
109 } elsif ($msg) {
110 0 error 'Cannot specify both -clipboard and <message>', 1;
111 } else {
112 0 $msg = eval {
113 0 open my $olderr, '>&', \*STDERR;
114 0 open STDERR, '>', '/dev/null';
115 0 my $res = Clipboard->paste;
116 0 open STDERR, '>&', $olderr;
117 0 $res;
118 };
119 0 ▶ if (!defined $msg || $msg eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $msg or $msg eq "") 0 0 0
Condition0%0/3
not defined $msg or $msg eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $msg or $msg eq ""
not defined $msg $msg eq ""
Truth table0%0/3
not(A) or B
A= not defined $msgB= $msg eq ""
ABresult
1X1
011
000
120 0 error 'Clipboard is empty or inaccessible', 1;
121 }
122 } # if
123 } elsif ($opts{file}) {
124 1 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) { } 0 1 1
125 0 error 'Cannot specify both -file and <message>', 1;
126 } else {
127 open my $file, '<', $opts{file}
128 1 ▶ or error "Unable to open $opts{file} - $!", 1;
Branch: 1 branch
BranchTrueFalseTotal
open my $file, "<", $opts{"file"} or error(qq[Unable to open $opts{"file"} - $!], 1) 1 0 1
129  
130 1 $msg = do {local $/; <$file>};
131  
132 1 close $file;
133 } # if
134 } # if
135  
136 1 speak ($msg, undef, undef, $opts{persona}, $opts{output}, $opts{app});
« blib/lib/Speak.pm ↑ Summary