blib/lib/AnyEvent/Tickit.pm

filter: Statementstmt 100% 13 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 18 / 18 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 # ABSTRACT: Use L<AnyEvent> with L<Tickit> user interface.
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 package AnyEvent::Tickit;
6 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 $AnyEvent::Tickit::AUTHORITY = 'cpan:AJGB';
8 }
9 $AnyEvent::Tickit::VERSION = '0.01';
10 6 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use base qw( Tickit );
Subroutine BEGIN: called CC=1 SCAR=0.0
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__
↑ Summary