| 1 | package Bash::Completion::Plugin; | ||
| 2 | { | ||
| 3 | 3 | $Bash::Completion::Plugin::VERSION = '0.008'; | |
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: base class for Bash::Completion plugins | ||
| 7 | |||
| 8 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | |||
| 12 | 7 | ▶ | sub new { |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||
| 13 | 7 | my $class = shift; | |
| 14 | 7 | my %args = (args => [], @_); | |
| 15 | |||
| 16 | 7 | return bless \%args, $class; | |
| 17 | } | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | 3 | ▶ | sub args { |
|
Subroutine
args: called CC=1 SCAR=0.0
Pod
documented
| |||
| 22 | 3 | my ($self) = @_; | |
| 23 | |||
| 24 | 3 | return @{$self->{args}}; | |
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | 1 | ▶ | sub should_activate { return [] } |
|
Subroutine
should_activate: called CC=1 SCAR=0.0
Pod
documented
| |||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | 2 | ▶ | sub generate_bash_setup { return [] } |
|
Subroutine
generate_bash_setup: called CC=1 SCAR=0.0
Pod
documented
| |||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | 3 | 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<Bash::Completion> plugins that provides the default | ||
| 93 | implementations for the required plugin methods. | ||
| 94 | |||
| 95 | See the L</SYNOPSIS> 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<should_activate()> 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<Bash::Completion::Utils> library has a C<command_in_path()> 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</should_activate> and must return a list of options to use when | ||
| 143 | creating the bash C<complete> 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<Bash::Completion> will call | ||
| 158 | this method with a L<Bash::Completion::Request> object, and your code | ||
| 159 | should use the Request C<candidates()> method to set the possible | ||
| 160 | completions. | ||
| 161 | |||
| 162 | The L<Bash::Completion::Utils> library has two functions, | ||
| 163 | C<match_perl_module()> and C<prefix_math()> that can be pretty | ||
| 164 | useful here. | ||
| 165 | |||
| 166 | =head1 AUTHOR | ||
| 167 | |||
| 168 | Pedro Melo <melo@cpan.org> | ||
| 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__ | ||