File Coverage

blib/lib/Bash/Completion.pm
Criterion Covered Total %
statement 49 49 100.0
branch 7 12 58.3
condition n/a
subroutine 10 10 100.0
pod 4 4 100.0
total 70 75 93.3


line stmt bran cond sub pod time code
1             package Bash::Completion;
2             {
3             $Bash::Completion::VERSION = '0.008';
4             }
5              
6             # ABSTRACT: Extensible system to provide bash completion
7              
8 3     3   1590 use strict;
  3         6  
  3         127  
9 3     3   14 use warnings;
  3         5  
  3         87  
10 3     3   1856 use Bash::Completion::Request;
  3         7  
  3         73  
11 3     3   6498 use Module::Load ();
  3         3545  
  3         92  
12             use Module::Pluggable
13 3         25 search_path => ['Bash::Completion::Plugins'],
14 3     3   14804 sub_name => 'plugin_names';
  3         36581  
15              
16              
17 4     4 1 15231 sub new { return bless {}, $_[0] }
18              
19              
20              
21             sub complete {
22 3     3 1 62 my ($self, $plugin, $cmd_line) = @_;
23              
24 3         9 my $class = "Bash::Completion::Plugins::$plugin";
25 3 50       11 return unless $self->_load_class($class);
26              
27 3         22 my $req = Bash::Completion::Request->new;
28 3         21 $class->new(args => $cmd_line)->complete($req);
29              
30 3         48 return $req;
31             }
32              
33              
34              
35             sub setup {
36 1     1 1 1886 my ($self) = @_;
37 1         3 my $script = '';
38              
39 1         4 for my $plugin ($self->plugins) {
40 2         11 my $cmds = $plugin->should_activate;
41 2 50       7 next unless @$cmds;
42              
43 2         10 my $snippet = $plugin->generate_bash_setup($cmds);
44              
45 2 50       7 if (ref $snippet) {
46 2         4 my $options = join(' ', map {"-o $_"} @$snippet);
  2         6  
47 2         3 my $plugin_name = ref($plugin);
48 2         9 $plugin_name =~ s/^Bash::Completion::Plugins:://;
49              
50 2         10 $snippet = join(
51             "\n",
52             map {
53 2         3 qq{complete -C 'bash-complete complete $plugin_name -- ' $options $_}
54             } @$cmds
55             );
56             }
57              
58 2 50       10 $script .= "$snippet\n" if $snippet;
59             }
60              
61 1         4 return $script;
62             }
63              
64              
65              
66             sub plugins {
67 2     2 1 1711 my ($self) = @_;
68              
69 2 100       11 unless ($self->{plugins}) {
70 1         2 my @plugins;
71              
72 1         3 for my $plugin_name ($self->plugin_names) {
73 2 50       903 next unless $self->_load_class($plugin_name);
74              
75 2         27 push @plugins, $plugin_name->new;
76             }
77              
78 1         5 $self->{plugins} = \@plugins;
79             }
80              
81 2         3 return @{$self->{plugins}};
  2         9  
82             }
83              
84              
85             #######
86             # Utils
87              
88             sub _load_class {
89 5     5   10 eval { Module::Load::load($_[1]); 1 };
  5         27  
  5         241  
90             }
91              
92             1;
93              
94              
95             __END__