blib/lib/Circle/Session/Tabbed.pm

filter: Statementstmt 14.7% 28 / 190 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 31.0% 9 / 29 Podpod 6.2% 1 / 16 totaltotal 11.4% 38 / 331 SCAR 76.5
CC
53
Cov
10%
command_tab_move
156.0
command_list
110.0
command_tab_goto
110.0
CRAP 2105.6
« blib/lib/Circle/Net/IRC/Channel.pm ↑ Summary blib/lib/Circle/Commandable.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2014 -- leonerd@leonerd.org.uk
4  
5 package Circle::Session::Tabbed;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use base qw( Tangence::Object Circle::Commandable Circle::Configurable );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub _session_type
12 {
13 0 ▶ my ( $opts ) = @_;
Subroutine _session_type: not called CC=2 SCAR=17.9
14  
15 0 ▶ keys %$opts or return __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
keys %$opts or return __PACKAGE__ 0 0 0
16  
17 0 print STDERR "Need Tabbed FE session for extra options:\n";
18 0 print STDERR " ".join( "|", sort keys %$opts )."\n";
19  
20 0 return undef;
21 }
22  
23 sub new
24 {
25 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my %args = @_;
27  
28 0 my $self = $class->SUPER::new( %args );
29  
30 0 $self->{root} = $args{root};
31 0 $self->{identity} = $args{identity};
32  
33 # Start with just the root object in first tab
34  
35 0 $self->set_prop_tabs( [ $args{root} ] );
36 0 $self->{items} = {};
37  
38 0 return $self;
39 }
40  
41 sub items
42 {
43 0 ▶ my $self = shift;
Subroutine items: not called CC=1 SCAR=6.9
Pod undocumented
44 0 return @{ $self->get_prop_tabs };
45 }
46  
47 sub describe
48 {
49 0 ▶ my $self = shift;
Subroutine describe: not called CC=1 SCAR=6.9
Pod documented
50 0 return __PACKAGE__."[$self->{identity}]";
51 }
52  
53 sub _item_to_index
54 {
55 0 ▶ my $self = shift;
Subroutine _item_to_index: not called CC=3 SCAR=24.8
56 0 my ( $item ) = @_;
57  
58 0 my @items = $self->items;
59 0 ▶ $items[$_] == $item and return $_ for 0 .. $#items;
Branch: 1 branch
BranchTrueFalseTotal
$items[$_] == $item and return $_ 0 0 0
60  
61 0 return undef;
62 }
63  
64 sub show_item
65 {
66 0 ▶ my $self = shift;
Subroutine show_item: not called CC=2 SCAR=17.9
Pod undocumented
67 0 my ( $item ) = @_;
68  
69 0 ▶ return if grep { $_ == $item } $self->items;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $item;} $self->items 0 0 0
70  
71 0 $self->push_prop_tabs( $item );
72 }
73  
74 sub unshow_item
75 {
76 0 ▶ my $self = shift;
Subroutine unshow_item: not called CC=3 SCAR=24.8
Pod undocumented
77 0 my ( $item ) = @_;
78  
79 0 my $index;
80 0 ▶ if( ref $item ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) { } 0 0 0
81 0 $index = $self->_item_to_index( $item );
82 0 ▶ return unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 0 0
83 }
84 else {
85 0 $index = $item;
86 }
87  
88 0 $self->splice_prop_tabs( $index, 1, () );
89 }
90  
91 sub new_item
92 {
93 0 ▶ my $self = shift;
Subroutine new_item: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my ( $item ) = @_;
95  
96 # Did we know about it?
97 0 ▶ return if exists $self->{items}->{$item};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"items"}{$item} 0 0 0
98  
99 0 $self->{items}->{$item} = 1;
100 0 $self->show_item( $item );
101 }
102  
103 sub delete_item
104 {
105 0 ▶ my $self = shift;
Subroutine delete_item: not called CC=1 SCAR=6.9
Pod undocumented
106 0 my ( $item ) = @_;
107  
108 0 delete $self->{items}->{$item};
109 0 $self->unshow_item( $item );
110 }
111  
112 sub clonefrom
113 {
114 0 ▶ my $self = shift;
Subroutine clonefrom: not called CC=4 SCAR=30.0
Pod undocumented
115 0 my ( $src ) = @_;
116  
117 0 my @srcitems = $src->items;
118  
119 0 foreach my $index ( 0 .. $#srcitems ) {
120 0 my $item = $srcitems[$index];
121  
122 0 my $curindex = $self->_item_to_index( $item );
123 0 ▶ if( !defined $curindex ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $curindex) { } 0 0 0
elsif ($curindex != $index) { } 0 0 0
124 0 $self->splice_prop_tabs( $index, 0, $item );
125 }
126 elsif( $curindex != $index ) {
127 0 $self->move_prop_tabs( $curindex, $index - $curindex );
128 }
129 }
130  
131 0 $self->splice_prop_tabs( scalar @srcitems, scalar $self->items - scalar @srcitems, () );
132 }
133  
134 sub _get_item
135 {
136 0 ▶ my $self = shift;
Subroutine _get_item: not called CC=9 SCAR=45.0
137 0 my ( $path, $curitem, $create ) = @_;
138  
139 0 ▶ $curitem or $path =~ m{^/} or croak "Cannot walk a relative path without a start item";
Branch: 1 branch
BranchTrueFalseTotal
$curitem or $path =~ m[^/] or croak("Cannot walk a relative path without a start item") 0 0 0
Condition0%0/3
$curitem or $path =~ m[^/]
l!l&&r!l&&!r
000
MC/DC0%0/2
$curitem or $path =~ m[^/]
$curitem $path =~ m[^/]
Truth table0%0/3
A or B
A= $curitemB= $path =~ m[^/]
ABresult
1X1
011
000
140  
141 0 ▶ $curitem = $self->{root} if $path =~ s{^/}{};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ s[^/][] 0 0 0
142  
143 0 foreach ( split( m{/}, $path ) ) {
144 0 ▶ next unless length $_; # skip empty path elements
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 0 0 0
145  
146 0 my $nextitem;
147 0 ▶ if( $curitem->can( "get_item" ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($curitem->can('get_item')) { } 0 0 0
elsif ($curitem->can('enumerate_items')) { } 0 0 0
148 0 $nextitem = $curitem->get_item( $_, $create );
149 }
150 elsif( $curitem->can( "enumerate_items" ) ) {
151 0 $nextitem = $curitem->enumerate_items->{$_};
152 }
153 else {
154 0 die "@{[ $curitem->describe ]} has no child items\n";
155 }
156  
157 0 ▶ defined $nextitem or die "@{[ $curitem->describe ]} has no child item called $_\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $nextitem or die "@{[$curitem->describe];} has no child item called $_\n" 0 0 0
158  
159 0 $curitem = $nextitem;
160 }
161  
162 0 return $curitem;
163 }
164  
165 sub _cat_path
166 {
167 0 ▶ my ( $p, $q ) = @_;
Subroutine _cat_path: not called CC=3 SCAR=24.8
168  
169 0 ▶ return $q if $p eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $p eq "" 0 0 0
170 0 ▶ return "/$q" if $p eq "/";
Branch: 1 branch
BranchTrueFalseTotal
if $p eq "/" 0 0 0
171 0 return "$p/$q";
172 }
173  
174 sub load_configuration
175 {
176 0 ▶ my $self = shift;
Subroutine load_configuration: not called CC=2 SCAR=17.9
Pod undocumented
177 0 my ( $ynode ) = @_;
178  
179 0 $self->set_prop_tabs( [] );
180  
181 0 foreach my $tab ( @{ $ynode->{tabs} } ) {
182 0 my $item = $self->_get_item( $tab, $self->{root}, 1 );
183 0 $self->push_prop_tabs( $item );
184 }
185 }
186  
187 sub store_configuration
188 {
189 0 ▶ my $self = shift;
Subroutine store_configuration: not called CC=2 SCAR=17.9
Pod undocumented
190 0 my ( $ynode ) = @_;
191  
192 $ynode->{tabs} = [ map {
193 0 my $item = $_;
194 0 my @components;
195 0 ▶ while( $item ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item) 0 0 0
196 0 unshift @components, $item->enumerable_name;
197 0 $item = $item->parent;
198 }
199 0 join "/", @components;
200 } $self->items ];
201 }
202  
203 sub command_list
204 : Command_description("List showable window items")
205 : Command_arg('path?')
206 : Command_opt('all=+', desc => "list all the items")
207 {
208 0 ▶ my $self = shift;
Subroutine command_list: not called CC=10 SCAR=47.0
Pod undocumented
209 0 my ( $itempath, $opts, $cinv ) = @_;
210  
211 0 my @items;
212
213 0 ▶ if( $opts->{all} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'all'}) { } 0 0 0
214 0 @items = ( [ "/" => $self->{root} ] );
215 }
216 else {
217 0 @items = ( [ "" => $cinv->invocant ] );
218 }
219  
220 0 ▶ if( defined $itempath ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $itempath) 0 0 0
221 0 ▶ if( $itempath =~ m{^/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($itempath =~ m[^/]) { } 0 0 0
222 0 $items[0]->[0] = $itempath;
223 }
224 else {
225 0 $items[0]->[0] .= $itempath;
226 }
227 0 $items[0]->[1] = $self->_get_item( $itempath, $items[0]->[1] );
228 }
229  
230 0 ▶ $cinv->respond( "The following items exist" . ( defined $itempath ? " from path $itempath" : "" ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $itempath ? : 0 0 0
231  
232 # Walk a tree without using a recursive function
233 0 my @table;
234 0 ▶ while( my $i = pop @items ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = pop @items) 0 0 0
235 0 my ( $name, $item ) = @$i;
236  
237 0 ▶ push @table, [ " $name", ref($item) ] if length $name;
Branch: 1 branch
BranchTrueFalseTotal
if length $name 0 0 0
238  
239 0 ▶ if( my $subitems = $item->can( "enumerate_items" ) && $item->enumerate_items ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subitems = $item->can("enumerate_items") && $item->enumerate_items) 0 0 0
Condition0%0/3
$item->can('enumerate_items') && $item->enumerate_items
!ll&&!rl&&r
000
MC/DC0%0/2
$item->can('enumerate_items') && $item->enumerate_items
$item->can('enumerate_items') $item->enumerate_items
Truth table0%0/3
A && B
A= $item->can('enumerate_items')B= $item->enumerate_items
ABresult
0X0
100
111
240 0 push @items, [ _cat_path( $name, $_ ) => $subitems->{$_} ] for reverse sort keys %$subitems;
241 }
242 }
243  
244 0 $cinv->respond_table( \@table, colsep => " - " );
245  
246 0 return;
247 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
248  
249 sub command_show
250 : Command_description("Show a window item")
251 : Command_arg("path")
252 {
253 0 ▶ my $self = shift;
Subroutine command_show: not called CC=1 SCAR=6.9
Pod undocumented
254 0 my ( $itempath, $cinv ) = @_;
255  
256 0 my $item = $self->_get_item( $itempath, $cinv->invocant );
257  
258 0 $self->show_item( $item );
259  
260 0 return;
261 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
262  
263 sub command_hide
264 : Command_description("Hide a window item")
265 {
266 0 ▶ my $self = shift;
Subroutine command_hide: not called CC=2 SCAR=17.9
Pod undocumented
267 0 my ( $cinv ) = @_;
268  
269 0 my $item = $cinv->invocant;
270  
271 0 ▶ if( $item->isa( "Circle::RootObj" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->isa("Circle::RootObj")) 0 0 0
272 0 $cinv->responderr( "Cannot hide the global tab" );
273 0 return;
274 }
275  
276 0 $self->unshow_item( $item );
277  
278 0 return;
279 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
280  
281 sub command_tab
282 : Command_description("Manipulate window item tabs")
283 ▶{
Subroutine command_tab: not called CC=1 SCAR=0.0
Pod undocumented
284 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
285  
286 sub command_tab_move
287 : Command_description("Move the tab to elsewhere in the window ordering\n".
288 "POSITION may be an absolute number starting from 1,\n".
289 " a relative number with a leading + or -,\n".
290 " one of first | left | right | last")
291 : Command_subof('tab')
292 : Command_arg('position')
293 {
294 0 ▶ my $self = shift;
Subroutine command_tab_move: not called CC=12 SCAR=50.5
Pod undocumented
295 0 my ( $position, $cinv ) = @_;
296  
297 0 my $tabs = $self->get_prop_tabs;
298  
299 0 my $item = $cinv->invocant;
300  
301 0 my $index;
302 0 ▶ $tabs->[$_] eq $item and ( $index = $_, last ) for 0 .. $#$tabs;
Branch: 1 branch
BranchTrueFalseTotal
$tabs->[$_] eq $item and $index = $_, last 0 0 0
303  
304 0 ▶ defined $index or return $cinv->responderr( "Cannot find current index of item" );
Branch: 1 branch
BranchTrueFalseTotal
defined $index or return $cinv->responderr("Cannot find current index of item") 0 0 0
305  
306 0 ▶ $position = "+1" if $position eq "right";
Branch: 1 branch
BranchTrueFalseTotal
if $position eq "right" 0 0 0
307 0 ▶ $position = "-1" if $position eq "left";
Branch: 1 branch
BranchTrueFalseTotal
if $position eq "left" 0 0 0
308 0 ▶ $position = "1" if $position eq "first"; # 1-based
Branch: 1 branch
BranchTrueFalseTotal
if $position eq "first" 0 0 0
309 0 ▶ $position = @$tabs if $position eq "last"; # 1-based
Branch: 1 branch
BranchTrueFalseTotal
if $position eq "last" 0 0 0
310  
311 0 my $delta;
312 0 ▶ if( $position =~ m/^[+-]/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($position =~ /^[+-]/) { } 0 0 0
elsif ($position =~ /^\d+$/) { } 0 0 0
313 # relative
314 0 $delta = $position+0;
315 }
316 elsif( $position =~ m/^\d+$/ ) {
317 # absolute; but input from user was 1-based.
318 0 $position -= 1;
319 0 $delta = $position - $index;
320 }
321 else {
322 0 return $cinv->responderr( "Unrecognised position/movement specification: $position" );
323 }
324  
325 0 ▶ return $cinv->responderr( "Cannot move that far left" ) if $index + $delta < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index + $delta < 0 0 0 0
326 0 ▶ return $cinv->responderr( "Cannot move that far right" ) if $index + $delta > $#$tabs;
Branch: 1 branch
BranchTrueFalseTotal
if $index + $delta > $tabs->$#* 0 0 0
327  
328 0 $self->move_prop_tabs( $index, $delta );
329 0 return;
330 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
331  
332 sub command_tab_goto
333 : Command_description("Activate a numbered tab\n".
334 "POSITION may be an absolute number starting from 1,\n".
335 " a relative number with a leading + or -,\n".
336 " one of first | left | right | last")
337 : Command_subof('tab')
338 : Command_arg('position')
339 {
340 0 ▶ my $self = shift;
Subroutine command_tab_goto: not called CC=10 SCAR=47.0
Pod undocumented
341 0 my ( $position, $cinv ) = @_;
342  
343 0 my $tabs = $self->get_prop_tabs;
344  
345 0 my $item = $cinv->invocant;
346  
347 0 my $index;
348 0 ▶ $tabs->[$_] eq $item and ( $index = $_, last ) for 0 .. $#$tabs;
Branch: 1 branch
BranchTrueFalseTotal
$tabs->[$_] eq $item and $index = $_, last 0 0 0
349  
350 0 ▶ defined $index or return $cinv->responderr( "Cannot find current index of item" );
Branch: 1 branch
BranchTrueFalseTotal
defined $index or return $cinv->responderr("Cannot find current index of item") 0 0 0
351  
352 0 ▶ $position = "+1" if $position eq "right";
Branch: 1 branch
BranchTrueFalseTotal
if $position eq "right" 0 0 0
353 0 ▶ $position = "-1" if $position eq "left";
Branch: 1 branch
BranchTrueFalseTotal
if $position eq "left" 0 0 0
354 0 ▶ $position = "1" if $position eq "first"; # 1-based
Branch: 1 branch
BranchTrueFalseTotal
if $position eq "first" 0 0 0
355 0 ▶ $position = @$tabs if $position eq "last"; # 1-based
Branch: 1 branch
BranchTrueFalseTotal
if $position eq "last" 0 0 0
356  
357 0 ▶ if( $position =~ m/^[+-]/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($position =~ /^[+-]/) { } 0 0 0
elsif ($position =~ /^\d+$/) { } 0 0 0
358 # relative
359 0 $index += $position;
360 }
361 elsif( $position =~ m/^\d+$/ ) {
362 # absolute; but input from user was 1-based.
363 0 $index = $position - 1;
364 }
365 else {
366 0 return $cinv->responderr( "Unrecognised position/movement specification: $position" );
367 }
368  
369 0 $self->get_prop_tabs->[$index]->fire_event( raise => () );
370 0 return;
371 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
372  
373 1 0x55AA;
« blib/lib/Circle/Net/IRC/Channel.pm ↑ Summary blib/lib/Circle/Commandable.pm »