ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-ReadLine-Gnu/Gnu.pm
(Generate patch)

Comparing AnyEvent-ReadLine-Gnu/Gnu.pm (file contents):
Revision 1.1 by root, Thu May 10 02:21:49 2012 UTC vs.
Revision 1.5 by root, Fri May 11 00:20:19 2012 UTC

8 8
9 # works always, prints message to stdout 9 # works always, prints message to stdout
10 AnyEvent::ReadLine::Gnu->print ("message\n"); 10 AnyEvent::ReadLine::Gnu->print ("message\n");
11 11
12 # now initialise readline 12 # now initialise readline
13 my $rl = new AnyEvent::ReadLine::Gnu prompt => "hi> ", cb => sub { 13 my $rl = new AnyEvent::ReadLine::Gnu prompt => "hi> ", on_line => sub {
14 # called for each line entered by the user 14 # called for each line entered by the user
15 AnyEvent::ReadLine::Gnu->print ("you entered: $_[0]\n"); 15 AnyEvent::ReadLine::Gnu->print ("you entered: $_[0]\n");
16 }; 16 };
17 17
18 # asynchronously print something 18 # asynchronously print something
56 require Term::ReadLine::Gnu; 56 require Term::ReadLine::Gnu;
57} 57}
58 58
59use base Term::ReadLine::; 59use base Term::ReadLine::;
60 60
61our $VERSION = '0.1'; 61our $VERSION = '0.2';
62 62
63=item $rl = new AnyEvent::ReadLine::Gnu key => value... 63=item $rl = new AnyEvent::ReadLine::Gnu key => value...
64 64
65Creates a new AnyEvent::ReadLine object. 65Creates a new AnyEvent::ReadLine object.
66 66
81 81
82=item on_line => $cb->($string) 82=item on_line => $cb->($string)
83 83
84The only mandatory parameter - passes the callback that will receive lines 84The only mandatory parameter - passes the callback that will receive lines
85that are completed by the user. 85that are completed by the user.
86
87The string will be in locale-encoding (a multibyte character string). For
88example, in an utf-8 using locale it will be utf-8. There is no portable
89way known to the author to convert this into e.g. a unicode string.
86 90
87=item prompt => $string 91=item prompt => $string
88 92
89The prompt string to use, defaults to C<< > >>. 93The prompt string to use, defaults to C<< > >>.
90 94
112our ($in, $out); 116our ($in, $out);
113 117
114our $saved_point; 118our $saved_point;
115our $saved_line; 119our $saved_line;
116 120
121# we postpone calling the user clalback here because readline
122# still has the input buffer at this point, so calling hide and
123# show might not have the desired effect.
124sub on_line {
125 my $line = shift;
126 my $point = $self->{point};
127
128 AE::postpone sub {
129 $cb->($line, $point);
130 };
131}
132
117sub new { 133sub new {
118 my ($class, %arg) = @_; 134 my ($class, %arg) = @_;
119 135
120 $in = $arg{in} || *STDIN; 136 $in = $arg{in} || *STDIN;
121 $out = $arg{out} || *STDOUT; 137 $out = $arg{out} || *STDOUT;
122 $prompt = $arg{prompt} || "> "; 138 $prompt = $arg{prompt} || "> ";
123 $cb = $arg{on_line}; 139 $cb = $arg{on_line} || $arg{cb}
140 or do { require Carp; Carp::croak ("AnyEvent::ReadLine::Gnu->new on_line callback argument mandatry, but missing") };
124 141
125 $self = $class->SUPER::new ($arg{name} || $0, $in, $out); 142 $self = $class->SUPER::new ($arg{name} || $0, $in, $out);
126 143
127 $self->CallbackHandlerInstall ($prompt, $cb); 144 $self->CallbackHandlerInstall ($prompt, \&on_line);
128 # set the unadorned prompt 145 # set the unadorned prompt
129 $self->rl_set_prompt ($prompt); 146 $self->rl_set_prompt ($prompt);
147 $self->redisplay;
130 148
131 $hidden = 1; 149 $hidden = 1;
132 $self->show; 150 $self->show;
133 151
134 $self 152 $self
197=item AnyEvent::ReadLine::Gnu->print ($string, ...) 215=item AnyEvent::ReadLine::Gnu->print ($string, ...)
198 216
199Prints the given strings to the terminal, by first hiding the readline, 217Prints the given strings to the terminal, by first hiding the readline,
200printing the message, and showing it again. 218printing the message, and showing it again.
201 219
202This function cna be called even when readline has never been initialised. 220This function can be called even when readline has never been initialised.
203 221
204The last string should end with a newline. 222The last string should end with a newline.
205 223
206=cut 224=cut
207 225
223 241
2241; 2421;
225 243
226=back 244=back
227 245
246=head1 CAVEATS
247
248There are some issues with readline that can be problematic in event-based
249programs:
250
251=over 4
252
253=item blocking I/O
254
255Readline uses blocking terminal I/O. Under most circumstances, this does
256not cause big delays, but ttys have the potential to block programs
257indefinitely (e.g. on XOFF).
258
259=item unexpected disk I/O
260
261By default, readline does filename completion on TAB, and reads its
262config files.
263
264Tab completion can be disabled by calling C<< $rl->unbind_key (9) >>.
265
266=item tty settings
267
268After readline has been initialised, it will mangle the termios tty
269settings. This does not normally affect output very much, but should be
270taken into consideration.
271
272=item output intermixing
273
274Your program might wish to print messages (for example, log messages) to
275STDOUT or STDERR. This will usually cause confusion, unless readline is
276hidden with the hide method.
277
278=back
279
280Oh, and the above list is probably not complete.
281
228=head1 AUTHOR, CONTACT, SUPPORT 282=head1 AUTHOR, CONTACT, SUPPORT
229 283
230 Marc Lehmann <schmorp@schmorp.de> 284 Marc Lehmann <schmorp@schmorp.de>
231 http://software.schmorp.de/pkg/AnyEvent-Readline-Gnu.html 285 http://software.schmorp.de/pkg/AnyEvent-Readline-Gnu.html
232 286
233=cut 287=head1 SEE ALSO
234 288
289L<rltelnet> - a simple tcp_connect-with-readline program using this module.
290
291=cut
292

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines