File Coverage

blib/lib/App/Rad/Plugin/ReadLine.pm
Criterion Covered Total %
statement 16 75 21.3
branch 0 34 0.0
condition 0 14 0.0
subroutine 6 15 40.0
pod 2 2 100.0
total 24 140 17.1


line stmt bran cond sub pod time code
1             package App::Rad::Plugin::ReadLine;
2             BEGIN {
3 1     1   1051 $App::Rad::Plugin::ReadLine::VERSION = '0.003'; # TRIAL
4             }
5              
6             # ABSTRACT: App::Rad::Plugin::ReadLine a Term::UI ->shell for Rad Apps
7              
8              
9 1     1   8 use warnings;
  1         3  
  1         48  
10 1     1   5 use strict;
  1         2  
  1         140  
11              
12             our $GreetWithCommand = 'help'; # when the shell starts, run this command
13             our $GreetWithSub ; # you can pass in a sub too, both GreetWith's are run
14             our $DefaultCommand = '' ;
15             our $ShellPrompt = "[" . $0 . "]";
16              
17             # we keep prompting until this is false
18             # (we register "exit" as a command to set it to 0 upon entering shell)
19             our $still_going = 1;
20              
21              
22 1     1   978 use Term::UI;
  1         52405  
  1         35  
23 1     1   12 use Term::ReadLine;
  1         2  
  1         578  
24              
25              
26             our $term;
27             sub _terminal {
28 0   0 0     $term ||= Term::ReadLine->new($ENV{TERM} || 'critter');
      0        
29             }
30              
31             sub _shell_prompt {
32             # add a space to the end
33 0 0   0     $ShellPrompt =~ / $/? $ShellPrompt : "$ShellPrompt "
34             }
35 0     0     sub _shell_help { "run $0 in interactive mode" };
36 0     0     sub _shell_command { 'shell' }
37              
38              
39             sub shell_options {
40 0     0 1   my $c = shift;
41              
42 0 0         if (@_==0) {
43 0           $c->register( _shell_command() => \&shell, _shell_help() );
44 0           return;
45             }
46              
47 0           my ($opts, $name, $help);
48 0           my $no_register;
49              
50 0 0         if ('HASH' eq ref $_[0]) {
51 0           ($opts, $name, $help) = @_;
52 0 0         $GreetWithCommand = $opts->{GreetWithCommand}
53             if exists $opts->{GreetWithCommand};
54 0 0         $GreetWithSub = $opts->{GreetWithSub}
55             if exists $opts->{GreetWithSub};
56 0 0         $DefaultCommand = $opts->{DefaultCommand}
57             if exists $opts->{DefaultCommand};
58 0 0         $ShellPrompt = $opts->{ShellPrompt}
59             if exists $opts->{ShellPrompt};
60              
61 0 0         $c->debug(sprintf'shell_options '
62             . '$GreetWithCommand = %s, '
63             . '$GreetWithSub = %s, '
64             . '$ShellPrompt = %s, '
65             . '$DefaultCommand = %s. also '
66             . '$name = %s, $help = %s',
67             map defined $_ ? $_ : '(not defined)',
68             $GreetWithCommand,
69             $GreetWithSub,
70             $ShellPrompt,
71             $DefaultCommand,
72             $name, $help
73             );
74              
75 0 0 0       $no_register = 1
76             if @_ >= 2 and not defined $name;
77             }
78             else {
79 0           ($name, $help) = @_;
80 0 0 0       $no_register = 1
81             if @_ >= 1 and not defined $name;
82             }
83              
84 0 0         if ($no_register) {
85 0           $c->debug("not registering shell as a command since \$name was passed as undef");
86             }
87             else {
88 0 0         $name = _shell_command() if not defined $name;
89 0 0         $help = _shell_help() if not defined $help;
90            
91              
92 0           $c->debug("registering shell as '$name' => '$help'");
93 0           $c->register( $name => \&shell, $help );
94             }
95             }
96              
97              
98             # this is printed when your shell first starts, it will optionally run a command,
99             # then optionally run a sub
100             # again, you can do both, but that's likely to be unhelpful
101             sub _welcome {
102 0     0     my $c = shift;
103             # $c->critters();
104 0           $c->stash->{shelllvl} ++;
105              
106 0 0         do {
107 0           local $c->{'cmd'} = $GreetWithCommand;
108 0           $c->execute();
109             } if defined $GreetWithCommand;
110              
111 0 0         $c->$GreetWithSub() if defined $GreetWithSub;
112              
113             }
114              
115             sub shell {
116 0     0 1   my $c = shift;
117             # $c->critters();
118 0           $c->stash->{shelllvl} ++;
119 1     1   6 {no warnings qw[ redefine ];
  1         1  
  1         293  
  0            
120             *App::Rad::Help::usage = sub {
121 0     0     "Your app as a shell. Type commands with arguments"
122 0           };}
123              
124            
125             # sub-shells
126 0           local $GreetWithCommand = $GreetWithCommand ;
127 0           local $GreetWithSub = $GreetWithSub ;
128 0           local $DefaultCommand = $DefaultCommand ;
129              
130 0           local $ShellPrompt = $ShellPrompt ;
131 0           local $still_going = $still_going ;
132              
133 0 0         $c->shell_options(@_) if @_;
134              
135             $c->register( 'exit', sub {
136 0     0     $App::Rad::Plugin::ReadLine::still_going = 0;
137 0           }, "exit the $0 shell" );
138              
139 0           $c->unregister_command('shell');# Xhibit forbidden
140              
141 0           my $welcome = \&_welcome;
142 0           $c->$welcome();
143              
144 0           while($still_going) {
145 0           (my $cmd, local @ARGV) = split ' ',
146             _terminal->get_reply(
147             prompt => _shell_prompt(),
148             default => $DefaultCommand,
149             );
150 0 0 0       if (defined $cmd and $cmd ne '') {
151 0           @{$c->argv} = @ARGV;
  0            
152 0           $c->{'cmd'} = $cmd;
153              
154 0           $c->debug('received command: ' . $c->{'cmd'});
155 0           $c->debug('received parameters: ' . join (' ', @{$c->argv} ));
  0            
156             }
157 0           $c->_tinygetopt();
158             # run the specified command
159             # setup/pre_process are run for us...
160 0           $c->execute();
161             # teardown is run after this (again, magically)
162             }
163             }
164              
165              
166             "Say my name"
167              
168             __END__