| 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")
$ENV{'TERM'} || 'critter'
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")
Truth table0%0/2
A
A= $ENV{'TERM'}
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 51 | 0 | ($opts, $name, $help) = @_; | |||||||||||||||||||||||||||||
| 52 | $GreetWithCommand = $opts->{GreetWithCommand} | ||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if exists $opts->{GreetWithCommand}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 54 | $GreetWithSub = $opts->{GreetWithSub} | ||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if exists $opts->{GreetWithSub}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 56 | $DefaultCommand = $opts->{DefaultCommand} | ||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if exists $opts->{DefaultCommand}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 58 | $ShellPrompt = $opts->{ShellPrompt} | ||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if exists $opts->{ShellPrompt}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $c->debug(sprintf'shell_options ' | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
Condition0%0/3
@_ >= 2 and not defined $name
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
| |||||||||||||||||||||||||||||||
| 76 | if @_ >= 2 and not defined $name; | ||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||||
| 79 | 0 | ($name, $help) = @_; | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $no_register = 1 | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ >= 1 and not defined $name
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
| |||||||||||||||||||||||||||||||
| 81 | if @_ >= 1 and not defined $name; | ||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ($no_register) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $help = _shell_help() if not defined $help; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $cmd and $cmd ne ""
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 ""
| |||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||