| 1 | package App::Cleo; | ||||||||||||||||||||||||||||||||
| 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 | our $VERSION = 0.004; | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 16 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | my $self = { | ||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | shell => $ENV{SHELL} || '/bin/bash', | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{'SHELL'} || '/bin/bash'
MC/DC0%0/1
$ENV{'SHELL'} || '/bin/bash'
$ENV{'SHELL'}
Truth table0%0/2
A
A= $ENV{'SHELL'}
| |||||||||||||||||||||||||||||||||
| 20 | prompt => colored( ['green'], '(%d)$ '), | ||||||||||||||||||||||||||||||||
| 21 | delay => 25_000, | ||||||||||||||||||||||||||||||||
| 22 | @_, | ||||||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 0 | return bless $self, $class; | |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=21 SCAR=61.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 31 | 0 | my ($self, $commands) = @_; | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 0 | my $type = ref $commands; | |||||||||||||||||||||||||||||||
| 34 | my @commands = !$type ? read_file($commands) | ||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | : $type eq 'SCALAR' ? split "\n", ${$commands} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | : $type eq 'ARRAY' ? @{$commands} | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 37 | : die "Unsupported type: $type"; | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | open my $fh, '|-', $self->{shell} or die $!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 0 | $self->{fh} = $fh; | |||||||||||||||||||||||||||||||
| 41 | 0 | ReadMode('raw'); | |||||||||||||||||||||||||||||||
| 42 | 0 | local $| = 1; | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 0 | chomp @commands; | |||||||||||||||||||||||||||||||
| 45 | 0 | @commands = grep { /^\s*[^\#;]\S+/ } @commands; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | CMD: | ||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | for (my $i = 0; $i < @commands; $i++) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 0 | my $cmd = $commands[$i]; | |||||||||||||||||||||||||||||||
| 51 | 0 | chomp $cmd; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $self->do_cmd($cmd) and next CMD | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$cmd =~ s/^!!!// && ($self->do_cmd($cmd) && (next CMD))
$self->do_cmd($cmd) and next CMD
MC/DC0%0/2
$cmd =~ s/^!!!// && ($self->do_cmd($cmd) && (next CMD))
$cmd =~ s/^!!!//
$self->do_cmd($cmd)
Truth table0%0/3
A && B
A= $cmd =~ s/^!!!//B= $self->do_cmd($cmd)
| |||||||||||||||||||||||||||||||||
| 54 | if $cmd =~ s/^!!!//; | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 0 | print sprintf $self->{prompt}, $i; | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | my @steps = split /%%%/, $cmd; | |||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | while (my $step = shift @steps) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 0 | my $key = ReadKey(0); | |||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | print "\n" if $key =~ m/[srp]/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | last CMD if $key eq 'q'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | next CMD if $key eq 's'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | redo CMD if $key eq 'r'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | $i--, redo CMD if $key eq 'p'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $step .= ' ' if not @steps; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 0 | my @chars = split '', $step; | |||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | print and usleep $self->{delay} for @chars; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 0 | my $key = ReadKey(0); | |||||||||||||||||||||||||||||||
| 75 | 0 | print "\n"; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | last CMD if $key eq 'q'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | next CMD if $key eq 's'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | redo CMD if $key eq 'r'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $i--, redo CMD if $key eq 'p'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | $self->do_cmd($cmd); | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 0 | ReadMode('restore'); | |||||||||||||||||||||||||||||||
| 86 | 0 | print "\n"; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub do_cmd { | ||||||||||||||||||||||||||||||
|
Subroutine
do_cmd: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 94 | 0 | my ($self, $cmd) = @_; | |||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | 0 | my $cmd_is_finished; | |||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | local $SIG{ALRM} = sub {$cmd_is_finished = 1}; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 0 | $cmd =~ s/%%%//g; | |||||||||||||||||||||||||||||||
| 100 | 0 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 0 | print $fh "$cmd\n"; | |||||||||||||||||||||||||||||||
| 103 | 0 | print $fh "kill -14 $$\n"; | |||||||||||||||||||||||||||||||
| 104 | 0 | $fh->flush; | |||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | # Wait for signal that command has ended | ||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | until ($cmd_is_finished) {} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | 0 | $cmd_is_finished = 0; | |||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 114 | 1 | 1; | |||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | =pod | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | App::Cleo - Play back shell commands for live demonstrations | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | use App::Cleo | ||||||||||||||||||||||||||||||||
| 125 | my $cleo = App::Cleo->new(%options); | ||||||||||||||||||||||||||||||||
| 126 | $cleo->run($commands); | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | App::Cleo is the back-end for the L<cleo> utility. Please see the L<cleo> | ||||||||||||||||||||||||||||||||
| 131 | documentation for details on how to use this. | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | The constructor accepts arguments as key-value pairs. The following keys are | ||||||||||||||||||||||||||||||||
| 136 | supported: | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | =over 4 | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | =item delay | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | Number of milliseconds to wait before displaying each character of the command. | ||||||||||||||||||||||||||||||||
| 143 | The default is C<25_000>. | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | =item prompt | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | String to use for the artificial prompt. The token C<%d> will be substituted | ||||||||||||||||||||||||||||||||
| 148 | with the number of the current command. The default is C<(%d)$>. | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | =item shell | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | Path to the shell command that will be used to run the commands. Defaults to | ||||||||||||||||||||||||||||||||
| 153 | either the C<SHELL> environment variable or C</bin/bash>. | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | =back | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | =over 4 | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | =item run( $commands ) | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | Starts playback of commands. If the argument is a string, it will be treated | ||||||||||||||||||||||||||||||||
| 164 | as a file name and commands will be read from the file. If the argument is a | ||||||||||||||||||||||||||||||||
| 165 | scalar reference, it will be treated as a string of commands separated by | ||||||||||||||||||||||||||||||||
| 166 | newlines. If the argument is an array reference, then each element of the | ||||||||||||||||||||||||||||||||
| 167 | array will be treated as a command. | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | =back | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | Jeffrey Ryan Thalhammer <thaljef@cpan.org> | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | Copyright (c) 2014, Imaginative Software Systems | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||||||||||||||