| 1 | package Term::ReadLine::Event; | ||||||||||||||
| 2 | { | ||||||||||||||
| 3 | 9 | $Term::ReadLine::Event::VERSION = '0.04'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 9 | ▶ | use 5.006; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 9 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 9 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 9 | ▶ | use Term::ReadLine 1.09; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | # ABSTRACT: Wrappers for Term::ReadLine's new event_loop model. | ||||||||||||||
| 14 | |||||||||||||||
| 15 | |||||||||||||||
| 16 | sub _new { | ||||||||||||||
| 17 | 0 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
_new: not called CC=3 SCAR=24.8
| |||||||||||||||
| 18 | 0 | my $app = shift; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 0 | my $self = bless {@_}, $class; | |||||||||||||
| 21 | |||||||||||||||
| 22 | 0 | ▶ | $self->{_term} = blessed $app ? $app : | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 23 | Term::ReadLine->new(ref $app ? @$app : $app); | ||||||||||||||
| 24 | 0 | $self; | |||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | |||||||||||||||
| 28 | sub with_AnyEvent { | ||||||||||||||
| 29 | 0 | ▶ | my $self = _new(@_); | ||||||||||||
|
Subroutine
with_AnyEvent: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 30 | |||||||||||||||
| 31 | $self->trl->event_loop( | ||||||||||||||
| 32 | sub { | ||||||||||||||
| 33 | 0 | ▶ | my $data = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 34 | 0 | $data->[0] = AE::cv(); | |||||||||||||
| 35 | 0 | $data->[0]->recv(); | |||||||||||||
| 36 | }, sub { | ||||||||||||||
| 37 | 0 | ▶ | my $fh = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 38 | |||||||||||||||
| 39 | # The data for AE are: the file event watcher (which | ||||||||||||||
| 40 | # cannot be garbage collected until we're done) and | ||||||||||||||
| 41 | # a placeholder for the condvar we're sharing between | ||||||||||||||
| 42 | # the AE::io callback created here and the wait | ||||||||||||||
| 43 | # callback above. | ||||||||||||||
| 44 | 0 | my $data = []; | |||||||||||||
| 45 | 0 | ▶ | $data->[1] = AE::io($fh, 0, sub { $data->[0]->send() }); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 46 | 0 | $data; | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | 0 | ); | |||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | $self; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | |||||||||||||||
| 54 | sub with_Coro { | ||||||||||||||
| 55 | 0 | ▶ | my $self = _new(@_); | ||||||||||||
|
Subroutine
with_Coro: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 56 | 0 | require Coro::Handle; | |||||||||||||
| 57 | |||||||||||||||
| 58 | $self->trl->event_loop( | ||||||||||||||
| 59 | sub { | ||||||||||||||
| 60 | # Tell Coro to wait until we have something to read, | ||||||||||||||
| 61 | # and then we can return. | ||||||||||||||
| 62 | 0 | ▶ | shift->readable(); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 63 | }, sub { | ||||||||||||||
| 64 | # in Coro, we just need to unblock the filehandle, | ||||||||||||||
| 65 | # and save the unblocked filehandle. | ||||||||||||||
| 66 | 0 | ▶ | my $fh = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 67 | 0 | Coro::Handle::unblock $fh; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | 0 | ); | |||||||||||||
| 70 | 0 | $self; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | |||||||||||||||
| 74 | sub with_IO_Async { | ||||||||||||||
| 75 | 0 | ▶ | my $self = _new(@_); | ||||||||||||
|
Subroutine
with_IO_Async: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 76 | |||||||||||||||
| 77 | $self->trl->event_loop( | ||||||||||||||
| 78 | sub { | ||||||||||||||
| 79 | 0 | ▶ | my $ready = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 80 | 0 | $$ready = 0; | |||||||||||||
| 81 | 0 | ▶ | $self->{loop}->loop_once while !$$ready; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | }, | ||||||||||||||
| 83 | sub { | ||||||||||||||
| 84 | 0 | ▶ | my $fh = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 85 | |||||||||||||||
| 86 | # The data for IO::Async is just the ready flag. To | ||||||||||||||
| 87 | # ensure we're referring to the same value, this is | ||||||||||||||
| 88 | # a SCALAR ref. | ||||||||||||||
| 89 | 0 | my $ready = \ do{my $dummy}; | |||||||||||||
| 90 | $self->{loop}->add( | ||||||||||||||
| 91 | $self->{watcher} = | ||||||||||||||
| 92 | IO::Async::Handle->new( | ||||||||||||||
| 93 | read_handle => $fh, | ||||||||||||||
| 94 | 0 | ▶ | on_read_ready => sub { $$ready = 1 }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 95 | ) | ||||||||||||||
| 96 | 0 | ); | |||||||||||||
| 97 | 0 | $ready; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | 0 | ); | |||||||||||||
| 100 | |||||||||||||||
| 101 | $self->{_cleanup} = sub { | ||||||||||||||
| 102 | 0 | ▶ | my $s = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 103 | 0 | $s->{loop}->remove($s->{watcher}); | |||||||||||||
| 104 | 0 | }; | |||||||||||||
| 105 | |||||||||||||||
| 106 | 0 | $self; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | |||||||||||||||
| 110 | sub with_POE | ||||||||||||||
| 111 | { | ||||||||||||||
| 112 | 0 | ▶ | my $self = _new(@_); | ||||||||||||
|
Subroutine
with_POE: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | my $waiting_for_input; | |||||||||||||
| 115 | |||||||||||||||
| 116 | POE::Session->create( | ||||||||||||||
| 117 | inline_states => { | ||||||||||||||
| 118 | |||||||||||||||
| 119 | # Initialize the session that will drive Term::ReadLine. | ||||||||||||||
| 120 | # Tell Term::ReadLine to invoke a couple POE event handlers when | ||||||||||||||
| 121 | # it's ready to wait for input, and when it needs to register an | ||||||||||||||
| 122 | # I/O watcher. | ||||||||||||||
| 123 | |||||||||||||||
| 124 | _start => sub { | ||||||||||||||
| 125 | 0 | ▶ | $self->trl->event_loop( | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 126 | $_[POE::Session->SESSION]->callback('term_readline_waitfunc'), | ||||||||||||||
| 127 | $_[POE::Session->SESSION]->callback('term_readline_regfunc'), | ||||||||||||||
| 128 | ); | ||||||||||||||
| 129 | }, | ||||||||||||||
| 130 | |||||||||||||||
| 131 | # This callback is invoked every time Term::ReadLine wants to | ||||||||||||||
| 132 | # read something from its input file handle. It blocks | ||||||||||||||
| 133 | # Term::ReadLine until input is seen. | ||||||||||||||
| 134 | # | ||||||||||||||
| 135 | # It sets a flag indicating that input hasn't arrived yet. | ||||||||||||||
| 136 | # It watches Term::ReadLine's input filehandle for input. | ||||||||||||||
| 137 | # It runs while it's waiting for input. | ||||||||||||||
| 138 | # It turns off the input watcher when it's no longer needed. | ||||||||||||||
| 139 | # | ||||||||||||||
| 140 | # POE::Kernel's run_while() dispatches other events (including | ||||||||||||||
| 141 | # "term_readline_readable" below) until $waiting_for_input goes | ||||||||||||||
| 142 | # to zero. | ||||||||||||||
| 143 | |||||||||||||||
| 144 | term_readline_waitfunc => sub { | ||||||||||||||
| 145 | 0 | ▶ | my $input_handle = $_[POE::Session->ARG1][0]; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 146 | 0 | $waiting_for_input = 1; | |||||||||||||
| 147 | 0 | $_[POE::Session->KERNEL]->select_read($input_handle => 'term_readline_readable'); | |||||||||||||
| 148 | 0 | $_[POE::Session->KERNEL]->run_while(\$waiting_for_input); | |||||||||||||
| 149 | 0 | $_[POE::Session->KERNEL]->select_read($input_handle => undef); | |||||||||||||
| 150 | }, | ||||||||||||||
| 151 | |||||||||||||||
| 152 | # This callback is invoked as Term::ReadLine is starting up for | ||||||||||||||
| 153 | # the first time. It saves the exposed input filehandle where | ||||||||||||||
| 154 | # the "term_readline_waitfunc" callback can see it. | ||||||||||||||
| 155 | |||||||||||||||
| 156 | term_readline_regfunc => sub { | ||||||||||||||
| 157 | 0 | ▶ | my $input_handle = $_[POE::Session->ARG1][0]; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 158 | 0 | return $input_handle; | |||||||||||||
| 159 | }, | ||||||||||||||
| 160 | |||||||||||||||
| 161 | # This callback is invoked when data is seen on Term::ReadLine's | ||||||||||||||
| 162 | # input filehandle. It clears the $waiting_for_input flag. | ||||||||||||||
| 163 | # This causes run_while() to return in "term_readline_waitfunc". | ||||||||||||||
| 164 | |||||||||||||||
| 165 | term_readline_readable => sub { | ||||||||||||||
| 166 | 0 | ▶ | $waiting_for_input = 0; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 167 | }, | ||||||||||||||
| 168 | }, | ||||||||||||||
| 169 | 0 | ); | |||||||||||||
| 170 | 0 | $self; | |||||||||||||
| 171 | } | ||||||||||||||
| 172 | |||||||||||||||
| 173 | |||||||||||||||
| 174 | sub with_Reflex | ||||||||||||||
| 175 | { | ||||||||||||||
| 176 | 0 | ▶ | my $self = _new(@_); | ||||||||||||
|
Subroutine
with_Reflex: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 177 | |||||||||||||||
| 178 | $self->trl->event_loop( | ||||||||||||||
| 179 | sub { | ||||||||||||||
| 180 | 0 | ▶ | my $input_watcher = shift(); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 181 | 0 | $input_watcher->next(); | |||||||||||||
| 182 | }, | ||||||||||||||
| 183 | sub { | ||||||||||||||
| 184 | 0 | ▶ | my $input_handle = shift(); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 185 | 0 | my $input_watcher = Reflex::Filehandle->new( | |||||||||||||
| 186 | handle => $input_handle, | ||||||||||||||
| 187 | rd => 1, | ||||||||||||||
| 188 | ); | ||||||||||||||
| 189 | 0 | return $input_watcher; | |||||||||||||
| 190 | }, | ||||||||||||||
| 191 | 0 | ); | |||||||||||||
| 192 | |||||||||||||||
| 193 | |||||||||||||||
| 194 | 0 | $self; | |||||||||||||
| 195 | } | ||||||||||||||
| 196 | |||||||||||||||
| 197 | |||||||||||||||
| 198 | sub with_Tk | ||||||||||||||
| 199 | { | ||||||||||||||
| 200 | 0 | ▶ | my $self = _new(@_); | ||||||||||||
|
Subroutine
with_Tk: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 201 | |||||||||||||||
| 202 | $self->trl->event_loop( | ||||||||||||||
| 203 | sub { | ||||||||||||||
| 204 | 0 | ▶ | my $data = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 205 | 0 | ▶ | Tk::DoOneEvent(0) until $$data; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 206 | 0 | $$data = 0; | |||||||||||||
| 207 | }, | ||||||||||||||
| 208 | sub { | ||||||||||||||
| 209 | # save filehandle for unhooking later. | ||||||||||||||
| 210 | 0 | ▶ | $self->{tkFH} = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 211 | 0 | my $data; | |||||||||||||
| 212 | 0 | $$data = 0; | |||||||||||||
| 213 | 0 | ▶ | Tk->fileevent($self->{tkFH}, 'readable', sub { $$data = 1 }); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 214 | 0 | $data; | |||||||||||||
| 215 | }, | ||||||||||||||
| 216 | 0 | ); | |||||||||||||
| 217 | |||||||||||||||
| 218 | $self->{_cleanup} = sub { | ||||||||||||||
| 219 | 0 | ▶ | my $s = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 220 | 0 | Tk->fileevent($s->{tkFH}, 'readable', ""); | |||||||||||||
| 221 | 0 | }; | |||||||||||||
| 222 | |||||||||||||||
| 223 | 0 | $self; | |||||||||||||
| 224 | } | ||||||||||||||
| 225 | |||||||||||||||
| 226 | |||||||||||||||
| 227 | sub DESTROY | ||||||||||||||
| 228 | { | ||||||||||||||
| 229 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||
| 230 | |||||||||||||||
| 231 | 0 | local $@; | |||||||||||||
| 232 | 0 | eval { | |||||||||||||
| 233 | 0 | $self->trl->event_loop(undef); | |||||||||||||
| 234 | |||||||||||||||
| 235 | 0 | ▶ | $self->{_cleanup}->($self) if $self->{_cleanup}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 236 | }; | ||||||||||||||
| 237 | } | ||||||||||||||
| 238 | |||||||||||||||
| 239 | |||||||||||||||
| 240 | sub trl | ||||||||||||||
| 241 | { | ||||||||||||||
| 242 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
trl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 243 | 0 | $self->{_term}; | |||||||||||||
| 244 | } | ||||||||||||||
| 245 | |||||||||||||||
| 246 | 9 | our $AUTOLOAD; | |||||||||||||
| 247 | sub AUTOLOAD | ||||||||||||||
| 248 | { | ||||||||||||||
| 249 | 0 | ▶ | (my $f = $AUTOLOAD) =~ s/.*:://; | ||||||||||||
|
Subroutine
AUTOLOAD: not called CC=1 SCAR=2.2
| |||||||||||||||
| 250 | |||||||||||||||
| 251 | 9 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 252 | 0 | *{$f} = sub { | |||||||||||||
| 253 | 0 | ▶ | shift->trl()->$f(@_); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 254 | 0 | }; | |||||||||||||
| 255 | |||||||||||||||
| 256 | 0 | goto &$f; | |||||||||||||
| 257 | } | ||||||||||||||
| 258 | |||||||||||||||
| 259 | |||||||||||||||
| 260 | 9 | 1; # End of Term::ReadLine::Event | |||||||||||||
| 261 | |||||||||||||||
| 262 | __END__ | ||||||||||||||