File Coverage

blib/lib/Mojo/Console.pm
Criterion Covered Total %
statement 12 58 20.6
branch 0 12 0.0
condition 0 25 0.0
subroutine 4 18 22.2
pod 9 9 100.0
total 25 122 20.4


line stmt bran cond sub pod time code
1             package Mojo::Console;
2 1     1   53334 use Mojo::Base 'Mojolicious::Command';
  1         151637  
  1         6  
3              
4 1     1   39486 use List::Util qw(any none);
  1         2  
  1         52  
5              
6 1     1   351 use Mojo::Console::Input;
  1         3  
  1         7  
7 1     1   340 use Mojo::Console::Output;
  1         2  
  1         6  
8              
9             our $VERSION = '0.0.3';
10              
11             has 'input' => sub { Mojo::Console::Input->new };
12             has 'max_attempts' => 10;
13             has 'output' => sub { Mojo::Console::Output->new };
14              
15             sub ask {
16 0     0 1   my ($self, $message, $required) = @_;
17              
18 0           my $attempts = $self->max_attempts;
19 0           my $answer = '';
20              
21 0   0       while ((($required || $attempts == 10) && !$answer) && $attempts--) {
      0        
      0        
22 0           $self->line($message . ' ');
23 0           $answer = $self->input->ask;
24             }
25              
26 0 0         if ($attempts < 0) {
27 0           $self->error("Please answer the question.\n");
28             }
29              
30 0           return $answer;
31             }
32              
33             sub confirm {
34 0     0 1   my ($self, $message, $default) = @_;
35              
36 0   0 0     my $default_yes = (any { lc($default || '') eq $_ } qw/y yes/);
  0            
37 0   0 0     my $default_no = (any { lc($default || '') eq $_ } qw/n no/);
  0            
38              
39 0           my $attempts = $self->max_attempts;
40 0           my $answer = '';
41              
42 0   0 0     while ((none { lc($answer) eq $_ } qw/y yes n no/) && $attempts--) {
  0            
43 0           $self->line($message);
44              
45 0           $self->success(' [yes/no] ');
46              
47 0 0         if ($default) {
48 0           $self->warn(sprintf('[default=%s] ', $default));
49             }
50              
51 0   0       $answer = $self->input->ask || $default;
52             }
53              
54 0 0         if ($attempts < 0) {
55 0           $self->error("Please answer with [yes/no]\n");
56             }
57              
58 0 0   0     return (any { lc($answer) eq $_ } qw/y yes/) ? 1 : 0;
  0            
59             }
60              
61             sub choice {
62 0     0 1   my ($self, $message, $choices, $default) = @_;
63              
64 0           my $attempts = $self->max_attempts;
65 0           my $answer = '';
66              
67 0   0 0     while ((none { $answer eq $_ } @$choices) && $attempts--) {
  0            
68 0           $self->line($message);
69 0           $self->success(sprintf(' [%s] ', join(', ', @$choices)));
70              
71 0 0         if ($default) {
72 0           $self->warn(sprintf('[default=%s] ', $default));
73             }
74              
75 0   0       $answer = $self->input->ask || $default;
76             }
77              
78 0 0         if ($attempts < 0) {
79 0           $self->error(sprintf("Please chose one of the following options: [%s] \n", join(', ', @$choices)));
80             }
81              
82 0           return $answer;
83             }
84              
85             sub error {
86 0     0 1   return shift->output->error(@_);
87             }
88              
89             sub info {
90 0     0 1   return shift->output->info(@_);
91             }
92              
93             sub line {
94 0     0 1   return shift->output->line(@_);
95             }
96              
97             sub newline {
98 0     0 1   return shift->output->newline(@_);
99             }
100              
101             sub success {
102 0     0 1   return shift->output->success(@_);
103             }
104              
105             sub warn {
106 0     0 1   return shift->output->warn(@_);
107             }
108              
109             1;
110              
111             =encoding utf8
112              
113             =head1 NAME
114              
115             Mojo::Console - Extend Mojolicious::Command to be able to ask for things from command line
116              
117             =head1 SYNOPSIS
118              
119             package MyApp::Command::helloworld;
120             use Mojo::Base 'Mojo::Console';
121              
122             sub run {
123             my $self = shift;
124              
125             my $name = $self->ask('What is your name?');
126             my $gender = $self->choice('Are you a male or a female?', ['male', 'female']);
127             my $bool = $self->confirm("Do you have a cat?");
128              
129             $self->line("Hi $name\n");
130             $self->line("We found out that you are a $gender ");
131              
132             if ($bool) {
133             $self->line("and you have a cat");
134             } else {
135             $self->line("and you don't have a cat");
136             }
137              
138             if ($self->confirm("Would you like an icecream?")) {
139             $self->success("Thanks");
140             } else {
141             $self->error("Oh no!");
142             }
143              
144             $self->info("You got here because you took an icecream");
145             }
146              
147             1;
148              
149             =head1 DESCRIPTION
150              
151             L is an extension of L
152              
153             =head1 ATTRIBUTES
154              
155             L inherits all attributes from L.
156              
157             =head1 METHODS
158              
159             L inherits all methods from L and implements
160             the following new ones.
161              
162             =head2 ask
163              
164             my $answer = $self->ask('What is your name?');
165             my $required_answer = $self->ask('What is your name?', 1); # this will ask for an answer maximum 10 times and will exit in case the answer is empty
166              
167             =head2 confirm
168              
169             my $bool = $self->confirm("Are you sure?");
170             my $bool_with_default_answer = $self->confirm("Are you sure?", 'yes');
171              
172             =head2 choice
173              
174             my $choice = $self->choice('Are you a male or a female?', ['male', 'female']);
175             my $choice_with_default_answer = $self->choice('Are you a male or a female?', ['male', 'female'], 'male');
176              
177             =head2 error
178              
179             $self->error("The program will stop here");
180              
181             =head2 info
182              
183             $self->info("This is just an info message");
184              
185             =head2 line
186              
187             $self->line("This message will not have a new line at the end");
188              
189             =head2 newline
190              
191             $self->line("This message will have a new line at the end");
192              
193             =head2 success
194              
195             $self->success("This is just a success message");
196              
197             =head2 warn
198              
199             $self->success("This is just a warning message");
200              
201             =head1 SEE ALSO
202              
203             L, L, L, L.
204              
205             =cut