File Coverage

blib/lib/Bash/Completion/Plugin.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 4 4 100.0
total 24 24 100.0


line stmt bran cond sub pod time code
1             package Bash::Completion::Plugin;
2             {
3             $Bash::Completion::Plugin::VERSION = '0.008';
4             }
5              
6             # ABSTRACT: base class for Bash::Completion plugins
7              
8 3     3   1948 use strict;
  3         6  
  3         92  
9 3     3   16 use warnings;
  3         6  
  3         508  
10              
11              
12             sub new {
13 7     7 1 420 my $class = shift;
14 7         27 my %args = (args => [], @_);
15              
16 7         37 return bless \%args, $class;
17             }
18              
19              
20              
21             sub args {
22 3     3 1 507 my ($self) = @_;
23              
24 3         3 return @{$self->{args}};
  3         20  
25             }
26              
27              
28              
29 1     1 1 6 sub should_activate { return [] }
30              
31              
32              
33 2     2 1 9 sub generate_bash_setup { return [] }
34              
35              
36              
37             1;
38              
39              
40              
41             =pod
42              
43             =head1 NAME
44              
45             Bash::Completion::Plugin - base class for Bash::Completion plugins
46              
47             =head1 VERSION
48              
49             version 0.008
50              
51             =head1 SYNOPSIS
52              
53             ## Example plugin for xpto command
54             package Bash::Completion::Plugin::XPTO;
55            
56             use strict;
57             use warnings;
58             use parent 'Bash::Completion::Plugin';
59             use Bash::Completion::Utils qw( command_in_path );
60            
61             sub should_activate {
62             return [grep { command_in_path(_) } ('xpto')];
63             }
64            
65            
66             ## Optionally, for full control of the generated bash code
67             sub generate_bash_setup {
68             return q{complete -C 'bash-complete complete XPTO' xpto};
69             }
70            
71             ## Use plugin arguments
72             sub generate_bash_setup {
73             return q{complete -C 'bash-complete complete XPTO arg1 arg2 arg3' xpto};
74             }
75             ## $plugin->args will have ['arg1', 'arg2', 'arg3']
76            
77            
78             sub complete {
79             my ($self, $r) = @_;
80            
81             my @options = ('-h', '--help');
82             $r->candidates(prefix_match($r->word, @options));
83             }
84             1;
85              
86             =head1 DESCRIPTION
87              
88             WARNING: the most important class for Plugin writers is the Request
89             class. Please note that the Request class interface is Alpha-quality
90             software, and I will update it before 1.0.
91              
92             A base class for L plugins that provides the default
93             implementations for the required plugin methods.
94              
95             See the L for an example of a plugin.
96              
97             =head1 ATTRIBUTES
98              
99             =head2 args
100              
101             An list reference with plugin arguments.
102              
103             =head1 METHODS
104              
105             =head2 new
106              
107             A basic plugin constructor. Accepts a list of key/values. Accepted keys:
108              
109             =over 4
110              
111             =item args
112              
113             A list reference with parameters to this plugin.
114              
115             =back
116              
117             =head2 should_activate
118              
119             The method C is used by the automatic setup of
120             completion rules in the .bashrc. It should return a reference to a list
121             of commands that the plugin is can complete.
122              
123             If this method returns a reference to an empty list (the default), the
124             plugin will not be used.
125              
126             A common implementation of this method is to check the PATH for the
127             command we want to provide completion, and return the com only if that
128             command is found.
129              
130             The L library has a C that
131             can be pretty useful here.
132              
133             For example:
134              
135             sub should_activate {
136             return [grep { command_in_path($_) } qw( perldoc pod )];
137             }
138              
139             =head2 generate_bash_setup
140              
141             This method receives the list of commands that where found by
142             L and must return a list of options to use when
143             creating the bash C command.
144              
145             For example, if a plugin returns C<[qw( nospace default )]>, the
146             following bash code is generated:
147              
148             complete -C 'bash-complete complete PluginName' -o nospace -o default command
149              
150             By default this method returns a reference to an empty list.
151              
152             Alternatively, and for complete control, you can return a string with
153             the entire bash code to activate the plugin.
154              
155             =head2 complete
156              
157             The plugin completion logic. The class L will call
158             this method with a L object, and your code
159             should use the Request C method to set the possible
160             completions.
161              
162             The L library has two functions,
163             C and C that can be pretty
164             useful here.
165              
166             =head1 AUTHOR
167              
168             Pedro Melo
169              
170             =head1 COPYRIGHT AND LICENSE
171              
172             This software is Copyright (c) 2011 by Pedro Melo.
173              
174             This is free software, licensed under:
175              
176             The Artistic License 2.0 (GPL Compatible)
177              
178             =cut
179              
180              
181             __END__