AnyEvent-ReadLine-Gnu
view release on metacpan or search on metacpan
}
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;
};
}
=item $rl->print ($string, ...)
=item 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.
The last string should end with a newline.
=cut
sub print {
shift;
hide;
my $out = $out || *STDOUT;
print $out @_;
show;
}
END {
return unless $self;
$self->hide;
$self->callback_handler_remove;
}
1;
=back
=head1 CAVEATS
There are some issues with readline that can be problematic in event-based
programs:
=over 4
=item blocking I/O
( run in 2.594 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )