Nag.pm

filter: Statementstmt 37.0% 43 / 116 Branchbran 7.1% 2 / 28 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 85.7% 12 / 14 Podpod 100% 3 / 3 totaltotal 33.9% 60 / 177 CC 19 SCAR 43.3
CC
19
Cov
29%
validate_time
168.2
run
12.0
_parse_time
4.5
CRAP 75.6
↑ Summary Nag/Slap.pm »
1 package App::Nag;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $App::Nag::VERSION = '0.002';
4 }
5  
6 # ABSTRACT: send yourself a reminder
7  
8  
9 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Getopt::Long::Descriptive qw(describe_options prog_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # some icon specs
13 1 ▶use constant PHRASE => [qw(psst hey HEY !!!)];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant STROKE => [qw(0000ff 0000ff ff0000 ff0000)];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant FILL => [qw(ffffff ffffff ffffff ffff00)];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant OPACITY => [ 0, 1, 1, 1 ];
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant FONT_SIZE => [ 20, 25, 28, 32 ];
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant XY => [ [ 8, 40 ], [ 9, 40 ], [ 7, 41 ], [ 3, 43 ] ];
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 1 ▶sub validate_args {
Subroutine validate_args: called CC=2 SCAR=7.3
Pod documented
22 1 my $name = prog_name;
23 1 my ( $opt, $usage ) = describe_options(
24 "$name %o <time> <text>+",
25 [],
26 ['Send yourself a reminder.'],
27 [],
28 [
29 'urgency' => hidden => {
30 one_of => [
31 [ 'nudge|n', 'low key reminder' ],
32 [
33 'poke|p',
34 'reminder with no particular urgency (default)'
35 ],
36 [ 'shake|s', 'urgent reminder' ],
37 [ 'slap', 'do this!!!' ],
38 ]
39 }
40 ],
41 [],
42 [ 'help', "print usage message and exit" ],
43 );
44  
45 1 ▶ print( $usage->text ), exit if $opt->help;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->help 0 1 1
46 1 given ( scalar @ARGV ) {
47 1 when (0) {
48 0 $usage->die(
49 {
50 pre_text => "ERROR: No time or message.\n\n"
51 }
52 )
53 }
54 1 when (1) {
55 0 $usage->die(
56 {
57 pre_text => "ERROR: No message.\n\n"
58 }
59 )
60 }
61 }
62 1 return ( $opt, $usage, $name );
63 }
64  
65  
66 1 ▶sub validate_time {
Subroutine validate_time: called CC=14 SCAR=51.2
Pod documented
67 1 my ( undef, $opt, $usage, $time, @args ) = @_;
68 1 require DateTime;
69 1 require DateTime::TimeZone;
70  
71 # parse time
72 1 ▶ $usage->die(
Branch: 1 branch
BranchTrueFalseTotal
unless my(%props) = _parse_time($time) 0 1 1
73 {
74 pre_text => "ERROR: could not understand time expression: $time\n\n"
75 }
76 ) unless my %props = _parse_time($time);
77 1 my $tz = DateTime::TimeZone->new( name => 'local' );
78 0 my $now = DateTime->now( time_zone => $tz );
79 0 my $then = $now->clone;
80  
81 0 ▶ if ( $props{unit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props{'unit'}) { } 0 0 0
82 0 my $unit = $props{unit};
83 0 given ($unit) {
84 0 when ('h') { $unit = 'hours' }
85 0 when ('m') { $unit = 'minutes' }
86 0 when ('s') { $unit = 'seconds' }
87 }
88 0 $then->add( $unit => $props{time} );
89 }
90 else {
91 0 my ( $hour, $minute ) = @props{qw(hour minute)};
92 0 ▶ $usage->die( { pre_text => "ERROR: impossible time\n\n" } )
Branch: 1 branch
BranchTrueFalseTotal
unless $hour < 25 and $minute < 60 0 0 0
Condition0%0/3
$hour < 25 and $minute < 60
!ll&&!rl&&r
000
MC/DC0%0/2
$hour < 25 and $minute < 60
$hour < 25 $minute < 60
Truth table0%0/3
A and B
A= $hour < 25B= $minute < 60
ABresult
0X0
100
111
93 unless $hour < 25 && $minute < 60;
94 0 my $suffix = $props{suffix};
95 0 ▶ $usage->die( { pre_text => "ERROR: impossible time\n\n" } )
Branch: 1 branch
BranchTrueFalseTotal
if $hour > 12 and $suffix eq "a" 0 0 0
Condition0%0/3
$hour > 12 and $suffix eq "a"
!ll&&!rl&&r
000
MC/DC0%0/2
$hour > 12 and $suffix eq "a"
$hour > 12 $suffix eq "a"
Truth table0%0/3
A and B
A= $hour > 12B= $suffix eq "a"
ABresult
0X0
100
111
96 if $hour > 12 && $suffix eq 'a';
97 0 $then->set( hour => $hour, minute => $minute, second => 0 );
98 0 ▶ if ( $hour < 13 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hour < 13) { } 0 0 0
99 0 ▶ $then->add( hours => 12 ) while $then < $now;
Branch: 1 branch
BranchTrueFalseTotal
$then < $now and do { $then->add("hours", 12) } 0 0 0
100 0 given ($suffix) {
101 0 ▶ when ('a') { $then->add( hours => 12 ) if $then->hour >= 12 }
Branch: 1 branch
BranchTrueFalseTotal
if $then->hour >= 12 0 0 0
102 0 ▶ when ('p') { $then->add( hours => 12 ) if $then->hour < 12 }
Branch: 1 branch
BranchTrueFalseTotal
if $then->hour < 12 0 0 0
103 }
104 }
105 else {
106 0 ▶ $then->add( days => 1 ) if $then < $now;
Branch: 1 branch
BranchTrueFalseTotal
if $then < $now 0 0 0
107 }
108 }
109 0 my $seconds = $then->epoch - $now->epoch;
110 0 ▶ $seconds = 0 if $seconds < 0; # same moment
Branch: 1 branch
BranchTrueFalseTotal
if $seconds < 0 0 0 0
111  
112 # set verbosity level
113 0 my $verbosity;
114 0 given ( $opt->urgency ) {
115 0 when ('nudge') { $verbosity = 0 }
116 0 when ('poke') { $verbosity = 1 }
117 0 when ('shake') { $verbosity = 2 }
118 0 when ('slap') { $verbosity = 3 }
119 0 default { $verbosity = 1 }
120 };
121  
122 # generate message text and synopsis
123 0 my $text = join ' ', @args;
124 0 $text =~ s/^\s++|\s++$//g;
125 0 $text =~ s/\s++/ /g;
126 0 ( my $synopsis = $text ) =~ s/^(\S++(?: \S++){0,3}).*/$1/;
127 0 ▶ $synopsis .= ' ...' if length($text) - length($synopsis) > 4;
Branch: 1 branch
BranchTrueFalseTotal
if length($text) - length($synopsis) > 4 0 0 0
128 0 return ( $verbosity, $text, $synopsis, $seconds );
129 }
130  
131 # extract useful bits out of a time expression
132 # tried to do this with a more readable recursive regex and callbacks but got
133 # OOM errors at unpredictable intervals so I gave up
134 1 ▶sub _parse_time {
Subroutine _parse_time: called CC=3 SCAR=15.0
135 1 my %props;
136 1 given ( $_[0] ) {
137 1 when (/^(\d++)([hms])$/i) { @props{qw(time unit)} = ( $1, lc $2 ) }
138 0 when (/^(\d{1,2})(?::(\d{2}))?(?:([ap])(\.?)m\4)?$/i) {
139 0 ▶ @props{qw(hour minute suffix)} = ( $1, $2 || 0, lc( $3 || '' ) )
Condition0%0/4
$2 || 0
l!l
00
$3 || ''
l!l
00
MC/DC0%0/1
$2 || 0
$2
MC/DC0%0/1
$3 || ''
$3
Truth table0%0/2
A
A= $2
Aresult
11
00
Truth table0%0/2
A
A= $3
Aresult
11
00
140 }
141 }
142 1 return %props;
143 }
144  
145  
146 0 ▶sub run {
Subroutine run: not called CC=3 SCAR=24.8
Pod documented
147 0 my ( undef, $name, $seconds, $text, $synopsis, $verbosity ) = @_;
148 0 ▶ unless (fork) {
Branch: 1 branch
BranchTrueFalseTotal
unless (fork) 0 0 0
149 0 require Gtk2::Notify;
150 0 Gtk2::Notify->init($name);
151  
152 0 sleep $seconds;
153  
154 0 my $icon = _icon( $name, $verbosity );
155 0 ▶ if ( $verbosity == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbosity == 3) { } 0 0 0
156 0 require App::Nag::Slap;
157 0 App::Nag::Slap->run( $synopsis, $text, $icon );
158 }
159 else {
160 0 Gtk2::Notify->new( $synopsis, $text, $icon )->show;
161 }
162 0 unlink $icon;
163 }
164 }
165  
166 # make a somewhat eye-catching icon
167 0 ▶sub _icon {
Subroutine _icon: not called CC=1 SCAR=6.9
168 0 my ( $name, $verbosity ) = @_;
169 0 require File::Temp;
170  
171 0 my $phrase = PHRASE->[$verbosity];
172 0 my $fill = FILL->[$verbosity];
173 0 my $stroke = STROKE->[$verbosity];
174 0 my $font_size = FONT_SIZE->[$verbosity];
175 0 my ( $x, $y ) = @{ XY->[$verbosity] };
176 0 my $text = <<END;
177 <?xml version="1.0" encoding="utf-8" standalone="no"?>
178 <!--
179 Created by $name with some help from Inkscape (http://www.inkscape.org/)
180 -->
181 <svg xmlns:dc="http://purl.org/dc/elements/1.1/"
182 xmlns:cc="http://creativecommons.org/ns#"
183 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
184 xmlns:svg="http://www.w3.org/2000/svg"
185 xmlns="http://www.w3.org/2000/svg" width="64px" height="64px"
186 version="1.1">
187 <g id="layer1">
188 <rect style="fill:#$fill;fill-rule:evenodd;stroke:#$stroke;stroke-width:3px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1"
189 id="rect2993" width="62" height="62" x="1" y="1" />
190 <text xml:space="preserve"
191 style="font-size:${font_size}px;font-style:normal;font-weight:bold;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#$stroke;fill-opacity:1;stroke:none;font-family:Monospace;opacity:1"
192 x="12.525171" y="28.595528" id="text3763">
193 <tspan id="tspan3765" x="$x" y="$y">$phrase</tspan>
194 </text>
195 </g>
196 </svg>
197 END
198 0 my ( $fh, $filename ) =
199 File::Temp::tempfile( TEMPLATE => 'nagXXXXXX', SUFFIX => '.svg' );
200 0 print $fh $text;
201 0 $fh->close;
202 0 return $filename;
203 }
204  
205 1 1;
206  
207 __END__
↑ Summary Nag/Slap.pm »