blib/lib/CPANPLUS/Shell.pm untested

filter: Statementstmt 0.0% 0 / 143 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 230 SCAR 56.1
CC
16
Cov
0%
_show_banner
42.0
import
20.0
_term_rowcount
20.0
CRAP 272.0
1 package CPANPLUS::Shell;
2  
3 use strict;
4  
5 use CPANPLUS::Error;
6 use CPANPLUS::Configure;
7 use CPANPLUS::Internals::Constants;
8  
9 use Module::Load qw[load];
10 use Params::Check qw[check];
11 use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
12  
13 $Params::Check::VERBOSE = 1;
14  
15 use vars qw[@ISA $SHELL $DEFAULT $VERSION];
16  
17 $VERSION = "0.9912";
18 $DEFAULT = SHELL_DEFAULT;
19  
20 =pod
21  
22 =head1 NAME
23  
24 CPANPLUS::Shell - base class for CPANPLUS shells
25  
26 =head1 SYNOPSIS
27  
28 use CPANPLUS::Shell; # load the shell indicated by your
29 # config -- defaults to
30 # CPANPLUS::Shell::Default
31  
32 use CPANPLUS::Shell qw[Classic] # load CPANPLUS::Shell::Classic;
33  
34 my $ui = CPANPLUS::Shell->new();
35 my $name = $ui->which; # Find out what shell you loaded
36  
37 $ui->shell; # run the ui shell
38  
39  
40 =head1 DESCRIPTION
41  
42 This module is the generic loading (and base class) for all C<CPANPLUS>
43 shells. Through this module you can load any installed C<CPANPLUS>
44 shell.
45  
46 Just about all the functionality is provided by the shell that you have
47 loaded, and not by this class (which merely functions as a generic
48 loading class), so please consult the documentation of your shell of
49 choice.
50  
51 =cut
52  
53 sub import {
54 my $class = shift;
55 my $option = shift;
56  
57 ### find out what shell we're supposed to load ###
58 $SHELL = $option
59 ? $class . '::' . $option
60 : do { ### XXX this should offer to reconfigure
61 ### CPANPLUS, somehow. --rs
62 ### XXX load Configure only if we really have to
63 ### as that means any $Conf passed later on will
64 ### be ignored in favour of the one that was
65 ### retrieved via ->new --kane
66 my $conf = CPANPLUS::Configure->new() or
67 die loc("No configuration available -- aborting") . $/;
68 $conf->get_conf('shell') || $DEFAULT;
69 };
70  
71 ### load the shell, fall back to the default if required
72 ### and die if even that doesn't work
73 EVAL: {
74 eval { load $SHELL };
75  
76 if( $@ ) {
77 my $err = $@;
78  
79 die loc("Your default shell '%1' is not available: %2",
80 $DEFAULT, $err) .
81 loc("Check your installation!") . "\n"
82 if $SHELL eq $DEFAULT;
83  
84 warn loc("Failed to use '%1': %2", $SHELL, $err),
85 loc("Switching back to the default shell '%1'", $DEFAULT),
86 "\n";
87  
88 $SHELL = $DEFAULT;
89 redo EVAL;
90 }
91 }
92 @ISA = ($SHELL);
93 }
94  
95 sub which { return $SHELL }
96  
97 1;
98  
99 ###########################################################################
100 ### abstracted out subroutines available to programmers of other shells ###
101 ###########################################################################
102  
103 package CPANPLUS::Shell::_Base::ReadLine;
104  
105 use strict;
106 use vars qw($AUTOLOAD $TMPL);
107  
108 use FileHandle;
109 use CPANPLUS::Error;
110 use Params::Check qw[check];
111 use Module::Load::Conditional qw[can_load];
112 use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
113  
114 $Params::Check::VERBOSE = 1;
115  
116  
117 $TMPL = {
118 brand => { default => '', strict_type => 1 },
119 prompt => { default => '> ', strict_type => 1 },
120 pager => { default => '' },
121 backend => { default => '' },
122 term => { default => '' },
123 format => { default => '' },
124 dist_format => { default => '' },
125 remote => { default => undef },
126 noninteractive => { default => '' },
127 cache => { default => [ ] },
128 settings => { default => { install_all_prereqs => undef },
129 no_override => 1 },
130 _old_sigpipe => { default => '', no_override => 1 },
131 _old_outfh => { default => '', no_override => 1 },
132 _signals => { default => { INT => { } }, no_override => 1 },
133 };
134  
135 ### autogenerate accessors ###
136 for my $key ( keys %$TMPL ) {
137 no strict 'refs';
138 *{__PACKAGE__."::$key"} = sub {
139 my $self = shift;
140 $self->{$key} = $_[0] if @_;
141 return $self->{$key};
142 }
143 }
144  
145 sub _init {
146 my $class = shift;
147 my %hash = @_;
148  
149 my $self = check( $TMPL, \%hash ) or return;
150  
151 bless $self, $class;
152  
153 ### signal handler ###
154 $SIG{INT} = $self->_signals->{INT}->{handler} =
155 sub {
156 unless ( $self->_signals->{INT}->{count}++ ) {
157 warn loc("Caught SIGINT"), "\n";
158 } else {
159 warn loc("Got another SIGINT"), "\n"; die;
160 }
161 };
162 ### end sig handler ###
163  
164 return $self;
165 }
166  
167 ### display shell's banner, takes the Backend object as argument
168 sub _show_banner {
169 my $self = shift;
170 my $cpan = $self->backend;
171 my $term = $self->term;
172  
173 ### Tries to probe for our ReadLine support status
174 # a) under an interactive shell?
175 my $rl_avail = (!$term->isa('CPANPLUS::Shell::_Faked'))
176 # b) do we have a tty terminal?
177 ? (-t STDIN)
178 # c) should we enable the term?
179 ? (!$self->__is_bad_terminal($term))
180 # d) external modules available?
181 ? ($term->ReadLine ne "Term::ReadLine::Stub")
182 # a+b+c+d => "Smart" terminal
183 ? loc("enabled")
184 # a+b+c => "Stub" terminal
185 : loc("available (try 'i Term::ReadLine::Perl')")
186 # a+b => "Bad" terminal
187 : loc("disabled")
188 # a => "Dumb" terminal
189 : loc("suppressed")
190 # none => "Faked" terminal
191 : loc("suppressed in batch mode");
192  
193 $rl_avail = loc("ReadLine support %1.", $rl_avail);
194 $rl_avail = "\n*** $rl_avail" if (length($rl_avail) > 45);
195  
196 $self->__print(
197 loc("%1 -- CPAN exploration and module installation (v%2)",
198 $self->which, $self->which->VERSION()), "\n",
199 loc("*** Please report bugs to <bug-cpanplus\@rt.cpan.org>."), "\n",
200 loc("*** Using CPANPLUS::Backend v%1. %2",
201 $cpan->VERSION, $rl_avail), "\n\n"
202 );
203 }
204  
205 ### checks whether the Term::ReadLine is broken and needs to fallback to Stub
206 sub __is_bad_terminal {
207 my $self = shift;
208 my $term = $self->term;
209  
210 return unless $^O eq 'MSWin32';
211  
212 ### replace the term with the default (stub) one
213 return $self->term(Term::ReadLine::Stub->new( $self->brand ) );
214 }
215  
216 ### open a pager handle
217 sub _pager_open {
218 my $self = shift;
219 my $cpan = $self->backend;
220 my $cmd = $cpan->configure_object->get_program('pager') or return;
221  
222 $self->_old_sigpipe( $SIG{PIPE} );
223 $SIG{PIPE} = 'IGNORE';
224  
225 my $fh = new FileHandle;
226 unless ( $fh->open("| $cmd") ) {
227 error(loc("could not pipe to %1: %2\n", $cmd, $!) );
228 return;
229 }
230  
231 $fh->autoflush(1);
232  
233 $self->pager( $fh );
234 $self->_old_outfh( select $fh );
235  
236 return $fh;
237 }
238  
239 ### print to the current pager handle, or STDOUT if it's not opened
240 sub _pager_close {
241 my $self = shift;
242 my $pager = $self->pager or return;
243  
244 $pager->close if (ref($pager) and $pager->can('close'));
245  
246 $self->pager( undef );
247  
248 select $self->_old_outfh;
249 $SIG{PIPE} = $self->_old_sigpipe;
250  
251 return 1;
252 }
253  
254  
255  
256 {
257 my $win32_console;
258  
259 ### determines row count of current terminal; defaults to 25.
260 ### used by the pager functions
261 sub _term_rowcount {
262 my $self = shift;
263 my $cpan = $self->backend;
264 my %hash = @_;
265  
266 my $default;
267 my $tmpl = {
268 default => { default => 25, allow => qr/^\d$/,
269 store => \$default }
270 };
271  
272 check( $tmpl, \%hash ) or return;
273  
274 if ( $^O eq 'MSWin32' ) {
275 if ( can_load( modules => { 'Win32::Console' => '0.0' } ) ) {
276 $win32_console ||= Win32::Console->new();
277 my $rows = ($win32_console->Info)[-1];
278 return $rows;
279 }
280  
281 } else {
282 local $Module::Load::Conditional::VERBOSE = 0;
283 if ( can_load(modules => {'Term::Size' => '0.0'}) ) {
284 my ($cols, $rows) = Term::Size::chars();
285 return $rows;
286 }
287 }
288 return $default;
289 }
290 }
291  
292 ### Custom print routines, mainly to be able to catch output
293 ### in test cases, or redirect it if need be
294 { sub __print {
295 my $self = shift;
296 print @_;
297 }
298  
299 sub __printf {
300 my $self = shift;
301 my $fmt = shift;
302  
303 ### MUST specify $fmt as a separate param, and not as part
304 ### of @_, as it will then miss the $fmt and return the
305 ### number of elements in the list... =/ --kane
306 $self->__print( sprintf( $fmt, @_ ) );
307 }
308 }
309  
310 1;
311  
312 =pod
313  
314 =head1 BUG REPORTS
315  
316 Please report bugs or other issues to E<lt>bug-cpanplus@rt.cpan.org<gt>.
317  
318 =head1 AUTHOR
319  
320 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
321  
322 =head1 COPYRIGHT
323  
324 The CPAN++ interface (of which this module is a part of) is copyright (c)
325 2001 - 2007, Jos Boumans E<lt>kane@cpan.orgE<gt>. All rights reserved.
326  
327 This library is free software; you may redistribute and/or modify it
328 under the same terms as Perl itself.
329  
330 =head1 SEE ALSO
331  
332 L<CPANPLUS::Shell::Default>, L<CPANPLUS::Shell::Classic>, L<cpanp>
333  
334 =cut
335  
336 # Local variables:
337 # c-indentation-style: bsd
338 # c-basic-offset: 4
339 # indent-tabs-mode: nil
340 # End:
341 # vim: expandtab shiftwidth=4:
342