Dialog/Screen/Menu.pm

filter: Statementstmt 34.2% 26 / 76 Branchbran 24.0% 12 / 50 Conditioncond 4.7% 1 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 50.0% 8 / 16 Podpod 100% 9 / 9 totaltotal 30.4% 56 / 184 CC 34 SCAR 36.5
CC
34
Cov
27%
run
110.0
set_menu_item
42.0
new
27.7
CRAP 38.4
« Dialog/Backend/Nautilus.pm ↑ Summary
1 package UI::Dialog::Screen::Menu;
2 ###############################################################################
3 # Copyright (C) 2004-2016 Kevin C. Krinke <kevin@krinke.ca>
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ###############################################################################
19 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant { TRUE => 1, FALSE => 0 };
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 BEGIN {
25 1 ▶ use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶ $VERSION = '1.20';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 }
28  
29 1 ▶use UI::Dialog;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Example Usage
32 #
33 # my $screen = new UI::Dialog::Screen::Menu ( dialog => $d );
34 # $screen->add_menu_item( "Label", \&func );
35 # $screen->loop();
36 #
37  
38 sub new {
39 1 ▶ my ($class, %args) = @_;
Subroutine new: called CC=14 SCAR=33.2
Pod documented
40 1 $args{__loop_active} = FALSE;
41 1 ▶ unless (exists $args{dialog}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"dialog"}) 1 0 1
42 $args{dialog} = new UI::Dialog
43 (
44 title => (defined $args{title}) ? $args{title} : '',
45 backtitle => (defined $args{backtitle}) ? $args{backtitle} : '',
46 height => (defined $args{height}) ? $args{height} : 20,
47 width => (defined $args{width}) ? $args{width} : 65,
48 listheight => (defined $args{listheight}) ? $args{listheight} : 5,
49 order => (defined $args{order}) ? $args{order} : undef,
50 PATH => (defined $args{PATH}) ? $args{PATH} : undef,
51 beepbefore => (defined $args{beepbefore}) ? $args{beepbefore} : undef,
52 beepafter => (defined $args{beepafter}) ? $args{beepafter} : undef,
53 1 ▶ 'trust-input' => (defined $args{'trust-input'} && $args{'trust-input'} == 1) ? 1 : 0,
Branch: 10 branches
BranchTrueFalseTotal
defined $args{'title'} ? : 0 1 1
defined $args{'backtitle'} ? : 0 1 1
defined $args{'height'} ? : 0 1 1
defined $args{'width'} ? : 0 1 1
defined $args{'listheight'} ? : 0 1 1
defined $args{'order'} ? : 0 1 1
defined $args{'PATH'} ? : 0 1 1
defined $args{'beepbefore'} ? : 0 1 1
defined $args{'beepafter'} ? : 0 1 1
defined $args{'trust-input'} && $args{'trust-input'} == 1 ? : 0 1 1
Condition33%1/3
defined $args{"trust-input"} and $args{"trust-input"} == 1
!ll&&!rl&&r
100
MC/DC0%0/2
defined $args{"trust-input"} and $args{"trust-input"} == 1
defined $args{"trust-input"} $args{"trust-input"} == 1
Truth table33%1/3
A and B
A= defined $args{"trust-input"}B= $args{"trust-input"} == 1
ABresult
0X0
100
111
54 );
55 }
56 1 ▶ unless (exists $args{menu}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"menu"}) 1 0 1
57 1 $args{menu} = [];
58 }
59 1 return bless { %args }, $class;
60 }
61  
62 #: $screen->add_menu_item( "Label", \&func );
63 #: Add an item to the menu with a label and a callback func
64 #
65 sub add_menu_item {
66 0 ▶ my ($self,$label,$func) = @_;
Subroutine add_menu_item: not called CC=1 SCAR=6.9
Pod documented
67 0 push(@{$self->{menu}},{label=>$label,func=>$func});
68 0 return @{$self->{menu}} - 1;
69 }
70  
71 #: @list_of_menu_items = $screen->get_menu_items();
72 #: Return a list of all the menu items in order. Each item is a hash
73 #: with a label and a func reference.
74 #
75 sub get_menu_items {
76 0 ▶ my ($self) = @_;
Subroutine get_menu_items: not called CC=1 SCAR=6.9
Pod documented
77 0 return @{$self->{menu}};
78 }
79  
80 #: %item = $screen->del_menu_item( $index );
81 #: Remove a menu item and return it. The item will no longer show in the
82 #: list of avaliable menu items.
83 #
84 sub del_menu_item {
85 0 ▶ my ($self,$index) = @_;
Subroutine del_menu_item: not called CC=4 SCAR=30.0
Pod documented
86 0 ▶ if (defined $index && $index >= 0 && $index < @{$self->{menu}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index and $index >= 0 and $index < @{$self->{"menu"};}) 0 0 0
Condition0%0/6
defined $index and $index >= 0 and $index < @{$self->{"menu"};}
!ll&&!rl&&r
000
defined $index and $index >= 0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $index and $index >= 0 and $index < @{$self->{"menu"};}
defined $index $index >= 0 $index < @{$self->{"menu"};}
Truth table0%0/4
A and B and C
A= defined $indexB= $index >= 0C= $index < @{$self->{"menu"};}
ABCresult
0XX0
10X0
1100
1111
87 0 return splice(@{$self->{menu}}, $index, 1);
88 }
89 0 return undef;
90 }
91  
92 #: $screen->set_menu_item( $index, $label||undef, $func||undef );
93 #: Update a menu item's properties. If a field is "undef", no action
94 #: is performed on that item's field. Returns the menu_item before
95 #: modification.
96 #: Note: $index starts from 0.
97 #
98 sub set_menu_item {
99 0 ▶ my ($self,$index,$label,$func) = @_;
Subroutine set_menu_item: not called CC=6 SCAR=37.4
Pod documented
100 0 ▶ if (defined $index && $index >= 0 && $index < @{$self->{menu}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index and $index >= 0 and $index < @{$self->{"menu"};}) 0 0 0
Condition0%0/6
defined $index and $index >= 0 and $index < @{$self->{"menu"};}
!ll&&!rl&&r
000
defined $index and $index >= 0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $index and $index >= 0 and $index < @{$self->{"menu"};}
defined $index $index >= 0 $index < @{$self->{"menu"};}
Truth table0%0/4
A and B and C
A= defined $indexB= $index >= 0C= $index < @{$self->{"menu"};}
ABCresult
0XX0
10X0
1100
1111
101 0 my $item = $self->{menu}->[$index];
102 0 my $orig = { label => $item->{label}, func => $item->{func} };
103 0 ▶ $self->{menu}->[$index]->{label} = $label if defined $label;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label 0 0 0
104 0 ▶ $self->{menu}->[$index]->{func} = $func if defined $func;
Branch: 1 branch
BranchTrueFalseTotal
if defined $func 0 0 0
105 0 return $orig;
106 }
107 0 return undef;
108 }
109  
110  
111 #: $screen->run();
112 #: Blocking call, display the menu and react once. Returns 0 if cancelled,
113 #: returns 1 if an item was selected and the function called.
114 #
115 sub run {
116 0 ▶ my ($self) = @_;
Subroutine run: not called CC=10 SCAR=47.0
Pod documented
117 0 my @menu_list = ();
118 0 my $c = 1;
119 0 foreach my $data (@{$self->{menu}}) {
120 0 push(@menu_list,$c,$data->{label});
121 0 $c++;
122 }
123 my $sel = $self->{dialog}->menu
124 (
125 title => (defined $self->{title}) ? $self->{title} : '',
126 0 ▶ text => (defined $self->{text}) ? $self->{text} : '',
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'title'} ? : 0 0 0
defined $self->{'text'} ? : 0 0 0
127 list => \@menu_list
128 );
129 0 ▶ if ($self->{dialog}->state() eq "OK") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dialog'}->state eq 'OK') { } 0 0 0
130 0 my $data = $self->{menu}->[$sel-1];
131 0 my $func = $data->{func};
132 0 ▶ &{$func}($self,$self->{dialog},$sel-1) if defined $func and ref($func) eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if defined $func and ref $func eq "CODE" 0 0 0
Condition0%0/3
defined $func and ref $func eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $func and ref $func eq "CODE"
defined $func ref $func eq "CODE"
Truth table0%0/3
A and B
A= defined $funcB= ref $func eq "CODE"
ABresult
0X0
100
111
133 0 return 1;
134 } else {
135 0 ▶ if (exists $self->{cancel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cancel"}) 0 0 0
136 0 my $func = $self->{cancel};
137 0 ▶ &{$func}($self,$self->{dialog},-1) if defined $func and ref($func) eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if defined $func and ref $func eq "CODE" 0 0 0
Condition0%0/3
defined $func and ref $func eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $func and ref $func eq "CODE"
defined $func ref $func eq "CODE"
Truth table0%0/3
A and B
A= defined $funcB= ref $func eq "CODE"
ABresult
0X0
100
111
138 }
139 }
140 0 return 0;
141 }
142  
143 #: $screen->loop();
144 #: Blocking call, execute $screen->run() indefinitely. If run() was cancelled,
145 #: the loop will break.
146 sub loop {
147 0 ▶ my ($self) = @_;
Subroutine loop: not called CC=3 SCAR=24.8
Pod documented
148 0 $self->{__loop_active} = TRUE;
149 0 ▶ while ($self->{__loop_active}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__loop_active"}) 0 0 0
150 0 ▶ last unless $self->run();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->run 0 0 0
151 }
152 }
153  
154 #: $screen->break_loop();
155 #: Notify loop() to break instead of re-iterate regardless of user input.
156 #
157 sub break_loop {
158 0 ▶ my ($self) = @_;
Subroutine break_loop: not called CC=1 SCAR=6.9
Pod documented
159 0 $self->{__loop_active} = FALSE;
160 }
161  
162 #: $screen->is_looping();
163 #: Returns TRUE if currently looping, FALSE otherwise
164 #
165 sub is_looping {
166 0 ▶ my ($self) = @_;
Subroutine is_looping: not called CC=2 SCAR=17.9
Pod documented
167 0 ▶ return ($self->{__loop_active}) ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'__loop_active'} ? : 0 0 0
168 }
169  
170 1 1; # END OF UI::Dialog::Screen::Menu
« Dialog/Backend/Nautilus.pm ↑ Summary