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.4 by root, Thu May 10 23:28:38 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);
130 147
131 $hidden = 1; 148 $hidden = 1;
132 $self->show; 149 $self->show;
197=item AnyEvent::ReadLine::Gnu->print ($string, ...) 214=item AnyEvent::ReadLine::Gnu->print ($string, ...)
198 215
199Prints the given strings to the terminal, by first hiding the readline, 216Prints the given strings to the terminal, by first hiding the readline,
200printing the message, and showing it again. 217printing the message, and showing it again.
201 218
202This function cna be called even when readline has never been initialised. 219This function can be called even when readline has never been initialised.
203 220
204The last string should end with a newline. 221The last string should end with a newline.
205 222
206=cut 223=cut
207 224
223 240
2241; 2411;
225 242
226=back 243=back
227 244
245=head1 CAVEATS
246
247There are some issues with readline that can be problematic in event-based
248programs:
249
250=over 4
251
252=item blocking I/O
253
254Readline uses blocking terminal I/O. Under most circumstances, this does
255not cause big delays, but ttys have the potential to block programs
256indefinitely (e.g. on XOFF).
257
258=item unexpected disk I/O
259
260By default, readline does filename completion on TAB, and reads its
261config files.
262
263=item tty settings
264
265After readline has been initialised, it will mangle the termios tty
266settings. This does not normally affect output very much, but should be
267taken into consideration.
268
269=item output intermixing
270
271Your program might wish to print messages (for example, log messages) to
272STDOUT or STDERR. This will usually cause confusion, unless readline is
273hidden with the hide method.
274
275=back
276
277Oh, and the above list is probably not complete.
278
228=head1 AUTHOR, CONTACT, SUPPORT 279=head1 AUTHOR, CONTACT, SUPPORT
229 280
230 Marc Lehmann <schmorp@schmorp.de> 281 Marc Lehmann <schmorp@schmorp.de>
231 http://software.schmorp.de/pkg/AnyEvent-Readline-Gnu.html 282 http://software.schmorp.de/pkg/AnyEvent-Readline-Gnu.html
232 283

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines