1 package CPANPLUS::Shell;
6 use CPANPLUS::Configure;
7 use CPANPLUS::Internals::Constants;
9 use Module::Load qw[load];
10 use Params::Check qw[check];
11 use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
13 $Params::Check::VERBOSE = 1;
15 use vars qw[@ISA $SHELL $DEFAULT];
17 $DEFAULT = SHELL_DEFAULT;
27 use CPANPLUS::Shell; # load the shell indicated by your
28 # config -- defaults to
29 # CPANPLUS::Shell::Default
31 use CPANPLUS::Shell qw[Classic] # load CPANPLUS::Shell::Classic;
33 my $ui = CPANPLUS::Shell->new();
34 my $name = $ui->which; # Find out what shell you loaded
36 $ui->shell; # run the ui shell
41 This module is the generic loading (and base class) for all C<CPANPLUS>
42 shells. Through this module you can load any installed C<CPANPLUS>
45 Just about all the functionality is provided by the shell that you have
46 loaded, and not by this class (which merely functions as a generic
47 loading class), so please consult the documentation of your shell of
56 ### find out what shell we're supposed to load ###
58 ? $class . '::' . $option
59 : do { ### XXX this should offer to reconfigure
60 ### CPANPLUS, somehow. --rs
61 ### XXX load Configure only if we really have to
62 ### as that means any $Conf passed later on will
63 ### be ignored in favour of the one that was
64 ### retrieved via ->new --kane
65 my $conf = CPANPLUS::Configure->new() or
66 die loc("No configuration available -- aborting") . $/;
67 $conf->get_conf('shell') || $DEFAULT;
70 ### load the shell, fall back to the default if required
71 ### and die if even that doesn't work
78 die loc("Your default shell '%1' is not available: %2",
80 loc("Check your installation!") . "\n"
81 if $SHELL eq $DEFAULT;
83 warn loc("Failed to use '%1': %2", $SHELL, $err),
84 loc("Switching back to the default shell '%1'", $DEFAULT),
94 sub which { return $SHELL }
98 ###########################################################################
99 ### abstracted out subroutines available to programmers of other shells ###
100 ###########################################################################
102 package CPANPLUS::Shell::_Base::ReadLine;
105 use vars qw($AUTOLOAD $TMPL);
109 use Params::Check qw[check];
110 use Module::Load::Conditional qw[can_load];
111 use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
113 $Params::Check::VERBOSE = 1;
117 brand => { default => '', strict_type => 1 },
118 prompt => { default => '> ', strict_type => 1 },
119 pager => { default => '' },
120 backend => { default => '' },
121 term => { default => '' },
122 format => { default => '' },
123 dist_format => { default => '' },
124 remote => { default => undef },
125 noninteractive => { default => '' },
126 cache => { default => [ ] },
127 settings => { default => { install_all_prereqs => undef },
129 _old_sigpipe => { default => '', no_override => 1 },
130 _old_outfh => { default => '', no_override => 1 },
131 _signals => { default => { INT => { } }, no_override => 1 },
134 ### autogenerate accessors ###
135 for my $key ( keys %$TMPL ) {
137 *{__PACKAGE__."::$key"} = sub {
139 $self->{$key} = $_[0] if @_;
140 return $self->{$key};
148 my $self = check( $TMPL, \%hash ) or return;
152 ### signal handler ###
153 $SIG{INT} = $self->_signals->{INT}->{handler} =
155 unless ( $self->_signals->{INT}->{count}++ ) {
156 warn loc("Caught SIGINT"), "\n";
158 warn loc("Got another SIGINT"), "\n"; die;
161 ### end sig handler ###
166 ### display shell's banner, takes the Backend object as argument
169 my $cpan = $self->backend;
170 my $term = $self->term;
172 ### Tries to probe for our ReadLine support status
173 # a) under an interactive shell?
174 my $rl_avail = (!$term->isa('CPANPLUS::Shell::_Faked'))
175 # b) do we have a tty terminal?
177 # c) should we enable the term?
178 ? (!$self->__is_bad_terminal($term))
179 # d) external modules available?
180 ? ($term->ReadLine ne "Term::ReadLine::Stub")
181 # a+b+c+d => "Smart" terminal
183 # a+b+c => "Stub" terminal
184 : loc("available (try 'i Term::ReadLine::Perl')")
185 # a+b => "Bad" terminal
187 # a => "Dumb" terminal
189 # none => "Faked" terminal
190 : loc("suppressed in batch mode");
192 $rl_avail = loc("ReadLine support %1.", $rl_avail);
193 $rl_avail = "\n*** $rl_avail" if (length($rl_avail) > 45);
196 loc("%1 -- CPAN exploration and module installation (v%2)",
197 $self->which, $self->which->VERSION()), "\n",
198 loc("*** Please report bugs to <bug-cpanplus\@rt.cpan.org>."), "\n",
199 loc("*** Using CPANPLUS::Backend v%1. %2",
200 $cpan->VERSION, $rl_avail), "\n\n"
204 ### checks whether the Term::ReadLine is broken and needs to fallback to Stub
205 sub __is_bad_terminal {
207 my $term = $self->term;
209 return unless $^O eq 'MSWin32';
211 ### replace the term with the default (stub) one
212 return $self->term(Term::ReadLine::Stub->new( $self->brand ) );
215 ### open a pager handle
218 my $cpan = $self->backend;
219 my $cmd = $cpan->configure_object->get_program('pager') or return;
221 $self->_old_sigpipe( $SIG{PIPE} );
222 $SIG{PIPE} = 'IGNORE';
224 my $fh = new FileHandle;
225 unless ( $fh->open("| $cmd") ) {
226 error(loc("could not pipe to %1: %2\n", $cmd, $!) );
233 $self->_old_outfh( select $fh );
238 ### print to the current pager handle, or STDOUT if it's not opened
241 my $pager = $self->pager or return;
243 $pager->close if (ref($pager) and $pager->can('close'));
245 $self->pager( undef );
247 select $self->_old_outfh;
248 $SIG{PIPE} = $self->_old_sigpipe;
258 ### determines row count of current terminal; defaults to 25.
259 ### used by the pager functions
262 my $cpan = $self->backend;
267 default => { default => 25, allow => qr/^\d$/,
271 check( $tmpl, \%hash ) or return;
273 if ( $^O eq 'MSWin32' ) {
274 if ( can_load( modules => { 'Win32::Console' => '0.0' } ) ) {
275 $win32_console ||= Win32::Console->new();
276 my $rows = ($win32_console->Info)[-1];
281 local $Module::Load::Conditional::VERBOSE = 0;
282 if ( can_load(modules => {'Term::Size' => '0.0'}) ) {
283 my ($cols, $rows) = Term::Size::chars();
291 ### Custom print routines, mainly to be able to catch output
292 ### in test cases, or redirect it if need be
302 ### MUST specify $fmt as a seperate param, and not as part
303 ### of @_, as it will then miss the $fmt and return the
304 ### number of elements in the list... =/ --kane
305 $self->__print( sprintf( $fmt, @_ ) );
315 Please report bugs or other issues to E<lt>bug-cpanplus@rt.cpan.org<gt>.
319 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
323 The CPAN++ interface (of which this module is a part of) is copyright (c)
324 2001 - 2007, Jos Boumans E<lt>kane@cpan.orgE<gt>. All rights reserved.
326 This library is free software; you may redistribute and/or modify it
327 under the same terms as Perl itself.
331 L<CPANPLUS::Shell::Default>, L<CPANPLUS::Shell::Classic>, L<cpanp>
336 # c-indentation-style: bsd
338 # indent-tabs-mode: nil
340 # vim: expandtab shiftwidth=4: