blib/lib/UI/Bowdlator/Filter.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 85 CC 6 SCAR 19.8
CC
6
Cov
0%
new
12.0
getKey
12.0
commit
6.0
CRAP 7.2
1 use strict;
2 use warnings;
3 package UI::Bowdlator::Filter;
4 use IO::Socket::UNIX;
5  
6 # ABSTRACT: Provides convenience function for specifing input Filters to Bowdlator
7 our $VERSION = '0.002'; # VERSION
8  
9 use Carp;
10 my $DEFAULT_SOCK = '/usr/local/var/run/bowdlator.sock';
11  
12 =pod
13  
14 =head1 NAME
15  
16 UI::Bowdlator::Filter - Helper for specifying input filters to Bowdlator
17  
18  
19 =head1 SYNOPSIS
20  
21 # maps all typed characters to uppercase
22 use UI::Bowdlator::Filter;
23  
24 # connect to Bowdlator server
25 my $bowdlator = UI::Bowdlator::Filter->new()
26 or die "Bowdlator server not online\n";
27  
28 my $composed = '';
29 while ($bowdlator->getKey(handle_backspace => \$composed)) {
30  
31 if (/^[^[:graph:]]/a) { # non graphical character ends composition
32 $bowdlator->commit(\$composed);
33 next;
34 }
35  
36 $composed .= uc;
37  
38 $bowdlator->suggest($composed);
39 }
40  
41 =head1 DESCRIPTION
42  
43 Makes writing filters for Bowdlator (L<http://github.com/a3f/Bowdlator>) easier.
44  
45  
46 =head1 METHODS AND ARGUMENTS
47  
48 =over 4
49  
50 =item new([$sock])
51  
52 Connects to a running Bowdlator C<AF_UNIX> socket. Returns C<undef> on connection failure.
53 Default socket is C</usr/local/var/run/bowdlator.sock>. Optionally, an actual socket or a path to one can be specified.
54  
55 =cut
56  
57 sub new {
58 my $type = shift;
59 my $sock = shift || $DEFAULT_SOCK;
60 $sock = IO::Socket::UNIX->new(
61 Type => SOCK_STREAM,
62 Peer => $sock,
63 ) if ref($sock) ne 'IO::Socket::UNIX';
64 return undef unless $sock;
65  
66 binmode $sock, ":encoding(UTF-8)";
67 my $self->{sock} = $sock;
68 bless $self, $type;
69 return $self;
70 }
71  
72 =item getKey([keep_nul => 0, buffer_size => 160, handle_backspace => undef])
73  
74 Blocks till the user types a Key while Bowdlator is selected. Accepts following optional arguments:
75  
76 =over 4
77  
78 =item handle_backspace
79  
80 User code can offload backspace handling to the module. On backspace receipt, the module will discard the composed string's last character, and chop and return the one before it, so it can be rehandled. If the buffer is empty, a backspace (C<\b>) is returned.
81  
82 =item keep_nul
83  
84 Bowdlator sends NUL-terminated strings. The module strips them unless instructed otherwise.
85  
86 =item buffer_size
87  
88 The recv buffer size. This shouldn't need changing. Default is 160.
89  
90  
91 =back
92  
93 C<undef> is returned on socket error.
94  
95 =cut
96  
97 sub getKey {
98 my $self = shift;
99 my %opts = (
100 keep_nul => 0,
101 buffer_size => 160,
102 handle_backspace => undef,
103  
104 @_
105 );
106  
107 my $data;
108 defined $self->{sock}->recv($data, $opts{buffer_size}) or return $_ = undef;
109 chop $data if !$opts{keep_nul}; # strips NUL, so client doesn't have to
110 $_ = $data;
111
112 # handle backspace
113 if (defined $opts{handle_backspace} && /^[\b]/) {
114 chop ${$opts{handle_backspace}};
115 $_ = chop ${$opts{handle_backspace}};
116 $_ = $_ || "\b"; # return backspace if deleting beyond buffer
117 }
118  
119 return $_;
120 }
121  
122 =item suggest($display, @candidates)
123  
124 Sends off a suggestion to C<$display> and a list of C<@candidates> to choose from. (Candidates list support not immplemented yet).
125  
126 =cut
127  
128 sub suggest {
129 my $self = shift;
130 my ($displayed, @candidates) = @_;
131  
132 $self->{sock}->write($displayed);
133 }
134  
135 =item commit(\$commit)
136  
137 Sends off the final string to C<$commit>. If C<$commit> is a C<ref>, it will be cleared.
138  
139 =cut
140  
141 sub commit {
142 my $self = shift;
143 my $commit = $_[0] || '';
144 $commit = $$commit, ${$_[0]} = '' if ref $commit;
145  
146 $self->{sock}->write("$commit\0\4\0");
147 }
148  
149 1;
150  
151 __END__