File Coverage

blib/lib/Bot/Cobalt/Frontend/Utils.pm
Criterion Covered Total %
statement 49 58 84.4
branch 13 24 54.1
condition 8 21 38.1
subroutine 8 8 100.0
pod 2 2 100.0
total 80 113 70.8


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Frontend::Utils;
2             $Bot::Cobalt::Frontend::Utils::VERSION = '0.021002';
3 1     1   13374 use v5.10;
  1         2  
4 1     1   403 use strictures 2;
  1         1138  
  1         29  
5              
6 1     1   138 use Carp;
  1         1  
  1         66  
7              
8 1     1   374 use parent 'Exporter::Tiny';
  1         249  
  1         4  
9              
10             our @EXPORT_OK = qw/
11             ask_yesno
12             ask_question
13             /;
14             our %EXPORT_TAGS;
15             { my %s; push @{$EXPORT_TAGS{all}}, grep {!$s{$_}++} @EXPORT_OK }
16              
17              
18             sub ask_question {
19 1     1 1 1320 my %args = @_;
20              
21 1   33     6 my $question = delete $args{prompt} || croak "No prompt => specified";
22 1         3 my $default = delete $args{default};
23              
24 1         2 my $validate_sub;
25 1 50       4 if (defined $args{validate}) {
26             $validate_sub = ref $args{validate} eq 'CODE' ?
27             delete $args{validate}
28 1 50       4 : croak "validate => should be a coderef";
29             }
30              
31 1         2 select(STDOUT); $|++;
  1         2  
32              
33             my $print_and_grab = sub {
34 1 50   1   33 print "$question ", defined $default ? "[$default] " : "> ";
35 1         16 my $ret = ; chomp($ret);
  1         2  
36 1 50 33     4 $ret = $default if defined $default and $ret eq '';
37 1         2 $ret
38 1         4 };
39              
40 1         2 my $input = $print_and_grab->();
41 1         3 until (length $input) {
42 0         0 print "No input specified.\n";
43 0         0 $input = $print_and_grab->()
44             }
45              
46             VALID: {
47 1 50       1 if ($validate_sub) {
  1         3  
48 1         3 my $invalid = $validate_sub->($input);
49 1 50       8 last VALID unless defined $invalid;
50             die "Invalid input; $invalid\n"
51 0 0 0     0 if $args{die_if_invalid} or $args{die_unless_valid};
52 0         0 until (not defined $invalid) {
53 0         0 print "Invalid input; $invalid\n";
54 0         0 $print_and_grab->();
55             redo VALID
56 0         0 }
57             }
58             }
59              
60 1         6 return $input
61             }
62              
63             sub ask_yesno {
64 4     4 1 1520 my %args = @_;
65 4   33     11 my $question = $args{prompt} || croak "No prompt => specified";
66              
67             my $default = lc(
68 4   33     18 substr($args{default}||'', 0, 1) || croak "No default => specified"
69             );
70              
71 4 50       12 croak "default should be Y or N"
72             unless $default =~ /^[yn]$/;
73              
74 4 100       11 my $yn = $default eq 'y' ? 'Y/n' : 'y/N' ;
75              
76 4         6 select(STDOUT); $|++;
  4         9  
77              
78 4         2 my $input;
79             my $print_and_grab = sub {
80 4     4   126 print "$question [$yn] ";
81 4         12 $input = ; chomp($input);
  4         4  
82 4 50       10 $input = $default if $input eq '';
83 4 50       10 lc substr $input eq '' ? $default : $input, 0, 1
84 4         14 };
85 4         6 $print_and_grab->();
86 4   66     24 until ($input && $input eq 'y' || $input eq 'n') {
      66        
87 0         0 print "Invalid input; should be either Y or N\n";
88 0         0 $print_and_grab->();
89             }
90              
91 4 100       29 $input eq 'y' ? 1 : 0
92             }
93              
94             1;
95             __END__