… | |
… | |
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 = '0.2'; |
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 | |
… | |
… | |
81 | |
81 | |
82 | =item on_line => $cb->($string) |
82 | =item on_line => $cb->($string) |
83 | |
83 | |
84 | The only mandatory parameter - passes the callback that will receive lines |
84 | The only mandatory parameter - passes the callback that will receive lines |
85 | that are completed by the user. |
85 | that are completed by the user. |
|
|
86 | |
|
|
87 | The string will be in locale-encoding (a multibyte character string). For |
|
|
88 | example, in an utf-8 using locale it will be utf-8. There is no portable |
|
|
89 | way 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 | |
89 | The prompt string to use, defaults to C<< > >>. |
93 | The prompt string to use, defaults to C<< > >>. |
90 | |
94 | |
… | |
… | |
112 | our ($in, $out); |
116 | our ($in, $out); |
113 | |
117 | |
114 | our $saved_point; |
118 | our $saved_point; |
115 | our $saved_line; |
119 | our $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. |
|
|
124 | sub on_line { |
|
|
125 | my $line = shift; |
|
|
126 | my $point = $self->{point}; |
|
|
127 | |
|
|
128 | AE::postpone sub { |
|
|
129 | $cb->($line, $point); |
|
|
130 | }; |
|
|
131 | } |
|
|
132 | |
117 | sub new { |
133 | sub 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 | |
199 | Prints the given strings to the terminal, by first hiding the readline, |
216 | Prints the given strings to the terminal, by first hiding the readline, |
200 | printing the message, and showing it again. |
217 | printing the message, and showing it again. |
201 | |
218 | |
202 | This function cna be called even when readline has never been initialised. |
219 | This function can be called even when readline has never been initialised. |
203 | |
220 | |
204 | The last string should end with a newline. |
221 | The last string should end with a newline. |
205 | |
222 | |
206 | =cut |
223 | =cut |
207 | |
224 | |