File Coverage

blib/lib/Term/Menu.pm
Criterion Covered Total %
statement 37 96 38.5
branch 1 28 3.5
condition 0 17 0.0
subroutine 5 10 50.0
pod 0 6 0.0
total 43 157 27.3


line stmt bran cond sub pod time code
1             package Term::Menu;
2              
3 2     2   85949 use 5.000;
  2         8  
  2         108  
4 2     2   12 use strict;
  2         3  
  2         85  
5 2     2   23 use warnings;
  2         4  
  2         113  
6 2     2   11 use Carp;
  2         3  
  2         9179  
7              
8             our $VERSION = '0.10';
9              
10             sub new {
11 0     0 0 0 my $invocant = shift;
12 0   0     0 my $class = ref($invocant) || $invocant;
13 0         0 my $self = {
14             delim => ") ",
15             spaces => 7,
16             beforetext => "Please choose one of the following options.",
17             aftertext => "Please enter a letter or number corresponding to the option you want to choose: ",
18             nooptiontext => "That's not one of the available options.",
19             moreoptions => " or ",
20             tries => 0,
21             toomanytries => "You've tried too many times.",
22             hidekeys => 0,
23             @_,
24             lastval => undef,
25             tried => 0,
26             };
27            
28 0         0 bless $self, $class;
29 0         0 return $self;
30             }
31              
32             sub setcfg {
33 0     0 0 0 my $self = shift;
34 0 0       0 croak("Error: setcfg is an instance method!") if(!ref $self);
35 0         0 %$self = (%$self, @_);
36             }
37              
38             sub menu {
39 0     0 0 0 my $self = shift;
40              
41             # Create a default self if we didn't get one
42 0 0 0     0 $self = Term::Menu->new() if(!defined($self) or !ref($self));
43              
44             # Options by name
45 0         0 my %options = @_;
46              
47             # Options in order
48 0         0 my $i = 0;
49 0         0 my @options = grep { ++$i % 2 } @_;
  0         0  
50            
51 0         0 my $delim = $self->{delim}; # The delimiter between keys and label
52            
53 0         0 my @lines; # The lines of the options that need to be printed
54             my %keyvals; # A hash that holds what keys should return what values.
55 0         0 my $maxoptlen = 0; # Max length of keys that correspond to this value.
56            
57 0         0 foreach(@options) {
58 0         0 my $value = $_;
59 0         0 my @keys = @{$options{$_}};
  0         0  
60 0         0 my $label = shift @keys;
61 0         0 my $options = join ($self->{moreoptions}, @keys);
62 0         0 $keyvals{$_} = $value foreach(@keys);
63 0 0       0 push @lines, [($self->{hidekeys} ? "" : $options.$delim).$label."\n", length($options)];
64             #Length of options included to get the
65             #number of spaces that need to be included.
66 0 0 0     0 $maxoptlen = length($options) if(length($options) > $maxoptlen and !$self->{hidekeys});
67             }
68 0         0 my $spaces = $self->{spaces};
69 0 0       0 $spaces = $maxoptlen if($maxoptlen > $spaces);
70 0 0       0 print $self->{beforetext},"\n" if defined $self->{beforetext};
71 0         0 foreach (@lines) {
72 0         0 my ($line, $len) = @$_;
73 0         0 my $nspace = $spaces - $len;
74 0         0 print " " x $nspace, $line;
75             }
76 0         0 while(1) {
77 0 0       0 print $self->{aftertext} if defined $self->{aftertext};
78 0         0 my $answ = ;
79 0         0 chomp $answ;
80 0         0 foreach(keys %keyvals) {
81 0 0       0 if($answ eq $_) {
82 0         0 $self->{lastval} = $keyvals{$_};
83 0         0 return $keyvals{$_};
84             }
85             }
86 0 0       0 print $self->{nooptiontext},"\n" if defined $self->{nooptiontext};
87 0   0     0 $self->{tried} ||= 0;
88 0         0 $self->{tried}++;
89 0 0 0     0 if($self->{tried} >= $self->{tries} and $self->{tries} != 0) {
90 0         0 last;
91             }
92             }
93 0 0 0     0 if($self->{tried} >= $self->{tries} and $self->{tries} != 0) {
94 0 0       0 print $self->{toomanytries},"\n" if defined $self->{toomanytries};
95 0         0 $self->{tried} = 0;
96 0         0 $self->{lastval} = undef;
97 0         0 return undef;
98             }
99             }
100              
101             sub question {
102 0     0 0 0 my ($self, $question) = @_;
103 0         0 print $question;
104 0         0 my $answer = ;
105 0         0 $self->{lastval} = $answer;
106 0         0 return $answer;
107             }
108              
109             sub lastval {
110 0     0 0 0 my $self = shift;
111 0 0       0 croak("Error: lastval is an instance method") if(!ref($self));
112 0         0 return $self->{lastval};
113             }
114              
115             sub table {
116 1     1 0 408 my ($pkg, $heads, $contents) = @_;
117              
118             # We first get a list of columns and their sizes.
119             # We only print as many columns as there are things in $heads.
120 1         2 my $column_sizes = [];
121 1         6 for(my $i = 0; $i < @$heads; ++$i) {
122             # Max size of this column...
123 2         3 my $size = length($heads->[$i]);
124 2         3 foreach my $row (@$contents) {
125 4 50       13 $size = length($row->[$i])
126             if(length($row->[$i]) > $size);
127             }
128 2         8 $column_sizes->[$i] = $size;
129             }
130              
131             # Now we start printing.
132             # Line 1, 3 and n: delimiters
133 1         1 my $delimline = "+";
134 1         3 foreach(@$column_sizes) {
135 2         6 $delimline .= ("-" x $_) . "+";
136             }
137 1         2 $delimline .= "\n";
138 1         6 print $delimline;
139              
140             # Line 2: column names
141 1         3 my $nameline = "|";
142 1         6 for(my $i = 0; $i < @$heads; ++$i) {
143             # Name of the head, plus as many spaces as needed.
144 2         8 $nameline .= $heads->[$i] . (" " x ($column_sizes->[$i] - length($heads->[$i]))) . "|";
145             }
146 1         2 $nameline .= "\n";
147 1         1 print $nameline;
148              
149 1         2 print $delimline;
150              
151 1         3 foreach my $content (@$contents) {
152 2         3 my $contentline = "|";
153 2         5 for(my $i = 0; $i < @$heads; ++$i) {
154 4         12 $contentline .= $content->[$i] . (" " x ($column_sizes->[$i] - length($content->[$i]))) . "|";
155             }
156 2         3 $contentline .= "\n";
157 2         5 print $contentline;
158             }
159 1         4 print $delimline;
160             }
161              
162             1;
163             __END__