ReadLine.pm

filter: Statementstmt 28.0% 23 / 82 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 40.0% 6 / 15 Podpod 100% 2 / 2 totaltotal 19.6% 31 / 158 CC 24 SCAR 46.1
CC
24
Cov
17%
shell_options
240.0
shell
24.7
_terminal
12.0
CRAP 100.6
↑ Summary ReadLine/Demo.pm »
1 package App::Rad::Plugin::ReadLine;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $App::Rad::Plugin::ReadLine::VERSION = '0.002';
4 }
5  
6 # ABSTRACT: App::Rad::Plugin::ReadLine a Term::UI ->shell for Rad Apps
7  
8  
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $GreetWithCommand = 'help'; # when the shell starts, run this command
13 1 our $GreetWithSub ; # you can pass in a sub too, both GreetWith's are run
14 1 our $DefaultCommand = '' ;
15 1 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 1 our $still_going = 1;
20  
21  
22 1 ▶use Term::UI;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 1 our $term;
27 0 ▶sub _terminal {
Subroutine _terminal: not called CC=3 SCAR=24.8
28 0 ▶ $term ||= Term::ReadLine->new($ENV{TERM} || 'critter');
Condition0%0/5
$App::Rad::Plugin::ReadLine::term ||= "Term::ReadLine"->new($ENV{"TERM"} || "critter")
l!l&&r!l&&!r
000
$ENV{'TERM'} || 'critter'
l!l
00
MC/DC0%0/2
$App::Rad::Plugin::ReadLine::term ||= "Term::ReadLine"->new($ENV{"TERM"} || "critter")
$App::Rad::Plugin::ReadLine::term "Term::ReadLine"->new($ENV{"TERM"} || "critter")
MC/DC0%0/1
$ENV{'TERM'} || 'critter'
$ENV{'TERM'}
Truth table0%0/3
A ||= B
A= $App::Rad::Plugin::ReadLine::termB= "Term::ReadLine"->new($ENV{"TERM"} || "critter")
ABresult
1X1
011
000
Truth table0%0/2
A
A= $ENV{'TERM'}
Aresult
11
00
29 }
30  
31 0 ▶sub _shell_prompt {
Subroutine _shell_prompt: not called CC=2 SCAR=17.9
32 # add a space to the end
33 0 ▶ $ShellPrompt =~ / $/? $ShellPrompt : "$ShellPrompt "
Branch: 1 branch
BranchTrueFalseTotal
$App::Rad::Plugin::ReadLine::ShellPrompt =~ / $/ ? : 0 0 0
34 }
35 0 ▶sub _shell_help { "run $0 in interactive mode" };
Subroutine _shell_help: not called CC=1 SCAR=6.9
36 0 ▶sub _shell_command { 'shell' }
Subroutine _shell_command: not called CC=1 SCAR=6.9
37  
38  
39 0 ▶sub shell_options {
Subroutine shell_options: not called CC=15 SCAR=54.8
Pod documented
40 0 my $c = shift;
41  
42 0 ▶ if (@_==0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 0 0 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 ▶ if ('HASH' eq ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $_[0]) { } 0 0 0
51 0 ($opts, $name, $help) = @_;
52 $GreetWithCommand = $opts->{GreetWithCommand}
53 0 ▶ if exists $opts->{GreetWithCommand};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"GreetWithCommand"} 0 0 0
54 $GreetWithSub = $opts->{GreetWithSub}
55 0 ▶ if exists $opts->{GreetWithSub};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"GreetWithSub"} 0 0 0
56 $DefaultCommand = $opts->{DefaultCommand}
57 0 ▶ if exists $opts->{DefaultCommand};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"DefaultCommand"} 0 0 0
58 $ShellPrompt = $opts->{ShellPrompt}
59 0 ▶ if exists $opts->{ShellPrompt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"ShellPrompt"} 0 0 0
60  
61 0 ▶ $c->debug(sprintf'shell_options '
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
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 ▶ $no_register = 1
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 2 and not defined $name 0 0 0
Condition0%0/3
@_ >= 2 and not defined $name
!ll&&!rl&&r
000
MC/DC0%0/2
@_ >= 2 and not defined $name
@_ >= 2 not defined $name
Truth table0%0/3
A and not(B)
A= @_ >= 2B= not defined $name
ABresult
0X0
100
111
76 if @_ >= 2 and not defined $name;
77 }
78 else {
79 0 ($name, $help) = @_;
80 0 ▶ $no_register = 1
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 1 and not defined $name 0 0 0
Condition0%0/3
@_ >= 1 and not defined $name
!ll&&!rl&&r
000
MC/DC0%0/2
@_ >= 1 and not defined $name
@_ >= 1 not defined $name
Truth table0%0/3
A and not(B)
A= @_ >= 1B= not defined $name
ABresult
0X0
100
111
81 if @_ >= 1 and not defined $name;
82 }
83  
84 0 ▶ if ($no_register) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_register) { } 0 0 0
85 0 $c->debug("not registering shell as a command since \$name was passed as undef");
86 }
87 else {
88 0 ▶ $name = _shell_command() if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
89 0 ▶ $help = _shell_help() if not defined $help;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $help 0 0 0
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 0 ▶sub _welcome {
Subroutine _welcome: not called CC=3 SCAR=24.8
102 0 my $c = shift;
103 # $c->critters();
104 0 $c->stash->{shelllvl} ++;
105  
106 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $App::Rad::Plugin::ReadLine::GreetWithCommand) 0 0 0
107 0 local $c->{'cmd'} = $GreetWithCommand;
108 0 $c->execute();
109 } if defined $GreetWithCommand;
110  
111 0 ▶ $c->$GreetWithSub() if defined $GreetWithSub;
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::Rad::Plugin::ReadLine::GreetWithSub 0 0 0
112  
113 }
114  
115 0 ▶sub shell {
Subroutine shell: not called CC=5 SCAR=32.1
Pod documented
116 0 my $c = shift;
117 # $c->critters();
118 0 $c->stash->{shelllvl} ++;
119 0 ▶ {no warnings qw[ redefine ];
Subroutine BEGIN: called CC=1 SCAR=0.2
120 0 ▶ *App::Rad::Help::usage = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 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 ▶ $c->shell_options(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
134  
135 0 ▶ $c->register( 'exit', sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 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) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Rad::Plugin::ReadLine::still_going) 0 0 0
145 0 (my $cmd, local @ARGV) = split ' ',
146 _terminal->get_reply(
147 prompt => _shell_prompt(),
148 default => $DefaultCommand,
149 );
150 0 ▶ if (defined $cmd and $cmd ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd and $cmd ne "") 0 0 0
Condition0%0/3
defined $cmd and $cmd ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cmd and $cmd ne ""
defined $cmd $cmd ne ""
Truth table0%0/3
A and B
A= defined $cmdB= $cmd ne ""
ABresult
0X0
100
111
151 0 @{$c->argv} = @ARGV;
152 0 $c->{'cmd'} = $cmd;
153  
154 0 $c->debug('received command: ' . $c->{'cmd'});
155 0 $c->debug('received parameters: ' . join (' ', @{$c->argv} ));
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 1 "Say my name"
167  
168 __END__
↑ Summary ReadLine/Demo.pm »