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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines