File Coverage

blib/lib/AnyEvent/Tickit.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 18 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Use L with L user interface.
2              
3 5     5   301599 use strict;
  5         13  
  5         195  
4 5     5   28 use warnings;
  5         11  
  5         318  
5             package AnyEvent::Tickit;
6             BEGIN {
7 5     5   128 $AnyEvent::Tickit::AUTHORITY = 'cpan:AJGB';
8             }
9             $AnyEvent::Tickit::VERSION = '0.01';
10 5     5   1656 use AnyEvent;
  5         5268  
  5         124  
11 5     5   29 use base qw( Tickit );
  5         9  
  5         655337  
12              
13             sub _capture_weakself {
14             my ($self, $method) = @_;
15              
16             Scalar::Util::weaken $self;
17              
18             my $cb = $self->can($method);
19              
20             return $cb->( $self, @_ );
21             }
22              
23             sub new {
24             my ($class, %args) = @_;
25              
26             my $cv = delete $args{cv};
27              
28             my $self = $class->Tickit::new( %args );
29              
30             $self->{ae_loop} = $cv || AE::cv;
31              
32             $self->{ae_sigwinch} = AE::signal WINCH => sub {
33             $self->_SIGWINCH;
34             };
35              
36             $self->{ae_io} = AE::io $self->term->get_input_handle, 0, sub {
37             $self->_input_readready();
38             };
39              
40             $self->{ae_timer} = AE::timer 0, 0, sub {
41             $self->_timeout();
42             };
43              
44             return $self;
45             }
46              
47             sub get_loop {
48             return $_[0]->{ae_loop};
49             }
50              
51             sub _make_writer {
52             my ($self, $out) = @_;
53              
54             $self->{ae_writer} = AnyEvent::Tickit::Handle->new(
55             fh => $out,
56             no_delay => 1,
57             );
58              
59             return $self->{ae_writer};
60             }
61              
62             sub _input_readready {
63             my $self = shift;
64              
65             my $term = $self->term;
66              
67             undef $self->{timer};
68              
69             $term->input_readable();
70              
71             $self->_timeout;
72             }
73              
74             sub _timeout {
75             my $self = shift;
76              
77             my $term = $self->term;
78             if ( defined( my $timeout = $term->check_timeout) ) {
79             $self->{timer} = AE::timer $timeout / 1000, 0, sub {
80             $self->_timeout();
81             };
82             }
83             }
84              
85             sub later {
86             my ($self, $cb) = @_;
87             AnyEvent::postpone {
88             $cb->();
89             };
90             }
91              
92             sub timer {
93             my $self = shift;
94             my ($mode, $when, $cb) = @_;
95              
96             my $after = $mode eq 'at' ? $when - time : $when;
97              
98             push @{ $self->{ae_timers} }, AE::timer $after, 0, $cb;
99             }
100              
101             sub stop {
102             $_[0]->get_loop->send;
103             }
104              
105             sub run {
106             my $self = shift;
107              
108             $self->setup_term();
109              
110             $self->{ae_sigint} = AE::signal INT => sub {
111             $self->stop;
112             };
113              
114             $self->get_loop->recv;
115              
116             {
117             $self->teardown_term;
118              
119             delete $self->{$_} for qw(
120             ae_sigint
121             ae_sigwinch
122             ae_io
123             ae_loop
124             ae_timer
125             ae_timers
126             );
127             }
128              
129             return 1;
130             }
131              
132             package
133             AnyEvent::Tickit::Handle;
134              
135             use base 'AnyEvent::Handle';
136              
137             *write = \&AnyEvent::Handle::push_write;
138              
139             1;
140              
141             __END__