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 ### XXX this should offer to reconfigure CPANPLUS, somehow. --rs
57 my $conf = CPANPLUS::Configure->new()
58 or die loc("No configuration available -- aborting") . $/;
60 ### find out what shell we're supposed to load ###
62 ? $class . '::' . $option
63 : $conf->get_conf('shell') || $DEFAULT;
65 ### load the shell, fall back to the default if required
66 ### and die if even that doesn't work
73 die loc("Your default shell '%1' is not available: %2",
75 loc("Check your installation!") . "\n"
76 if $SHELL eq $DEFAULT;
78 warn loc("Failed to use '%1': %2", $SHELL, $err),
79 loc("Switching back to the default shell '%1'", $DEFAULT),
89 sub which { return $SHELL }
93 ###########################################################################
94 ### abstracted out subroutines available to programmers of other shells ###
95 ###########################################################################
97 package CPANPLUS::Shell::_Base::ReadLine;
100 use vars qw($AUTOLOAD $TMPL);
104 use Params::Check qw[check];
105 use Module::Load::Conditional qw[can_load];
106 use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
108 $Params::Check::VERBOSE = 1;
112 brand => { default => '', strict_type => 1 },
113 prompt => { default => '> ', strict_type => 1 },
114 pager => { default => '' },
115 backend => { default => '' },
116 term => { default => '' },
117 format => { default => '' },
118 dist_format => { default => '' },
119 remote => { default => undef },
120 noninteractive => { default => '' },
121 cache => { default => [ ] },
122 _old_sigpipe => { default => '', no_override => 1 },
123 _old_outfh => { default => '', no_override => 1 },
124 _signals => { default => { INT => { } }, no_override => 1 },
127 ### autogenerate accessors ###
128 for my $key ( keys %$TMPL ) {
130 *{__PACKAGE__."::$key"} = sub {
132 $self->{$key} = $_[0] if @_;
133 return $self->{$key};
141 my $self = check( $TMPL, \%hash ) or return;
145 ### signal handler ###
146 $SIG{INT} = $self->_signals->{INT}->{handler} =
148 unless ( $self->_signals->{INT}->{count}++ ) {
149 warn loc("Caught SIGINT"), "\n";
151 warn loc("Got another SIGINT"), "\n"; die;
154 ### end sig handler ###
159 ### display shell's banner, takes the Backend object as argument
162 my $cpan = $self->backend;
163 my $term = $self->term;
165 ### Tries to probe for our ReadLine support status
166 # a) under an interactive shell?
167 my $rl_avail = (!$term->isa('CPANPLUS::Shell::_Faked'))
168 # b) do we have a tty terminal?
170 # c) should we enable the term?
171 ? (!$self->__is_bad_terminal($term))
172 # d) external modules available?
173 ? ($term->ReadLine ne "Term::ReadLine::Stub")
174 # a+b+c+d => "Smart" terminal
176 # a+b+c => "Stub" terminal
177 : loc("available (try 'i Term::ReadLine::Perl')")
178 # a+b => "Bad" terminal
180 # a => "Dumb" terminal
182 # none => "Faked" terminal
183 : loc("suppressed in batch mode");
185 $rl_avail = loc("ReadLine support %1.", $rl_avail);
186 $rl_avail = "\n*** $rl_avail" if (length($rl_avail) > 45);
188 print loc("%1 -- CPAN exploration and module installation (v%2)",
189 $self->which, $self->which->VERSION()), "\n",
190 loc("*** Please report bugs to <bug-cpanplus\@rt.cpan.org>."), "\n",
191 loc("*** Using CPANPLUS::Backend v%1. %2",
192 $cpan->VERSION, $rl_avail), "\n\n";
195 ### checks whether the Term::ReadLine is broken and needs to fallback to Stub
196 sub __is_bad_terminal {
198 my $term = $self->term;
200 return unless $^O eq 'MSWin32';
202 ### replace the term with the default (stub) one
203 return $self->term(Term::ReadLine::Stub->new( $self->brand ) );
206 ### open a pager handle
209 my $cpan = $self->backend;
210 my $cmd = $cpan->configure_object->get_program('pager') or return;
212 $self->_old_sigpipe( $SIG{PIPE} );
213 $SIG{PIPE} = 'IGNORE';
215 my $fh = new FileHandle;
216 unless ( $fh->open("| $cmd") ) {
217 error(loc("could not pipe to %1: %2\n", $cmd, $!) );
224 $self->_old_outfh( select $fh );
229 ### print to the current pager handle, or STDOUT if it's not opened
232 my $pager = $self->pager or return;
234 $pager->close if (ref($pager) and $pager->can('close'));
236 $self->pager( undef );
238 select $self->_old_outfh;
239 $SIG{PIPE} = $self->_old_sigpipe;
249 ### determines row count of current terminal; defaults to 25.
250 ### used by the pager functions
253 my $cpan = $self->backend;
258 default => { default => 25, allow => qr/^\d$/,
262 check( $tmpl, \%hash ) or return;
264 if ( $^O eq 'MSWin32' ) {
265 if ( can_load( modules => { 'Win32::Console' => '0.0' } ) ) {
266 $win32_console ||= Win32::Console->new();
267 my $rows = ($win32_console->Info)[-1];
272 local $Module::Load::Conditional::VERBOSE = 0;
273 if ( can_load(modules => {'Term::Size' => '0.0'}) ) {
274 my ($cols, $rows) = Term::Size::chars();
288 Please report bugs or other issues to E<lt>bug-cpanplus@rt.cpan.org<gt>.
292 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
296 The CPAN++ interface (of which this module is a part of) is copyright (c)
297 2001 - 2007, Jos Boumans E<lt>kane@cpan.orgE<gt>. All rights reserved.
299 This library is free software; you may redistribute and/or modify it
300 under the same terms as Perl itself.
304 L<CPANPLUS::Shell::Default>, L<CPANPLUS::Shell::Classic>, L<cpanp>
309 # c-indentation-style: bsd
311 # indent-tabs-mode: nil
313 # vim: expandtab shiftwidth=4: