Circle/Session/Tabbed.pm

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