Kusarigama/Plugin/Command/Open.pm

filter: Statementstmt 25.8% 16 / 62 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 4 / 7 Podpod 33.3% 1 / 3 totaltotal 22.1% 21 / 95 CC 14 SCAR 45.6
CC
14
Cov
19%
on_command
156.0
setup
12.0
expand
2.0
CRAP 95.7
↑ Summary Kusarigama/Plugin/Command/Progress.pm »
1 package Taskwarrior::Kusarigama::Plugin::Command::Open;
2 1 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: open links associated to a task
4 1 $Taskwarrior::Kusarigama::Plugin::Command::Open::VERSION = '0.10.0';
5  
6 1 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use List::AllUtils qw/ pairgrep pairmap pairs /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 extends 'Taskwarrior::Kusarigama::Plugin';
13 1 with 'Taskwarrior::Kusarigama::Hook::OnCommand';
14  
15 1 ▶use experimental qw/ postderef signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub setup {
18 0 ▶ my $self = shift;
Subroutine setup: not called CC=3 SCAR=24.8
Pod documented
19  
20 0 for ( qw/ https http / ) {
21 0 ▶ next if eval { $self->tw->config->{kusarigama}{plugin}{open}{$_} };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->tw->config->{"kusarigama"}{"plugin"}{"open"}{$_} } } 0 0 0
22 0 say "adding '$_' format";
23 0 $self->tw->run_task->config(
24 [{ 'rc.confirmation' => 'off' }],
25 'kusarigama.plugin.open.'.$_ => "xdg-open {{{link}}}"
26 );
27 }
28 }
29  
30 sub on_command {
31 0 ▶ my $self = shift;
Subroutine on_command: not called CC=12 SCAR=50.5
Pod undocumented
32  
33 0 my $args = $self->args;
34 0 my( $id, $type ) = $args =~ /^task\s+(?<id>.*?)\s+open\s*(?<type>[\w-]*)/g;
35  
36 0 ▶ die "no task provided\n" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
37  
38 0 my $prefixes = eval { $self->tw->config->{kusarigama}{plugin}{'open'} };
39  
40 0 my @tasks = $self->export_tasks($id);
41  
42 0 ▶ die "task '$id' not found\n" unless @tasks;
Branch: 1 branch
BranchTrueFalseTotal
unless @tasks 0 0 0
43 0 ▶ die "'open' requires a single task\n" if @tasks > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @tasks > 1 0 0 0
44  
45 my @links =
46 0 map { [ $_, split ':', $_, 2 ] }
47 0 grep { /:/ }
48 0 map { $_->{description} }
49 0 eval { $tasks[0]->{annotations}->@* };
50  
51 0 ▶ if( $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 0 0 0
52 0 my( $subtype ) = split '-', $type;
53 0 @links = grep { $_->[1] eq $subtype } @links;
54 }
55  
56 0 ▶ unless( @links ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@links) 0 0 0
57 0 die "found nothing to open\n";
58 }
59  
60 0 ▶ if( @links > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@links > 1) 0 0 0
61  
62 0 ▶ open my $tty, '+<', '/dev/tty' or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $tty, "+<", "/dev/tty" or die $! 0 0 0
63  
64 0 my $i = 0;
65 0 $tty->say( '' );
66 0 $tty->say( '0: ALL THE THINGS!' );
67 0 for ( @links ) {
68 0 $tty->say( ++$i, ': ', $_->[0] );
69 }
70  
71 0 require IO::Prompt::Simple;
72  
73 0 my @answers = IO::Prompt::Simple::prompt( 'which one(s)? ', {
74 choices => [ 0..$i ],
75 input => $tty,
76 output => $tty,
77 default => 1,
78 multi => 1,
79 });
80  
81 0 ▶ if( grep { $_ > 0 } @answers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ > 0;} @answers) 0 0 0
82 0 @links = @links[ map { $_-1} @answers ];
83 }
84 }
85  
86 0 for my $l ( @links ) {
87 0 my( $link, $link_type ) = $l->@*;
88 0 ▶ my $command = $prefixes->{ $type || $link_type };
Condition0%0/3
$type || $link_type
l!l&&r!l&&!r
000
MC/DC0%0/2
$type || $link_type
$type $link_type
Truth table0%0/3
A || B
A= $typeB= $link_type
ABresult
1X1
011
000
89 0 $command = $self->expand( $command, $link, @tasks );
90 0 system $command;
91 }
92  
93  
94 };
95  
96  
97 ▶sub expand( $self, $command, $link, $task ) {
Subroutine expand: not called CC=1 SCAR=6.9
Pod undocumented
98 0 require Template::Mustache;
99  
100 0 return Template::Mustache->render(
101 $command, {
102 task => $task,
103 path => ( split ':', $link, 2 )[1],
104 link => $link,
105 }
106 );
107 }
108  
109 1 1;
110  
111 __END__
112  
113 =pod
114  
115 =encoding UTF-8
116  
117 =head1 NAME
118  
119 Taskwarrior::Kusarigama::Plugin::Command::Open - open links associated to a task
120  
121 =head1 VERSION
122  
123 version 0.10.0
124  
125 =head1 SYNOPSIS
126  
127 # open the link(s) of task 123
128 $ task 123 open
129  
130 # only consider the wiki link
131 $ task 123 open wiki
132  
133 =head1 DESCRIPTION
134  
135 Looks into the annotations of the task for link thingies, and open them.
136  
137 If the command finds exactly one link, it'll open it. If more than one is found,
138 you'll be given the choice of which one you wish to launch.
139  
140 The format for annotated links is C<format:path>. The different formats live in
141 the F<.taskrc>. When installed, the plugin will set up the C<http> and C<https> format,
142 but you can add as many as you want. E.g.
143  
144 $ task config kusarigama.plugin.open.http 'xdg-open {{{link}}}'
145 $ task config kusarigama.plugin.open.https 'xdg-open {{{link}}}'
146 $ task config kusarigama.plugin.open.wiki 'tmux split-window "nvim /home/yanick/vimwiki/{{{path}}}.mkd"'
147  
148 The commands are Mustache templates (using L<Template::Mustache>). The context provided
149 to the template has three variables: C<link> (e.g., C<wiki:my_page>), C<path> (C<my_page>)
150 and C<task>, which is the associated task object.
151  
152 Note that in the examples I'm using the triple bracket notation such that the '/' in the paths don't get escaped.
153  
154 If you want to set more than one opening action for a type, append C<-action> to it. E.g.:
155  
156 $ task config kusarigama.plugin.open.wiki 'cat "/home/yanick/vimwiki/{{{path}}}.mkd"'
157 $ task config kusarigama.plugin.open.wiki-edit 'tmux split-window "nvim /home/yanick/vimwiki/{{{path}}}.mkd"'
158  
159 $ task open wiki # prints it
160 $ task open wiki-edit # opens editor
161  
162 =head1 INSTALLATION
163  
164 $ task-kusarigama add Command::Plugin
165  
166 =head1 SEE ALSO
167  
168 L<https://github.com/ValiValpas/taskopen> - shell-based inspiration for
169 this plugin.
170  
171 =head1 AUTHOR
172  
173 Yanick Champoux <yanick@cpan.org>
174  
175 =head1 COPYRIGHT AND LICENSE
176  
177 This software is copyright (c) 2018, 2017 by Yanick Champoux.
178  
179 This is free software; you can redistribute it and/or modify it under
180 the same terms as the Perl 5 programming language system itself.
181  
182 =cut
↑ Summary Kusarigama/Plugin/Command/Progress.pm »