| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package OSGi::Osgish::CompletionHandler; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use File::Spec; | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | OSGi::Osgish::CompletionHandler - Custom completion routines for readline. | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | =head1 METHODS | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $class = shift; | ||
| 24 | my $osgish = shift || die "No osgish object given"; | ||
| 25 | my $self = { | ||
| 26 | osgish => $osgish | ||
| 27 | }; | ||
| 28 | bless $self,(ref($class) || $class); | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub bundles { | ||
| 33 | my $self = shift; | ||
| 34 | my $osgish = $self->{osgish}; | ||
| 35 | |||
| 36 | return $self->_bundle_or_service(sub { $osgish->agent->bundle_ids(use_cached => 1) }, | ||
| 37 | sub { $osgish->agent->bundle_symbolic_names(use_cached => 1)}, | ||
| 38 | @_); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub services { | ||
| 42 | my $self = shift; | ||
| 43 | my $osgish = $self->{osgish}; | ||
| 44 | return $self->_bundle_or_service(sub { $osgish->agent->service_ids(use_cached => 1) }, | ||
| 45 | sub { $osgish->agent->service_object_classes(use_cached => 1)}, | ||
| 46 | @_); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub files_extended { | ||
| 50 | my $self = shift; | ||
| 51 | return sub { | ||
| 52 | my $term = shift; | ||
| 53 | my $cmpl = shift; | ||
| 54 | my $filter = undef; | ||
| 55 | |||
| 56 | $term->suppress_completion_append_character(); | ||
| 57 | |||
| 58 | use File::Spec; | ||
| 59 | my @path = File::Spec->splitdir($cmpl->{str} || "."); | ||
| 60 | my $dir = File::Spec->catdir(@path[0..$#path-1]); | ||
| 61 | my $lookup_dir = $dir; | ||
| 62 | if ($dir =~ /^\~(.*)/) { | ||
| 63 | my $user = $1 || ""; | ||
| 64 | $lookup_dir = glob("~$user"); | ||
| 65 | } | ||
| 66 | my $file = $path[$#path]; | ||
| 67 | $file = '' unless $cmpl->{str}; | ||
| 68 | my $flen = length($file); | ||
| 69 | |||
| 70 | my @files = (); | ||
| 71 | $lookup_dir = length($lookup_dir) ? $lookup_dir : "."; | ||
| 72 | if (opendir(DIR, $lookup_dir)) { | ||
| 73 | if ($filter) { | ||
| 74 | @files = grep { substr($_,0,$flen) eq $file && $file =~ $filter } readdir DIR; | ||
| 75 | } else { | ||
| 76 | @files = grep { substr($_,0,$flen) eq $file } readdir DIR; | ||
| 77 | } | ||
| 78 | closedir DIR; | ||
| 79 | # eradicate dotfiles unless user's file begins with a dot | ||
| 80 | @files = grep { /^[^.]/ } @files unless $file =~ /^\./; | ||
| 81 | # reformat filenames to be exactly as user typed | ||
| 82 | my @ret = (); | ||
| 83 | for my $file (@files) { | ||
| 84 | $file .= "/" if -d $lookup_dir . "/" . $file; | ||
| 85 | $file = $dir eq '/' ? "/$file" : "$dir/$file" if length($dir); | ||
| 86 | push @ret,$file; | ||
| 87 | } | ||
| 88 | return \@ret; | ||
| 89 | } else { | ||
| 90 | $term->completemsg("Couldn't read dir: $!\n"); | ||
| 91 | return []; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | sub servers { | ||
| 97 | my $self = shift; | ||
| 98 | return sub { | ||
| 99 | my ($term,$cmpl) = @_; | ||
| 100 | my $osgish = $self->{osgish}; | ||
| 101 | my $server_list = $osgish->servers->list; | ||
| 102 | return [] unless @$server_list; | ||
| 103 | my $str = $cmpl->{str} || ""; | ||
| 104 | my $len = length($str); | ||
| 105 | return [ grep { substr($_,0,$len) eq $str } map { $_->{name} } @$server_list ]; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | # ====================================================================================== | ||
| 110 | |||
| 111 | sub _bundle_or_service { | ||
| 112 | my $self = shift; | ||
| 113 | my $osgish = $self->{osgish}; | ||
| 114 | my ($ids_sub,$names_sub,@rest) = @_; | ||
| 115 | my $args = @rest ? { @rest } : {}; | ||
| 116 | return sub { | ||
| 117 | my ($term,$cmpl) = @_; | ||
| 118 | return [] unless $osgish->agent; | ||
| 119 | my $str = $cmpl->{str} || ""; | ||
| 120 | my $len = length($str); | ||
| 121 | if (!$args->{no_ids} && $str =~ /^\d+$/) { | ||
| 122 | # Complete on ids | ||
| 123 | my $ret = [ sort { $a <=> $b } grep { substr($_,0,$len) eq $str } @{&$ids_sub()} ]; | ||
| 124 | return $ret; | ||
| 125 | } else { | ||
| 126 | my @sym_names = sort keys %{&$names_sub()}; | ||
| 127 | if ($str) { | ||
| 128 | return [ grep { substr($_,0,$len) eq $str } @sym_names ]; | ||
| 129 | } else { | ||
| 130 | return \@sym_names; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | =head1 LICENSE | ||
| 138 | |||
| 139 | This file is part of osgish. | ||
| 140 | |||
| 141 | Osgish is free software: you can redistribute it and/or modify | ||
| 142 | it under the terms of the GNU General Public License as published by | ||
| 143 | the Free Software Foundation, either version 2 of the License, or | ||
| 144 | (at your option) any later version. | ||
| 145 | |||
| 146 | osgish is distributed in the hope that it will be useful, | ||
| 147 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 148 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 149 | GNU General Public License for more details. | ||
| 150 | |||
| 151 | You should have received a copy of the GNU General Public License | ||
| 152 | along with osgish. If not, see <http://www.gnu.org/licenses/>. | ||
| 153 | |||
| 154 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 155 | further details. | ||
| 156 | |||
| 157 | =head1 PROFESSIONAL SERVICES | ||
| 158 | |||
| 159 | Just in case you need professional support for this module (or JMX or OSGi in | ||
| 160 | general), you might want to have a look at www.consol.com Contact | ||
| 161 | roland.huss@consol.de for further information (or use the contact form at | ||
| 162 | http://www.consol.com/contact/) | ||
| 163 | |||
| 164 | =head1 AUTHOR | ||
| 165 | |||
| 166 | roland@cpan.org | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | 1; | ||
| 173 | |||
| 174 | |||
| 175 |