lib/App/ConMenu.pm

filter: Statementstmt 56.6% 34 / 60 Branchbran 16.6% 1 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 6 totaltotal 49.4% 44 / 89 CC 6 SCAR 17.6
CC
6
Cov
51%
waitForInput
20.0
loadMenuFile
2.0
execute
2.0
CRAP 5.8
↑ Summary
1 package App::ConMenu;
2 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Term::ANSIScreen qw(cls);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our $VERSION = "1.00";
9  
10  
11 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 1 my $type = shift;
13 1 my $self = {};
14 1 return bless $self, $type;
15 }
16  
17 # load the yaml file you should have
18 # set filename by now.
19 1 ▶sub loadMenuFile {
Subroutine loadMenuFile: called CC=2 SCAR=7.0
Pod undocumented
20 1 my $self = shift;
21 1 ▶ $self->{fileName} or croak ("No yaml file Name set");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fileName"} or croak("No yaml file Name set") 1 0 1
22 1 my $yaml;
23 1 $yaml = YAML::Tiny -> read($self->{fileName});
24 1 $self->{'menu'}= $yaml;
25 1 return $yaml;
26 }
27  
28 1 ▶sub execute {
Subroutine execute: called CC=2 SCAR=6.9
Pod undocumented
29 1 my $self = shift;
30 1 my $commandStructure = shift;
31 1 my $commands = $commandStructure->{'commands'};
32 1 foreach my $command (@$commands)
33 {
34 1 print `$command`;
35 }
36 1 return 1; # return 1 so that testing knows we got this far.
37 }
38  
39 0 ▶sub printMenu {
Subroutine printMenu: not called CC=1 SCAR=6.9
Pod undocumented
40 0 my $self = shift;
41 0 my $menuItemsUnsorted = $self->{'menu'}->[0];
42 0 my @menuItems = sort { {$a} cmp {$b} } keys(%$menuItemsUnsorted);
43 0 $self->{menuItems} = \@menuItems;
44 0 cls();
45 0 my $i=1;
46 0 my @menuItemsNumerical = map { '['. $i++.'] '.$_ } @menuItems;
47 0 say join("\n", @menuItemsNumerical);
48 0 say 'Choose a menu item by pressing the corresponding number';
49 0 say 'q to exit';
50 }
51  
52 0 ▶sub waitForInput {
Subroutine waitForInput: not called CC=4 SCAR=30.0
Pod undocumented
53 0 my $self = shift;
54 0 my $selection = <>;
55 0 ▶ if ($selection =~ /[0-9]+/){
Branch: 1 branch
BranchTrueFalseTotal
if ($selection =~ /[0-9]+/) { } 0 0 0
56 0 ▶ if ($selection > scalar ($self->{menuItems}) or $selection < 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($selection > scalar $self->{"menuItems"} or $selection < 1) 0 0 0
Condition0%0/3
$selection > scalar $self->{"menuItems"} or $selection < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$selection > scalar $self->{"menuItems"} or $selection < 1
$selection > scalar $self->{"menuItems"} $selection < 1
Truth table0%0/3
A or B
A= $selection > scalar $self->{"menuItems"}B= $selection < 1
ABresult
1X1
011
000
57 0 say 'Error no such menu item';
58 0 exit;
59 }
60 } else {
61 0 exit;
62 }
63 0 my $menuItems = $self->{menuItems};
64 0 $self->execute($self->{menu}->[0]->{$menuItems->[$selection -1]})
65 }
66  
67 # create a default file to get people going.
68 0 ▶sub createDefaultFile{
Subroutine createDefaultFile: not called CC=1 SCAR=6.9
Pod undocumented
69 0 my $self = shift;
70 0 my $fileName = shift;
71 0 my $menu = {
72 'Menu option 1' => {
73 'commands' => [
74 'ls'
75 ],
76 'working_dir' => './'
77 },
78 'Menu Option 2' => {
79 'commands' => [
80 'dir'
81 ],
82 'working_dir' => './'
83 }
84  
85 };
86 0 my $yaml = YAML::Tiny->new($menu);
87 0 $yaml->write($fileName);
88  
89 }
90  
91  
92 1 1;
93 __END__
↑ Summary