Jaipo/UI/Console.pm

filter: Statementstmt 30.4% 39 / 128 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 63.1% 12 / 19 Podpod 55.5% 5 / 9 totaltotal 31.8% 56 / 176 CC 10 SCAR 19.9
CC
10
Cov
27%
process_built_in_commands
20.0
run
14.9
parse
12.0
CRAP 7.3
« Jaipo.pm ↑ Summary Jaipo/Service/Jaiku.pm »
1 package Jaipo::UI::Console;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Jaipo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use feature qw(:5.10);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes qw( setitimer ITIMER_REAL ITIMER_PROF time );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =encoding utf8
11  
12 =head1 SYNOPSIS
13  
14 to enter Jaipo console:
15  
16 $ jaipo console
17  
18 > use Twitter # enable Twitter service plugin
19 > use Plurk # enable Plurk service plugin
20 > use RSS # enable RSS plugin
21 > use IRC # enable IRC plugin
22  
23 Jaipo will automatically save your configuration, you only need to use 'use'
24 at first time.
25  
26 to update status
27  
28 > :突然下起大雨。
29  
30 to read all messeges
31 > r
32  
33 Service | User | Message
34 twitter c9s oh hohoho !
35 plurk xxx 剛喝完咖啡.
36 ...
37 ...
38 ...
39  
40 to read messages on Jaiku.com
41  
42 > jaiku r
43  
44 to read someone's messages on Jaiku.com
45  
46 > jaiku r UnitedNation
47  
48 to read public timeline
49  
50 > p
51  
52 to check user's profile
53  
54 > jaiku w IsYourDaddy
55  
56 setup location on Jaiku
57  
58 > jaiku l 我在墾丁,天氣情。
59  
60 to update post to Twitter
61  
62 > twitter :雨停了
63  
64 to reply to someone's post on Twiwter
65  
66 > twitter reply Someone 呆丸朱煮好棒
67  
68 to send direct message to someone on twitter
69  
70 > twitter send mama 媽,我阿雄啦!
71  
72 to follow someone on twitter
73  
74 > twitter follow mama
75  
76 to send a message to a channel on Jaiku
77  
78 > jaiku #TVshow 媽,我上電視了!(揮手)
79  
80 create a regular expression filter for twitter timeline
81  
82 > filter /cor[a-z]*s/i :twitter
83  
84 enter service-only mode
85  
86 > only twitter
87 :twitter>
88  
89 > only jaiku
90 :jaiku>
91  
92  
93 =head1 Command Reference
94  
95 =head2 Global Commands
96  
97 =item m
98  
99 read user updates
100  
101 =item p
102  
103 read friend updates
104  
105 =item g
106  
107 read global updates
108  
109 =head2 Service Commands
110  
111 [SERVICE] :[MESSAGE]
112  
113 [SERVICE] [ACTION] [ ARGUMENTS .. ]
114  
115 =head3 Jaiku
116  
117 =head1 Functions
118  
119 =head2 new
120  
121 =cut
122  
123 1 my $jobj;
124  
125 1 $|=1;
126  
127 sub new {
128 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
129 1 my %args = @_;
130  
131 1 my $self = {};
132 1 bless $self, $class;
133 1 $self->_pre_init;
134 1 return $self;
135 }
136  
137 =head2 _pre_init
138  
139 =cut
140  
141 sub _pre_init {
142 1 ▶ my $self = shift;
Subroutine _pre_init: called CC=1 SCAR=0.0
143 # binmode STDOUT,":utf8";
144  
145 }
146  
147 =head2 setup service
148  
149 =cut
150  
151 sub setup_service {
152 0 ▶ my ( $self , $args , $opt ) = @_;
Subroutine setup_service: not called CC=2 SCAR=17.9
Pod undocumented
153  
154 0 print "Init " , $args->{package_name} , ":\n";
155 0 for my $column ( @{ $args->{require_args} } ) {
156 0 my ($column_name , $column_option ) = each %$column;
157  
158 0 print $column_option->{label} , ":" ;
159 0 my $val = <STDIN>;
160 0 chomp $val;
161  
162 0 $opt->{$column_name} = $val;
163 }
164  
165 }
166  
167 =head2 init
168  
169 =cut
170  
171 sub init {
172 1 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
173 1 $self->print_welcome;
174  
175 # init Jaipo here
176 1 $jobj = Jaipo->new;
177 1 $jobj->init( $self );
178  
179  
180 }
181  
182 =head2 print_help
183  
184 =cut
185  
186 sub print_help {
187 0 ▶ my $self = shift;
Subroutine print_help: not called CC=1 SCAR=6.9
Pod documented
188  
189 0 print <<HELP ;
190  
191 m|mine to read updates of your own
192 p|public to read public messages (from friends,channels)
193 g|global to read global messages (from the whole world)
194  
195 :[message] update a message to all services.
196  
197 eval eval a part of code.
198  
199 conf edit edit configuration
200 conf load load configuration
201 conf save save configuration
202  
203 use [service trigger]
204  
205 f create create a filter
206 ? print help
207  
208 [service] [message]
209 [service] :[action] [arguments]
210  
211 HELP
212  
213 }
214  
215  
216 =head2 print_welcome
217  
218 =cut
219  
220 sub print_welcome {
221 1 ▶ print <<'END';
Subroutine print_welcome: called CC=1 SCAR=0.0
Pod documented
222 _________________________________________
223 Jaipo Console
224  
225 version 0.22
226 Type ? for help
227 Type [service] ? for service plugin help
228 Type :[message] to send a message
229  
230 END
231  
232 }
233  
234  
235 =head2 process_built_in_commands
236  
237 =cut
238  
239 sub process_built_in_commands {
240 0 ▶ my ($self ,$line ) = @_;
Subroutine process_built_in_commands: not called CC=4 SCAR=30.0
Pod documented
241  
242 0 $line =~ s/^://;
243  
244 0 given ($line) {
245  
246 # eval code
247 # such like: eval $jobj->_list_trigger;
248 0 when ( m/^eval (.*)$/i ) {
249 # eval a code
250 0 my $code = $1;
251 0 eval $1;
252 }
253  
254 0 when ( m/^conf\s+edit$/i ) {
255 0 ▶ my $editor = $ENV{EDITOR} || 'nano' ;
Condition0%0/2
$ENV{'EDITOR'} || 'nano'
l!l
00
MC/DC0%0/1
$ENV{'EDITOR'} || 'nano'
$ENV{'EDITOR'}
Truth table0%0/2
A
A= $ENV{'EDITOR'}
Aresult
11
00
256 0 my $file = Jaipo->config->app_config_path;
257  
258 0 print "Launching editor .. $editor\n";
259 0 qx{$editor $file};
260  
261 }
262  
263 0 when ( m/^conf\s+save$/i ) {
264 # TODO
265 # save configuration
266  
267 }
268  
269 0 when ( m/^conf\s+load$/i ) {
270 # TODO
271 # load configuration
272 # when user modify configuration by self, user can reload
273 # configuration
274  
275  
276 }
277  
278 0 when ( m/^list$/i ) {
279 0 $jobj->list_triggers;
280 }
281  
282  
283 # built-in commands
284 # TODO:
285 #
286 0 when (m/^(?:u|use)\s*/i) {
287 0 $line =~ s/;$//;
288 0 my @ops = split /\s+/,$line;
289 0 shift @ops; # shift out use command
290 0 my ( $service_name , $trigger_name ) = @ops;
291  
292 # init service plugins
293 # TODO:
294 # check if user specify trigger name
295 0 ▶ if ( !$service_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $service_name) { } 0 0 0
296 0 $jobj->list_triggers;
297 }
298 else {
299 0 $service_name = ucfirst $service_name;
300 0 ▶ $trigger_name ||= lc $service_name;
Condition0%0/3
$trigger_name ||= lc $service_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$trigger_name ||= lc $service_name
$trigger_name lc $service_name
Truth table0%0/3
A ||= B
A= $trigger_nameB= lc $service_name
ABresult
1X1
011
000
301 0 print "Trying to load $service_name => $trigger_name\n";
302 0 $jobj->runtime_load_service ( $self, $service_name, $trigger_name );
303 0 print "Done\n";
304 }
305 }
306  
307  
308 # Global Actions
309 #
310
311 0 when ( m/^(s|send)/i ) {
312 0 $jobj->action( "send_msg", $line );
313  
314 }
315
316 0 when ( m/^(m|mine)/i ) {
317 0 $jobj->action( "read_user_timeline", $line );
318  
319 }
320  
321 0 when ( m/^(p|public)/i ) {
322 0 $jobj->action( "read_public_timeline", $line );
323  
324 }
325  
326 0 when ( m/^(g|global)/i ) {
327 0 $jobj->action( "read_global_timeline", $line );
328  
329 }
330  
331 # XXX: let -p , -m works
332 0 when (m/^-$/) {
333 0 $jobj->cache_clear;
334 0 Jaipo->logger->info("Cache Flushed");
335 }
336  
337 0 when (m/^mark\s+as\s+read/i) {
338 0 $jobj->cache_clear();
339 0 Jaipo->logger->info("Cache Flushed");
340 }
341  
342  
343 # something like filter create /regexp/ :twitter:public
344 0 when (m/^(f|filter)\s/i) {
345 0 my ( $cmd, $action, @params ) = split /\s+/, $line;
346 }
347  
348 0 when ('?') {
349 0 $self->print_help;
350 }
351  
352 # try to find the trigger , if match then do it
353 # or show up command not found
354 0 default {
355 # dispatch to service
356 0 my ( $service_tg, $rest_line ) = ( $line =~ m/^(\w+)\s*(.*)/i );
357 0 $jobj->dispatch_to_service( $service_tg, $rest_line );
358 }
359 }
360  
361  
362 }
363  
364 sub parse {
365 0 ▶ my ($self,$line) = @_;
Subroutine parse: not called CC=3 SCAR=24.8
Pod undocumented
366 0 $line =~ s/^\s*//g;
367 0 $line =~ s/\s*$//g;
368 0 ▶ return unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
369 0 ▶ if( $line =~ m/^:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^:/) { } 0 0 0
370 # do update status message if string start with ":"
371 0 $line =~ s/^://;
372 0 $jobj->action ( "send_msg", $line );
373 }
374 else {
375 0 $self->process_built_in_commands( $line );
376 }
377 }
378  
379  
380 sub auto_update {
381 0 ▶ print "Auto-Update Mode\n";
Subroutine auto_update: not called CC=1 SCAR=6.9
Pod undocumented
382 $SIG{ALRM} = sub {
383 0 ▶ print "Updating ... \r";
Subroutine __ANON__: not called CC=1 SCAR=6.9
384 0 $jobj->action( "read_public_timeline" );
385 0 print "Done" . ' ' x 10 . "\r";
386 0 };
387 0 setitimer(ITIMER_REAL , 1 , 10);
388 }
389  
390 sub run {
391 0 ▶ my $self = shift;
Subroutine run: not called CC=4 SCAR=27.0
Pod undocumented
392 0 my $prompt = "jaipo> ";
393 1 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
394 # XXX: Term::ReadLine doesnt support UTF-8
395 # my $term = new Term::ReadLine; # 'Simple Perl';
396 # my $OUT = $term->OUT || \*STDOUT;
397 # binmode $OUT,":utf8";
398 0 binmode STDIN,":utf8";
399 0 binmode STDOUT,":utf8";
400 # while ( defined ($_ = $term->readline($prompt)) ) {
401 0 while ( 1 ) {
402 0 print $prompt;
403 0 my $line = <STDIN>;
404 0 chomp $line;
405 0 ▶ unless( $line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line) 0 0 0
406 0 $self->auto_update;
407 0 <STDIN>;
408 0 $SIG{ALRM} = undef;
409 0 setitimer(ITIMER_REAL , 0 );
410 }
411 0 ▶ chomp $line if( $line );
Branch: 1 branch
BranchTrueFalseTotal
if $line 0 0 0
412 0 eval( q{ $self->parse ( $line ); } );
413 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
414 # print $OUT $res, "\n" unless $@;
415 # $term->addhistory($_) if /\S/;
416 }
417 }
418  
419 1 1;
« Jaipo.pm ↑ Summary Jaipo/Service/Jaiku.pm »