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.2 by root, Thu May 10 21:53:03 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines