blib/lib/Neovim/RPC/Plugin/Taskwarrior.pm

filter: Statementstmt 26.2% 38 / 145 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 16.0% 9 / 56 Podpod 0.0% 0 / 6 totaltotal 20.6% 47 / 228 CC 21 SCAR 19.5
CC
21
Cov
23%
relative_time
42.0
task_line
12.4
__ANON__
12.0
CRAP 7.1
↑ Summary
1 package Neovim::RPC::Plugin::Taskwarrior;
2 1 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: UI for taskwarrior
4 1 $Neovim::RPC::Plugin::Taskwarrior::VERSION = '0.0.1';
5  
6 1 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Neovim::RPC::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Promises qw/ collect deferred /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Taskwarrior::Kusarigama::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use experimental 'signatures', 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 has task => (
18 is => 'ro',
19 lazy => 1,
20 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 Taskwarrior::Kusarigama::Wrapper->new
22 },
23 1 );
24  
25 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my $self = shift;
27  
28 0 ▶ $self->api->ready->then(sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 $self->api->nvim_command( 'set filetype=task' )
30 })
31 0 ▶ ->then(sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 $self->api->nvim_command( "call TW_show('+PENDING')")
33 0 });
34
35 }
36  
37 0 ▶before register => sub($self) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
38 push @$_, {
39 0 ▶ catch => sub($self,$warn,@) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
40 0 ▶ $warn = @$warn if ref $warn eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $warn eq "ARRAY" 0 0 0
41 0 $warn =~ s/\n.*//s;
42 0 $self->rpc->api->vim_command( qq{echo "perl error: $warn"} );
43 0 warn $warn;
44 }
45 0 } for values $self->subscriptions->%*;
46 1 };
47  
48 subscribe tw_done =>
49 0 ▶ sub($self,$event) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 $self->extract_uuids($event->params->[0]->@*)
51 }
52 0 ▶ => sub($self,@uuids) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
53 0 warn "here", time;
54 #$self->rpc->api->vim_command( "g/$_/d" ) for @uuids;
55 0 $self->rpc->send_notification( 'vim_command', [ "g/$_/d" ] ) for @uuids;
56 0 warn "there", time;
57 0 ▶ deferred(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
58 0 $self->task->done( [ "confirmation:no" ], $_ ) for @uuids;
59 0 });
60 1 };
61  
62 subscribe tw_mod => rpcrequest
63 0 ▶ sub( $self, $event ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 my( $mod, $from, $to, $lines ) = $event->all_params;
65 0 my @uuids = $self->extract_uuids($lines->@*);
66 0 my(undef, @condition) = map { ( 'or', "uuid:$_" ) } @uuids;
67  
68 0 $self->task->mod( [ @condition ], $mod );
69  
70 $self->rpc->api->vim_get_current_buffer
71 0 ▶ ->then(sub{ ord $_[0]->data })
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 ▶ ->then(sub($buffer_id) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 $self->rpc->api->nvim_buf_set_lines( $buffer_id, $from-1, $to, 0, [
74 0 map { $self->task_line($_) } $self->task->export( [ @condition ] )
75 ])
76 })
77 0 }
78 1 ;
79  
80 subscribe tw_append => rpcrequest
81 0 ▶ sub( $self, $event ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my( $mod, $from, $to, $lines ) = $event->all_params;
83 0 my @uuids = $self->extract_uuids($lines->@*);
84 0 my(undef, @condition) = map { ( 'or', "uuid:$_" ) } @uuids;
85  
86 0 $self->task->append( [ @condition ], $mod );
87  
88 $self->rpc->api->vim_get_current_buffer
89 0 ▶ ->then(sub{ ord $_[0]->data })
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 ▶ ->then(sub($buffer_id) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 $self->rpc->api->nvim_buf_set_lines( $buffer_id, $from-1, $to, 0, [
92 0 map { $self->task_line($_) } $self->task->export( [ @condition ] )
93 ])
94 })
95 0 }
96 1 ;
97  
98 subscribe tw_wait => rpcrequest(
99 0 ▶ sub( $self, $event ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 my( $wait, $from, $to, $lines ) = $event->all_params;
101 0 ( $wait, $self->extract_uuids($lines->@*) );
102 },
103 0 ▶ sub( $self, $wait, @uuids ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
104 0 $self->rpc->send_notification( 'vim_command', [ "g/$_/d" ] ) for @uuids;
105 0 ( $wait, @uuids );
106 } ),
107 0 ▶ sub ( $self, $wait, @uuids ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 my $condition = join ' or ', map { "uuid:$_" } @uuids;
109  
110 0 $self->task->wait( [ 'confirmation:no', $condition ], $wait );
111  
112 }
113 1 ;
114  
115 subscribe tw_delete => rpcrequest
116 0 ▶ sub($self,$event) { $self->extract_uuids($event->params->[0]->@*) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 0 ▶ => sub($self,@uuids) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
118 0 my @promises;
119 0 for my $uuid ( @uuids ) {
120 # TODO make this promisable?
121 0 $self->task->delete( [ "rc.confirmation:no" ], $uuid );
122 0 ▶ push @promises, $self->rpc->api->vim_command( "g/$uuid/d" )->then(sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 0 $self->rpc->api->vim_command( "echo 'deleted $uuid'" );
124 0 } );
125 }
126 0 return collect @promises;
127 1 };
128  
129 subscribe tw_info => rpcrequest
130 0 ▶ sub($self,$event) { $self->extract_uuids($event->params->[0]->@*) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 ▶ => sub($self,@uuids) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
132 0 my $promise = deferred;
133 0 $promise->resolve;
134  
135 0 for my $uuid ( @uuids ) {
136 0 ▶ my @info = map { length $_ ? $_ : ' ' } $self->task->info($uuid);
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 0 0 0
137 0 ▶ $promise = $promise->then(sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 $self->api->vim_command('new');
139 })
140 0 ▶ ->then(sub{ $self->rpc->api->vim_get_current_buffer })
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 ▶ ->then(sub{ ord $_[0]->data })
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 ▶ ->then(sub($buffer_id) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 $self->rpc->api->buffer_insert( $buffer_id, 0, [
144 @info
145 ])
146 })
147 0 ▶ ->then(sub { $self->rpc->api->vim_input( '1G' ) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
148  
149 }
150  
151 0 return $promise;
152 1 };
153  
154  
155 subscribe tw_show => rpcrequest
156 0 ▶ sub( $self, $event ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 0 my $buffer_id;
158  
159 $self->rpc->api->vim_get_current_buffer
160 0 ▶ ->then(sub{ $buffer_id = ord $_[0]->data })
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 0 ▶ ->then(sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
162 0 my @tasks = $self->task->export( '+READY', $event->all_params );
163  
164 my @things =
165 0 map { $self->task_line($_) }
166 0 sort { $b->{urgency} - $a->{urgency} }
167 @tasks;
168  
169 0 s/\n/ /g for @things;
170  
171 0 return @things;
172 })
173 0 ▶ ->then( sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
174 0 $self->rpc->api->nvim_buf_set_lines( $buffer_id, 0, 1E6, 0, [ @_ ] );
175 })
176 0 },
177 0 ▶ sub { $_[0]->rpc->api->vim_input( '1G' ); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 ▶ sub { $_[0]->rpc->api->vim_command( ':TableModeRealign' ); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
179
180 0 ▶sub tw_output($self,@lines) {
Subroutine tw_output: not called CC=1 SCAR=6.9
Pod undocumented
181 $self->tw_output_window
182 0 ▶ ->then( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 0 $self->rpc->api->nvim_buf_set_lines( $_[0], -1, -1, 0, [ '', @lines, '' ] )
184 0 });
185 }
186  
187 0 ▶sub extract_uuids($self,@lines) {
Subroutine extract_uuids: not called CC=1 SCAR=6.9
Pod undocumented
188 0 my $o = '[a-f0-9]';
189 0 map { /($o{8}(?:-$o{4}){3}-$o{12})/g } @lines;
190 }
191  
192 0 ▶sub task_line($self,$task) {
Subroutine task_line: not called CC=5 SCAR=25.2
Pod undocumented
193 0 $task->{urgency} = sprintf "%03d", $task->{urgency};
194 0 ▶ $task->{tags} &&= join ' ', $task->{tags}->@*;
Condition0%0/3
$task->{"tags"} &&= join(" ", @{$task->{"tags"}})
!ll&&!rl&&r
000
MC/DC0%0/2
$task->{"tags"} &&= join(" ", @{$task->{"tags"}})
$task->{"tags"} join(" ", @{$task->{"tags"}})
Truth table0%0/3
A &&= B
A= $task->{"tags"}B= join(" ", @{$task->{"tags"}})
ABresult
0X0
100
111
195  
196 # if ( length $task->{description} > 30 ) {
197 # $task->{description} = ( substr $task->{description}, 0, 27 ) . '...';
198 # }
199  
200 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
201 0 ▶ if ( length $task->{project} > 15 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $task->{"project"} > 15) 0 0 0
202 0 $task->{project} = ( substr $task->{description}, 0, 12 ) . '...';
203 }
204  
205 0 $task->{$_} = relative_time($task->{$_}) for qw/ due /;
206 0 $task->{$_} = relative_time($task->{$_},-1) for qw/ modified /;
207  
208 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
209 return join '|', undef,
210 0 $task->@{qw/ urgency priority due description project tags modified uuid /},
211 undef;
212 }
213  
214 0 ▶sub relative_time($date,$mult=1) {
Subroutine relative_time: not called CC=6 SCAR=37.4
Pod undocumented
215 0 state $now = DateTime->now;
216  
217 0 ▶ return unless $date;
Branch: 1 branch
BranchTrueFalseTotal
unless $date 0 0 0
218  
219 # fine, I'll calculate it like a savage
220  
221 0 $date = DateTime::Format::ISO8601->parse_datetime($date)->epoch;
222  
223 0 my $delta = int( $mult * ( $date - time ) / 60 / 60 / 24 );
224  
225 0 ▶ return int($delta/365) . 'y' if abs($delta) > 365;
Branch: 1 branch
BranchTrueFalseTotal
if abs $delta > 365 0 0 0
226 0 ▶ return int($delta/30) . 'm' if abs($delta) > 30;
Branch: 1 branch
BranchTrueFalseTotal
if abs $delta > 30 0 0 0
227 0 ▶ return int($delta/7) . 'w' if abs($delta) > 7;
Branch: 1 branch
BranchTrueFalseTotal
if abs $delta > 7 0 0 0
228 0 return $delta . 'd';
229 }
230  
231 0 ▶sub tw_output_window ($self) {
Subroutine tw_output_window: not called CC=1 SCAR=6.9
Pod undocumented
232 $self->_tw_find_output_window()
233 0 ▶ ->catch(sub{ $self->_tw_create_output_window()
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 0 ▶ ->then(sub{ $self->_tw_find_output_window() })
Subroutine __ANON__: not called CC=1 SCAR=6.9
235 0 })
236 0 }
237  
238 0 ▶sub _tw_create_output_window($self) {
Subroutine _tw_create_output_window: not called CC=1 SCAR=6.9
239 0 $self->rpc->api->vim_command( ':pedit ~/.task/nvim_task.log');
240 }
241  
242 0 ▶sub _tw_find_output_window($self) {
Subroutine _tw_find_output_window: not called CC=1 SCAR=6.9
243 0 ▶ $self->rpc->api->nvim_list_bufs->then(sub($list){
Subroutine __ANON__: not called CC=2 SCAR=17.9
244 0 my $promise = deferred;
245 0 $promise->reject;
246  
247 0 for my $id ( map { ord $_->data } @$list ) {
248 0 ▶ $promise = $promise->catch(sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 $self->rpc->api->nvim_buf_get_name($id)
250 0 ▶ ->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
251 0 ▶ $_[0] =~ /nvim_task\.log/ ? $id : die } );
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /nvim_task\.log/u ? : 0 0 0
252 0 });
253 }
254  
255  
256 0 return $promise;
257 })
258 0 }
259  
260  
261 1 1;
262  
263 __END__
↑ Summary