… | |
… | |
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 | |
59 | use base Term::ReadLine::; |
59 | use base Term::ReadLine::; |
60 | |
60 | |
61 | our $VERSION = '0.1'; |
61 | our $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 | |
65 | Creates a new AnyEvent::ReadLine object. |
65 | Creates a new AnyEvent::ReadLine object. |
66 | |
66 | |
… | |
… | |
73 | this object. |
73 | this object. |
74 | |
74 | |
75 | Once initialised, this module will also restore the terminal settings on a |
75 | Once initialised, this module will also restore the terminal settings on a |
76 | normal program exit. |
76 | normal program exit. |
77 | |
77 | |
|
|
78 | The callback will be installed with the C<CallbackHandlerInstall>, which |
|
|
79 | means it handles history expansion and history, among other things. |
|
|
80 | |
78 | The following key-value pairs are supported: |
81 | The 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 | |
84 | The only mandatory parameter - passes the callback that will receive lines |
87 | The only mandatory parameter - passes the callback that will receive lines |
85 | that are completed by the user. |
88 | that are completed by the user. |
|
|
89 | |
|
|
90 | The string will be in locale-encoding (a multibyte character string). For |
|
|
91 | example, in an utf-8 using locale it will be utf-8. There is no portable |
|
|
92 | way 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 | |
89 | The prompt string to use, defaults to C<< > >>. |
96 | The prompt string to use, defaults to C<< > >>. |
90 | |
97 | |
… | |
… | |
112 | our ($in, $out); |
119 | our ($in, $out); |
113 | |
120 | |
114 | our $saved_point; |
121 | our $saved_point; |
115 | our $saved_line; |
122 | our $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. |
|
|
127 | sub on_line { |
|
|
128 | my $line = shift; |
|
|
129 | my $point = $self->{point}; |
|
|
130 | |
|
|
131 | AE::postpone sub { |
|
|
132 | $cb->($line, $point); |
|
|
133 | }; |
|
|
134 | } |
|
|
135 | |
117 | sub new { |
136 | sub 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 | |
199 | Prints the given strings to the terminal, by first hiding the readline, |
218 | Prints the given strings to the terminal, by first hiding the readline, |
200 | printing the message, and showing it again. |
219 | printing the message, and showing it again. |
201 | |
220 | |
202 | This function cna be called even when readline has never been initialised. |
221 | This function can be called even when readline has never been initialised. |
203 | |
222 | |
204 | The last string should end with a newline. |
223 | The last string should end with a newline. |
205 | |
224 | |
206 | =cut |
225 | =cut |
207 | |
226 | |
… | |
… | |
223 | |
242 | |
224 | 1; |
243 | 1; |
225 | |
244 | |
226 | =back |
245 | =back |
227 | |
246 | |
|
|
247 | =head1 CAVEATS |
|
|
248 | |
|
|
249 | There are some issues with readline that can be problematic in event-based |
|
|
250 | programs: |
|
|
251 | |
|
|
252 | =over 4 |
|
|
253 | |
|
|
254 | =item blocking I/O |
|
|
255 | |
|
|
256 | Readline uses blocking terminal I/O. Under most circumstances, this does |
|
|
257 | not cause big delays, but ttys have the potential to block programs |
|
|
258 | indefinitely (e.g. on XOFF). |
|
|
259 | |
|
|
260 | =item unexpected disk I/O |
|
|
261 | |
|
|
262 | By default, readline does filename completion on TAB, and reads its |
|
|
263 | config files. |
|
|
264 | |
|
|
265 | Tab completion can be disabled by calling C<< $rl->unbind_key (9) >>. |
|
|
266 | |
|
|
267 | =item tty settings |
|
|
268 | |
|
|
269 | After readline has been initialised, it will mangle the termios tty |
|
|
270 | settings. This does not normally affect output very much, but should be |
|
|
271 | taken into consideration. |
|
|
272 | |
|
|
273 | =item output intermixing |
|
|
274 | |
|
|
275 | Your program might wish to print messages (for example, log messages) to |
|
|
276 | STDOUT or STDERR. This will usually cause confusion, unless readline is |
|
|
277 | hidden with the hide method. |
|
|
278 | |
|
|
279 | =back |
|
|
280 | |
|
|
281 | Oh, 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 | |
|
|
290 | L<rltelnet> - a simple tcp_connect-with-readline program using this module. |
|
|
291 | |
|
|
292 | =cut |
|
|
293 | |