AnyEvent-ReadLine-Gnu
view release on metacpan or search on metacpan
Revision history for Perl extension AnyEvent::ReadLine::Gnu
1.1 Tue Dec 12 16:42:04 CET 2017
- it was not possible to specify a completely empty prompt
(reported by Mons Anderson).
- added stability canary support.
1.0 Fri May 11 17:37:36 CEST 2012
- make sure any readline adornment is off even on first redisplay.
- added caveat section.
- fix "support" url.
0.2 Thu May 10 23:52:21 CEST 2012
- the SYNOPSIS used cb instead of the correct
AnyEvent::ReadLine::Gnu - event-based interface to Term::ReadLine::Gnu
=head1 SYNOPSIS
use AnyEvent::ReadLine::Gnu;
# works always, prints message to stdout
AnyEvent::ReadLine::Gnu->print ("message\n");
# now initialise readline
my $rl = new AnyEvent::ReadLine::Gnu prompt => "hi> ", on_line => sub {
# called for each line entered by the user
AnyEvent::ReadLine::Gnu->print ("you entered: $_[0]\n");
};
# asynchronously print something
my $t = AE::timer 1, 1, sub {
$rl->hide;
print "async message 1\n"; # mind the \n
$rl->show;
=item on_line => $cb->($string)
The only mandatory parameter - passes the callback that will receive lines
that are completed by the user.
The string will be in locale-encoding (a multibyte character string). For
example, in an utf-8 using locale it will be utf-8. There is no portable
way known to the author to convert this into e.g. a unicode string.
=item prompt => $string
The prompt string to use, defaults to C<< > >>.
=item name => $string
The readline application name, defaults to C<$0>.
=item in => $glob
The input filehandle (should be a glob): defaults to C<*STDIN>.
=item out => $glob
The output filehandle (should be a glob): defaults to C<*STDOUT>.
=back
=cut
our $self;
our $prompt;
our $cb;
our $hidden;
our $rw;
our ($in, $out);
our $saved_point;
our $saved_line;
# we postpone calling the user clalback here because readline
# still has the input buffer at this point, so calling hide and
AE::postpone sub {
$cb->($line, $point);
};
}
sub new {
my ($class, %arg) = @_;
$in = $arg{in} || *STDIN;
$out = $arg{out} || *STDOUT;
$prompt = $arg{prompt} // "> ";
$cb = $arg{on_line} || $arg{cb}
or do { require Carp; Carp::croak ("AnyEvent::ReadLine::Gnu->new on_line callback argument mandatry, but missing") };
$self = $class->SUPER::new ($arg{name} || $0, $in, $out);
$Term::ReadLine::Gnu::Attribs{term_set} = ["", "", "", ""];
$self->CallbackHandlerInstall ($prompt, \&on_line);
$hidden = 1;
$self->show;
$self
}
=item $rl->hide
=item AnyEvent::ReadLine::Gnu->hide
These methods I<hide> the readline prompt and text. Basically, it removes
the readline feedback from your terminal.
It is safe to call even when AnyEvent::ReadLine::Gnu has not yet been
initialised.
This is immensely useful in an event-based program when you want to output
some stuff to the terminal without disturbing the prompt - just C<hide>
readline, output your thing, then C<show> it again.
Since user input will not be processed while readline is hidden, you
should call C<show> as soon as possible.
=cut
sub hide {
return if !$self || $hidden++;
undef $rw;
$saved_point = $self->{point};
$saved_line = $self->{line_buffer};
$self->rl_set_prompt ("");
$self->{line_buffer} = "";
$self->rl_redisplay;
}
=item $rl->show
=item AnyEvent::ReadLine::Gnu->show
Undos any hiding. Every call to C<hide> has to be followed to a call to
C<show>. The last call will redisplay the readline prompt, current input
line and cursor position. Keys entered while the prompt was hidden will be
processed again.
=cut
sub show {
return if !$self || --$hidden;
if (defined $saved_point) {
$self->rl_set_prompt ($prompt);
$self->{line_buffer} = $saved_line;
$self->{point} = $saved_point;
$self->redisplay;
}
$rw = AE::io $in, 0, sub {
$self->rl_callback_read_char;
};
}
NAME
AnyEvent::ReadLine::Gnu - event-based interface to Term::ReadLine::Gnu
SYNOPSIS
use AnyEvent::ReadLine::Gnu;
# works always, prints message to stdout
AnyEvent::ReadLine::Gnu->print ("message\n");
# now initialise readline
my $rl = new AnyEvent::ReadLine::Gnu prompt => "hi> ", on_line => sub {
# called for each line entered by the user
AnyEvent::ReadLine::Gnu->print ("you entered: $_[0]\n");
};
# asynchronously print something
my $t = AE::timer 1, 1, sub {
$rl->hide;
print "async message 1\n"; # mind the \n
$rl->show;
on_line => $cb->($string)
The only mandatory parameter - passes the callback that will
receive lines that are completed by the user.
The string will be in locale-encoding (a multibyte character
string). For example, in an utf-8 using locale it will be utf-8.
There is no portable way known to the author to convert this
into e.g. a unicode string.
prompt => $string
The prompt string to use, defaults to ">".
name => $string
The readline application name, defaults to $0.
in => $glob
The input filehandle (should be a glob): defaults to *STDIN.
out => $glob
The output filehandle (should be a glob): defaults to *STDOUT.
$rl->hide
AnyEvent::ReadLine::Gnu->hide
These methods *hide* the readline prompt and text. Basically, it
removes the readline feedback from your terminal.
It is safe to call even when AnyEvent::ReadLine::Gnu has not yet
been initialised.
This is immensely useful in an event-based program when you want to
output some stuff to the terminal without disturbing the prompt -
just "hide" readline, output your thing, then "show" it again.
Since user input will not be processed while readline is hidden, you
should call "show" as soon as possible.
$rl->show
AnyEvent::ReadLine::Gnu->show
Undos any hiding. Every call to "hide" has to be followed to a call
to "show". The last call will redisplay the readline prompt, current
input line and cursor position. Keys entered while the prompt was
hidden will be processed again.
$rl->print ($string, ...)
AnyEvent::ReadLine::Gnu->print ($string, ...)
Prints the given strings to the terminal, by first hiding the
readline, printing the message, and showing it again.
This function can be called even when readline has never been
initialised.
( run in 1.459 second using v1.01-cache-2.11-cpan-6aa56a78535 )