Kusarigama/App/Decimate.pm

filter: Statementstmt 43.7% 42 / 96 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 44.4% 12 / 27 Podpod 0.0% 0 / 7 totaltotal 37.5% 54 / 144 CC 8 SCAR 16.3
CC
8
Cov
38%
pick_decimate
20.0
decimate_group
12.0
async_do
6.0
CRAP 5.1
« Kusarigama/Plugin/GitCommit.pm ↑ Summary Kusarigama/Plugin/Command/AndAfter.pm »
1 package Taskwarrior::Kusarigama::App::Decimate;
2 1 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: interactively re-prioritize tasks
4 1 $Taskwarrior::Kusarigama::App::Decimate::VERSION = '0.10.0';
5  
6 1 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Taskwarrior::Kusarigama::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::UtilsBy qw/ partition_by /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::AllUtils qw/ pairmap sum shuffle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Term::ANSIScreen qw/:screen :cursor :color/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use IO::Prompt::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Prompt::ReadKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use MooseX::App::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use MooseX::MungeHas;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use experimental 'postderef', 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 parameter subcommand => (
25 is => 'ro',
26 );
27  
28 has tw => sub {
29 0 ▶ Taskwarrior::Kusarigama::Wrapper->new
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 1 };
31  
32 has tasks => (
33 is => 'ro',
34 clearer => 1,
35 lazy => 1,
36 default => sub {
37 0 ▶ return +{ partition_by { $_->{priority} || 'U' } $_[0]->tw->export( '+READY' ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_->{"priority"} or "U" 0 0 0
38 }
39 1 );
40  
41 # for things that we don't want to wait to
42 # see the result. We just fire out and
43 # stop thinking about it
44 ▶sub async_do ($self,$code) {
Subroutine async_do: not called CC=2 SCAR=17.9
Pod undocumented
45 0 ▶ return if fork;
Branch: 1 branch
BranchTrueFalseTotal
if fork 0 0 0
46  
47 0 $code->();
48 0 exit;
49 }
50  
51 has _prompt => (
52 is => 'ro',
53 lazy => 1,
54 0 ▶ default => sub { Prompt::ReadKey->new; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 1 handles => { 'menu_prompt' => 'prompt' },
56 );
57  
58 ▶sub nbr_prioritized_tasks($self) {
Subroutine nbr_prioritized_tasks: not called CC=1 SCAR=6.9
Pod undocumented
59 0 return sum map { scalar $self->tasks->{$_}->@* } qw/ H M L /;
60 }
61  
62 ▶sub run($self) {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
63 0 $self->decimate_group( 0.10, 'H', 'M' );
64 0 $self->clear_tasks;
65 0 $self->decimate_group( 0.60, 'M', 'L' );
66 }
67  
68 ▶sub decimate_group($self,$target,$prio1,$prio2) {
Subroutine decimate_group: not called CC=3 SCAR=24.8
Pod undocumented
69 0 my $nbr = int $target * $self->nbr_prioritized_tasks;
70  
71 0 my $main = $self->tasks->{$prio1};
72  
73 0 say "Decimating $prio1 priority...";
74 0 say "We have @{[ scalar @$main ]}, we want $nbr";
75  
76 # do we have enough?
77 0 ▶ while( $nbr > @$main ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nbr > @$main) 0 0 0
78 0 say "need ", $nbr - @$main, ' more ', $prio1, ' tasks';
79 0 say "Promote one!\n\n";
80 0 my $task = $self->pick_decimate( $self->tasks->{$prio2} );
81  
82 0 push @$main, $task;
83 $self->async_do(sub{
84 0 ▶ $task->mod( 'priority:' . $prio1 );
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 });
86 }
87  
88 # do we have too much?
89 0 ▶ while( $nbr < @$main ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nbr < @$main) 0 0 0
90 0 say "need ", @$main - $nbr, ' less ', $prio1, ' tasks';
91 0 say "Demote one!\n\n";
92 0 my $task = $self->pick_decimate( $self->tasks->{$prio1} );
93  
94 0 shift @$main;
95 $self->async_do(sub{
96 0 ▶ $task->mod( 'priority:' . $prio2);
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 });
98 }
99 }
100  
101 ▶sub pick_decimate($self, $tasks ) {
Subroutine pick_decimate: not called CC=4 SCAR=30.0
Pod undocumented
102 0 my @contenders = (shuffle @$tasks)[0..9];
103  
104 0 for ( 0..9 ) {
105 0 my $c = $contenders[$_];
106  
107 printf "%2d %4d %s%s%s\n",
108 $_, $contenders[$_]{id},
109 colored( ['blue'], $c->{project} ? '['.$c->{project}.'] ' : '' ),
110 $contenders[$_]{description},
111 0 ▶ ( join ' ', map { colored [ 'cyan' ], " +$_" } @{ $c->{tags} } );
Branch: 1 branch
BranchTrueFalseTotal
$c->{'project'} ? : 0 0 0
112 }
113  
114 0 print "\n\n";
115  
116 my $action = $self->menu_prompt( prompt => "which one?",
117 options => [
118 { name => 'quit', keys => [ 'q' ] },
119 0 map { +{ keys => [ $_ ], name => $_ } } 0..9
120 ],
121 help_keys => [ '?' ],
122 );
123  
124 0 ▶ exit if $action eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "quit" 0 0 0
125  
126 0 @$tasks = grep { $_->{uuid} ne $contenders[$action]->{uuid} } @$tasks;
127  
128 0 return $contenders[$action];
129 }
130  
131 ▶sub wait_menu($self,$task) {
Subroutine wait_menu: not called CC=2 SCAR=17.9
Pod undocumented
132  
133 0 my $action = $self->menu_prompt( prompt => "how long?",
134 case_insensitive => 0,
135 options => [
136 { name => 'eow', doc => 'end of week priority', keys => [ 'e' ] },
137 { name => '1w', doc => 'one week', keys => [ 'w' ] },
138 { name => '1m', doc => 'one month', keys => [ 'm' ] },
139 { name => '3m', doc => 'three months', keys => [ 'M' ] },
140 { name => 'edit', doc => 'custom', keys => [ '.' ] },
141 ],
142 help_keys => [ '?' ],
143 );
144  
145 0 ▶ if( $action eq 'edit' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "edit") 0 0 0
146 0 $action = prompt 'wait';
147 }
148  
149 $self->async_do(sub{
150 0 ▶ $task->mod( 'wait:'.$action );
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 });
152 }
153  
154 ▶sub print_summary_line($self) {
Subroutine print_summary_line: not called CC=1 SCAR=6.9
Pod undocumented
155  
156 0 ▶ my %prio = pairmap { $a => scalar @$b } $self->tasks->%*;
Subroutine __ANON__: not called CC=1 SCAR=6.9
157  
158 0 my @colors = ( H => 'red', M => 'blue', L => 'cyan', 'U' => 'green' );
159  
160 0 ▶ say join ' ', pairmap { colored [ $b ], $prio{$a}, $a } @colors;
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 }
162  
163 1 1;
164  
165 __END__
166  
167 =pod
168  
169 =encoding UTF-8
170  
171 =head1 NAME
172  
173 Taskwarrior::Kusarigama::App::Decimate - interactively re-prioritize tasks
174  
175 =head1 VERSION
176  
177 version 0.10.0
178  
179 =head1 SYNOPSIS
180  
181 $ task-kusarigama decimate
182  
183 =head1 DESCRIPTION
184  
185 This command helps re-prioritize tasks. It assumes that we want
186 10% of the tasks as high priority, 60% as medium priority, and the remaining 30% as
187 low priority. If the distribution of the tasks does not match those proportions,
188 it will show 10 tasks of the category that has too many tasks, and ask you to pick
189 one to be promoted/demoted.
190  
191 =head1 AUTHOR
192  
193 Yanick Champoux <yanick@cpan.org>
194  
195 =head1 COPYRIGHT AND LICENSE
196  
197 This software is copyright (c) 2018, 2017 by Yanick Champoux.
198  
199 This is free software; you can redistribute it and/or modify it under
200 the same terms as the Perl 5 programming language system itself.
201  
202 =cut
« Kusarigama/Plugin/GitCommit.pm ↑ Summary Kusarigama/Plugin/Command/AndAfter.pm »