blib/lib/App/Cleo/Patra.pm

filter: Statementstmt 27.1% 29 / 107 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 64.2% 9 / 14 Podpod 33.3% 1 / 3 totaltotal 18.3% 39 / 213 CC 38 SCAR 66.1
CC
38
Cov
16%
run
1113.5
do_cmd
20.0
new
6.0
CRAP 744.4
↑ Summary
1 package App::Cleo::Patra;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Term::ANSIColor qw(colored);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use constant PS1 => 'ps1';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant PS2 => 'ps2';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 our $VERSION = 0.001;
14  
15 #-----------------------------------------------------------------------------
16  
17 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
18 0 my $class = shift;
19  
20 my $self = {
21 0 ▶ shell => $ENV{SHELL} || '/bin/bash',
Condition0%0/2
$ENV{'SHELL'} || '/bin/bash'
l!l
00
MC/DC0%0/1
$ENV{'SHELL'} || '/bin/bash'
$ENV{'SHELL'}
Truth table0%0/2
A
A= $ENV{'SHELL'}
Aresult
11
00
22 ps1 => colored( ['green'], '(%d)$ '),
23 ps2 => colored( ['green'], '> '),
24 delay => 25_000,
25 state => PS1,
26 @_,
27 };
28  
29 0 return bless $self, $class;
30 }
31  
32 #-----------------------------------------------------------------------------
33  
34 0 ▶sub run {
Subroutine run: not called CC=34 SCAR=70.2
Pod documented
35 0 my ($self, $input, $multiline) = @_;
36  
37 0 my $type = ref $input;
38 # my @commands_raw = !$type ? read_file($commands_raw)
39 # : $type eq 'SCALAR' ? split "\n", ${$commands_raw}
40 ##: $type eq 'SCALAR' and $multiline ? split /^\$\s/m,${$commands_raw}
41 # : $type eq 'ARRAY' ? @{$commands_raw}
42 # : die "Unsupported type: $type";
43  
44 0 my @commands = ();
45 0 ▶ if (!$type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 0 0
46 0 ▶ if ($multiline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiline) { } 0 0 0
47 0 my $data = read_file($input);
48 0 @commands = split /^\$\s/m, $data;
49 }
50 else {
51 0 @commands = read_file($input);
52 }
53 }
54  
55 0 ▶ open my $fh, '|-', $self->{shell} or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "|-", $self->{"shell"} or die $! 0 0 0
56 0 $self->{fh} = $fh;
57 0 ReadMode('raw');
58 0 local $| = 1;
59  
60 0 ▶ local $SIG{CHLD} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 print "Child shell exited!\n";
62 0 ReadMode('restore');
63 0 exit;
64 0 };
65  
66 0 chomp @commands;
67 0 @commands = grep { /^\s*[^\#;]\S+/ } @commands;
68 0 ▶ @commands = grep { /.+/ } @commands if $multiline;
Branch: 1 branch
BranchTrueFalseTotal
if $multiline 0 0 0
69  
70 # # squeeze multi line commands into one array slot (indicated by ~~~)
71 # my @commands = ();
72 # for (my $i=0; $i<@commands_raw; $i++) {
73 # if ($commands_raw[$i] =~ /[~]{3}(.*)/ and $i != 0) {
74 # $commands[@commands - 1] .= "\n$1";
75 # }
76 # else {
77 # push @commands, $commands_raw[$i];
78 # }
79 # }
80  
81 0 my $continue_to_end = 0;
82  
83 CMD:
84 0 ▶ for (my $i = 0; $i < @commands; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @commands) 0 0 0
85  
86 0 ▶ my $cmd = defined $commands[$i] ? $commands[$i] : die "no command $i";
Branch: 1 branch
BranchTrueFalseTotal
defined $commands[$i] ? : 0 0 0
87 0 chomp $cmd;
88  
89 0 my $keep_going = $cmd =~ s/^\.\.\.//;
90 0 my $run_in_background = $cmd =~ s/^!!!//;
91  
92 0 ▶ $self->do_cmd($cmd) and next CMD
Branch: 1 branch
BranchTrueFalseTotal
if $run_in_background 0 0 0
Condition0%0/5
$run_in_background && ($self->do_cmd($cmd) && (next CMD))
!ll&&!rl&&r
000
$self->do_cmd($cmd) and next CMD
!ll
00
MC/DC0%0/2
$run_in_background && ($self->do_cmd($cmd) && (next CMD))
$run_in_background $self->do_cmd($cmd)
Truth table0%0/3
A && B
A= $run_in_backgroundB= $self->do_cmd($cmd)
ABresult
0X0
100
111
93 if $run_in_background;
94  
95 1 ▶ no warnings 'redundant';
Subroutine BEGIN: called CC=1 SCAR=0.0
96 0 my $prompt_state = $self->{state};
97 0 print sprintf $self->{$prompt_state}, $i;
98  
99 0 my @steps = split /%%%/, $cmd;
100 0 ▶ while (my $step = shift @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $step = shift @steps) 0 0 0
101  
102 0 ▶ my $should_pause = !($keep_going || $continue_to_end);
Condition0%0/3
$keep_going || $continue_to_end
l!l&&r!l&&!r
000
MC/DC0%0/2
$keep_going || $continue_to_end
$keep_going $continue_to_end
Truth table0%0/3
A || B
A= $keep_goingB= $continue_to_end
ABresult
1X1
011
000
103 0 ▶ my $key = $should_pause ? ReadKey(0) : '';
Branch: 1 branch
BranchTrueFalseTotal
$should_pause ? : 0 0 0
104 0 ▶ if ($key =~ /^\d$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^\d$/) 0 0 0
105 0 ▶ $key .= $1 while (ReadKey(0) =~ /^(\d)/);
Branch: 1 branch
BranchTrueFalseTotal
ReadKey(0) =~ /^(\d)/ and do { $key .= $1 } 0 0 0
106 }
107 0 ▶ print "\n" if $key =~ m/^[srp]|[0-9]+/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^[srp]|[0-9]+/ 0 0 0
108  
109 0 ▶ last CMD if $key eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" 0 0 0
110 0 ▶ next CMD if $key eq 's';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "s" 0 0 0
111 0 ▶ redo CMD if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 0 0 0
112 0 ▶ $i--, redo CMD if $key eq 'p';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "p" 0 0 0
113 0 ▶ $i = $key, redo CMD if $key =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^\d+$/ 0 0 0
114 0 ▶ $continue_to_end = 1 if $key eq 'c';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "c" 0 0 0
115  
116 0 ▶ $step .= ' ' if not @steps;
Branch: 1 branch
BranchTrueFalseTotal
unless @steps 0 0 0
117 0 my @chars = split '', $step;
118 0 ▶ print and usleep $self->{delay} for @chars;
Branch: 1 branch
BranchTrueFalseTotal
print $_ and &usleep($self->{"delay"}) 0 0 0
119 }
120  
121 0 ▶ my $should_pause = !($keep_going || $continue_to_end);
Condition0%0/3
$keep_going || $continue_to_end
l!l&&r!l&&!r
000
MC/DC0%0/2
$keep_going || $continue_to_end
$keep_going $continue_to_end
Truth table0%0/3
A || B
A= $keep_goingB= $continue_to_end
ABresult
1X1
011
000
122 0 ▶ my $key = $should_pause ? ReadKey(0) : '';
Branch: 1 branch
BranchTrueFalseTotal
$should_pause ? : 0 0 0
123 0 ▶ if ($key =~ /^\d$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^\d$/) 0 0 0
124 0 ▶ $key .= $1 while (ReadKey(0) =~ /^(\d)/);
Branch: 1 branch
BranchTrueFalseTotal
ReadKey(0) =~ /^(\d)/ and do { $key .= $1 } 0 0 0
125 }
126 0 print "\n";
127  
128 0 ▶ last CMD if $key eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" 0 0 0
129 0 ▶ next CMD if $key eq 's';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "s" 0 0 0
130 0 ▶ redo CMD if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 0 0 0
131 0 ▶ $i--, redo CMD if $key eq 'p';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "p" 0 0 0
132 0 ▶ $i = $key, redo CMD if $key =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^\d+$/ 0 0 0
133 0 ▶ $continue_to_end = 1 if $key eq 'c';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "c" 0 0 0
134  
135 0 $self->do_cmd($cmd);
136 }
137  
138 0 ReadMode('restore');
139 0 print "\n";
140  
141 0 return $self;
142 }
143  
144 #-----------------------------------------------------------------------------
145  
146 0 ▶sub do_cmd {
Subroutine do_cmd: not called CC=4 SCAR=30.0
Pod undocumented
147 0 my ($self, $cmd) = @_;
148  
149 0 my $cmd_is_finished;
150 0 ▶ local $SIG{ALRM} = sub {$cmd_is_finished = 1};
Subroutine __ANON__: not called CC=1 SCAR=6.9
151  
152 0 $cmd =~ s/%%%//g;
153 0 my $fh = $self->{fh};
154  
155 0 print $fh "$cmd\n";
156  
157 0 ▶ ($self->{state} = PS2) and return 1
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /\s+\\$/ 0 0 0
Condition0%0/5
$cmd =~ /\s+\\$/ && (($self->{'state'} = 'ps2') && (return 1))
!ll&&!rl&&r
000
$self->{"state"} = "ps2" and return 1
!ll
00
MC/DC0%0/2
$cmd =~ /\s+\\$/ && (($self->{'state'} = 'ps2') && (return 1))
$cmd =~ /\s+\\$/ $self->{"state"} = "ps2"
Truth table0%0/3
A && B
A= $cmd =~ /\s+\\$/B= $self->{"state"} = "ps2"
ABresult
0X0
100
111
158 if $cmd =~ m{\s+\\$};
159  
160 0 print $fh "kill -14 $$\n";
161 0 $fh->flush;
162  
163 # Wait for signal that command has ended
164 0 ▶ until ($cmd_is_finished) {}
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd_is_finished) 0 0 0
165 0 $cmd_is_finished = 0;
166  
167 0 $self->{state} = PS1;
168  
169 0 return 1;
170 }
171  
172 #-----------------------------------------------------------------------------
173 1 1;
174  
175 =pod
176  
177 =encoding utf8
178  
179 =head1 NAME
180  
181 App::Cleo - Play back shell commands for live demonstrations
182  
183 =head1 SYNOPSIS
184  
185 use App::Cleo::Patra
186 my $patra = App::Cleo::Patra->new(%options);
187 $patra->run($commands);
188  
189 =head1 DESCRIPTION
190  
191 B<Important:>
192 C<patra> is an experimental fork from C<cleo>.
193 You should check the current differences from C<App-Cleo> and decide, which one you want to use.
194 It may be, that in your current time, C<patra> is merged back into C<cleo> or obsolete for other reasons.
195  
196 App::Cleo::Patra is the back-end for the L<patra> utility. Please see the L<patra>
197 documentation for details on how to use this.
198  
199 =head1 CONSTRUCTOR
200  
201 The constructor accepts arguments as key-value pairs. The following keys are
202 supported:
203  
204 =over 4
205  
206 =item delay
207  
208 Number of microseconds to wait before displaying each character of the command.
209 The default is C<25_000>.
210  
211 =item ps1
212  
213 String to use for the artificial prompt. The token C<%d> will be substituted
214 with the number of the current command. The default is C<(%d)$>.
215  
216 =item ps2
217  
218 String to use for the artificial prompt that appears for multiline commands. The
219 token C<%d> will be substituted with the number of the current command. The
220 default is C<< > >>.
221  
222 =item shell
223  
224 Path to the shell command that will be used to run the commands. Defaults to
225 either the C<SHELL> environment variable or C</bin/bash>.
226  
227 =back
228  
229 =head1 METHODS
230  
231 =over 4
232  
233 =item run( $commands )
234  
235 Starts playback of commands. If the argument is a string, it will be treated
236 as a file name and commands will be read from the file. If the argument is a
237 scalar reference, it will be treated as a string of commands separated by
238 newlines. If the argument is an array reference, then each element of the
239 array will be treated as a command.
240  
241 =back
242  
243 =head1 AUTHOR
244  
245 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
246  
247 Boris Däppeb (BORISD) <bdaeppen.perl@gmail.com>
248  
249 =head1 COPYRIGHT
250  
251 cleo - Copyright (c) 2014, Imaginative Software Systems
252  
253 patra - Boris Däppen (BORISD) 2018
254  
255 =cut
↑ Summary