📄 readline.pm
字号:
=head1 NAMETerm::ReadLine - Perl interface to various C<readline> packages. Ifno real package is found, substitutes stubs instead of basic functions.=head1 SYNOPSIS use Term::ReadLine; $term = new Term::ReadLine 'Simple Perl calc'; $prompt = "Enter your arithmetic expression: "; $OUT = $term->OUT || STDOUT; while ( defined ($_ = $term->readline($prompt)) ) { $res = eval($_), "\n"; warn $@ if $@; print $OUT $res, "\n" unless $@; $term->addhistory($_) if /\S/; }=head1 DESCRIPTIONThis package is just a front end to some other packages. At the momentthis description is written, the only such package is Term-ReadLine,available on CPAN near you. The real target of this stub package is toset up a common interface to whatever Readline emerges with time.=head1 Minimal set of supported functionsAll the supported functions should be called as methods, i.e., either as $term = new Term::ReadLine 'name';or as $term->addhistory('row');where $term is a return value of Term::ReadLine-E<gt>Init.=over 12=item C<ReadLine>returns the actual package that executes the commands. Among possiblevalues are C<Term::ReadLine::Gnu>, C<Term::ReadLine::Perl>,C<Term::ReadLine::Stub Exporter>.=item C<new>returns the handle for subsequent calls to followingfunctions. Argument is the name of the application. Optionally can befollowed by two arguments for C<IN> and C<OUT> filehandles. Thesearguments should be globs.=item C<readline>gets an input line, I<possibly> with actual C<readline>support. Trailing newline is removed. Returns C<undef> on C<EOF>.=item C<addhistory>adds the line to the history of input, from where it can be used ifthe actual C<readline> is present.=item C<IN>, $C<OUT>return the filehandles for input and output or C<undef> if C<readline>input and output cannot be used for Perl.=item C<MinLine>If argument is specified, it is an advice on minimal size of line tobe included into history. C<undef> means do not include anything intohistory. Returns the old value.=item C<findConsole>returns an array with two strings that give most appropriate names forfiles for input and output using conventions C<"E<lt>$in">, C<"E<gt>out">.=item Attribsreturns a reference to a hash which describes internal configurationof the package. Names of keys in this hash conform to standardconventions with the leading C<rl_> stripped.=item C<Features>Returns a reference to a hash with keys being features present incurrent implementation. Several optional features are used in theminimal interface: C<appname> should be present if the first argumentto C<new> is recognized, and C<minline> should be present ifC<MinLine> method is not dummy. C<autohistory> should be present iflines are put into history automatically (maybe subject toC<MinLine>), and C<addhistory> if C<addhistory> method is not dummy.If C<Features> method reports a feature C<attribs> as present, themethod C<Attribs> is not dummy.=back=head1 Additional supported functionsActually C<Term::ReadLine> can use some other package, that willsupport reacher set of commands.All these commands are callable via method interface and have nameswhich conform to standard conventions with the leading C<rl_> stripped.The stub package included with the perl distribution allows someadditional methods: =over 12=item C<tkRunning>makes Tk event loop run when waiting for user input (i.e., duringC<readline> method).=item C<ornaments>makes the command line stand out by using termcap data. The argumentto C<ornaments> should be 0, 1, or a string of a formC<"aa,bb,cc,dd">. Four components of this string should be names ofI<terminal capacities>, first two will be issued to make the promptstandout, last two to make the input line standout.=item C<newTTY>takes two arguments which are input filehandle and output filehandle.Switches to use these filehandles.=backOne can check whether the currently loaded ReadLine package supportsthese methods by checking for corresponding C<Features>.=head1 EXPORTSNone=head1 ENVIRONMENTThe environment variable C<PERL_RL> governs which ReadLine clone isloaded. If the value is false, a dummy interface is used. If the valueis true, it should be tail of the name of the package to use, such asC<Perl> or C<Gnu>. As a special case, if the value of this variable is space-separated,the tail might be used to disable the ornaments by setting the tail tobe C<o=0> or C<ornaments=0>. The head should be as described above, sayIf the variable is not set, or if the head of space-separated list isempty, the best available package is loaded. export "PERL_RL=Perl o=0" # Use Perl ReadLine without ornaments export "PERL_RL= o=0" # Use best available ReadLine without ornaments(Note that processing of C<PERL_RL> for ornaments is in the discretion of the particular used C<Term::ReadLine::*> package).=cutpackage Term::ReadLine::Stub;@ISA = qw'Term::ReadLine::Tk Term::ReadLine::TermCap';$DB::emacs = $DB::emacs; # To peacify -w*rl_term_set = \@Term::ReadLine::TermCap::rl_term_set;sub ReadLine {'Term::ReadLine::Stub'}sub readline { my $self = shift; my ($in,$out,$str) = @$self; my $prompt = shift; print $out $rl_term_set[0], $prompt, $rl_term_set[1], $rl_term_set[2]; $self->register_Tk if not $Term::ReadLine::registered and $Term::ReadLine::toloop and defined &Tk::DoOneEvent; #$str = scalar <$in>; $str = $self->get_line; $str =~ s/^\s*\Q$prompt\E// if ($^O eq 'MacOS'); print $out $rl_term_set[3]; # bug in 5.000: chomping empty string creats length -1: chomp $str if defined $str; $str;}sub addhistory {}sub findConsole { my $console; if ($^O eq 'MacOS') { $console = "Dev:Console"; } elsif (-e "/dev/tty") { $console = "/dev/tty"; } elsif (-e "con" or $^O eq 'MSWin32') { $console = "con"; } else { $console = "sys\$command"; } if (($^O eq 'amigaos') || ($^O eq 'beos') || ($^O eq 'epoc')) { $console = undef; } elsif ($^O eq 'os2') { if ($DB::emacs) { $console = undef; } else { $console = "/dev/con"; } } $consoleOUT = $console; $console = "&STDIN" unless defined $console; if (!defined $consoleOUT) { $consoleOUT = defined fileno(STDERR) ? "&STDERR" : "&STDOUT"; } ($console,$consoleOUT);}sub new { die "method new called with wrong number of arguments" unless @_==2 or @_==4; #local (*FIN, *FOUT); my ($FIN, $FOUT, $ret); if (@_==2) { ($console, $consoleOUT) = findConsole; open(FIN, "<$console"); open(FOUT,">$consoleOUT"); #OUT->autoflush(1); # Conflicts with debugger? $sel = select(FOUT); $| = 1; # for DB::OUT select($sel); $ret = bless [\*FIN, \*FOUT]; } else { # Filehandles supplied $FIN = $_[2]; $FOUT = $_[3]; #OUT->autoflush(1); # Conflicts with debugger? $sel = select($FOUT); $| = 1; # for DB::OUT select($sel); $ret = bless [$FIN, $FOUT]; } if ($ret->Features->{ornaments} and not ($ENV{PERL_RL} and $ENV{PERL_RL} =~ /\bo\w*=0/)) { local $Term::ReadLine::termcap_nowarn = 1; $ret->ornaments(1); } return $ret;}sub newTTY { my ($self, $in, $out) = @_; $self->[0] = $in; $self->[1] = $out; my $sel = select($out); $| = 1; # for DB::OUT select($sel);}sub IN { shift->[0] }sub OUT { shift->[1] }sub MinLine { undef }sub Attribs { {} }my %features = (tkRunning => 1, ornaments => 1, 'newTTY' => 1);sub Features { \%features }package Term::ReadLine; # So late to allow the above code be defined?my ($which) = exists $ENV{PERL_RL} ? split /\s+/, $ENV{PERL_RL} : undef;if ($which) { if ($which =~ /\bgnu\b/i){ eval "use Term::ReadLine::Gnu;"; } elsif ($which =~ /\bperl\b/i) { eval "use Term::ReadLine::Perl;"; } else { eval "use Term::ReadLine::$which;"; }} elsif (defined $which and $which ne '') { # Defined but false # Do nothing fancy} else { eval "use Term::ReadLine::Gnu; 1" or eval "use Term::ReadLine::Perl; 1";}#require FileHandle;# To make possible switch off RL in debugger: (Not needed, work done# in debugger).if (defined &Term::ReadLine::Gnu::readline) { @ISA = qw(Term::ReadLine::Gnu Term::ReadLine::Stub);} elsif (defined &Term::ReadLine::Perl::readline) { @ISA = qw(Term::ReadLine::Perl Term::ReadLine::Stub);} else { @ISA = qw(Term::ReadLine::Stub);}package Term::ReadLine::TermCap;# Prompt-start, prompt-end, command-line-start, command-line-end# -- zero-width beautifies to emit around prompt and the command line.@rl_term_set = ("","","","");# string encoded:$rl_term_set = ',,,';sub LoadTermCap { return if defined $terminal; require Term::Cap; $terminal = Tgetent Term::Cap ({OSPEED => 9600}); # Avoid warning.}sub ornaments { shift; return $rl_term_set unless @_; $rl_term_set = shift; $rl_term_set ||= ',,,'; $rl_term_set = 'us,ue,md,me' if $rl_term_set eq '1'; my @ts = split /,/, $rl_term_set, 4; eval { LoadTermCap }; unless (defined $terminal) { warn("Cannot find termcap: $@\n") unless $Term::ReadLine::termcap_nowarn; $rl_term_set = ',,,'; return; } @rl_term_set = map {$_ ? $terminal->Tputs($_,1) || '' : ''} @ts; return $rl_term_set;}package Term::ReadLine::Tk;$count_handle = $count_DoOne = $count_loop = 0;sub handle {$giveup = 1; $count_handle++}sub Tk_loop { # Tk->tkwait('variable',\$giveup); # needs Widget $count_DoOne++, Tk::DoOneEvent(0) until $giveup; $count_loop++; $giveup = 0;}sub register_Tk { my $self = shift; $Term::ReadLine::registered++ or Tk->fileevent($self->IN,'readable',\&handle);}sub tkRunning { $Term::ReadLine::toloop = $_[1] if @_ > 1; $Term::ReadLine::toloop;}sub get_c { my $self = shift; $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent; return getc $self->IN;}sub get_line { my $self = shift; $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent; my $in = $self->IN; local ($/) = "\n"; return scalar <$in>;}1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -