/usr/share/perl5/Catmandu/Interactive.pm is in libcatmandu-perl 0.9505-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | package Catmandu::Interactive;
use Catmandu::Sane;
our $VERSION = '0.9505';
use Catmandu;
use Moo;
use namespace::clean;
has in => (is => 'ro' , default => sub {
Catmandu::Util::io(\*STDIN);
});
has out => (is => 'ro' , default => sub {
Catmandu::Util::io(\*STDOUT, mode => 'w', binmode => ':encoding(utf-8)');
});
has silent => (is => 'ro');
has exporter => (is => 'ro' , default => sub { 'YAML'} );
has exporter_args => (is => 'ro' , default => sub { +{} });
has header => (is => 'ro' , default => sub {
"\e[36m\n" .
" A_A ____ _ _ \n" .
" (-.-) / ___|__ _| |_ _ __ ___ __ _ _ __ __| |_ _ \n" .
" |-| | | / _` | __| '_ ` _ \\ / _` | '_ \\ / _` | | | | \n" .
" / \\ | |__| (_| | |_| | | | | | (_| | | | | (_| | |_| | \n" .
" | | \\____\\__,_|\\__|_| |_| |_|\\__,_|_| |_|\\__,_|\\__,_|\n" .
" | || | | \\___ version: $Catmandu::VERSION \n" .
" \\_||_/_/ \e[0m\n" .
" \n" .
"Commands: | Interactive support is still \n" .
" \\h - the fix history | experimental. Run: \n" .
" \\r - repeat the previous fix | \$ catmandu run <your_fix_script> \n" .
" \\q - quit | to access all Catmandu features \n";
});
has data => (is => 'rw' , default => sub { + {} });
has _history => (is => 'ro' , default => sub { [] });
sub run {
my $self = shift;
my $keep_reading = 0;
my $buffer = '';
$self->head;
$self->prompt;
while (my $line = $self->in->getline) {
if ($line =~ /^\\(.*)/) {
next if length $buffer;
my ($command,$args) = split(/\s+/,$1,2);
if ($command eq 'h') {
$self->cmd_history;
$self->prompt('fix');
next;
}
elsif ($command eq 'r') {
if (@{$self->_history} > 0) {
$line = $self->_history->[-1];
} else {
$self->prompt('fix');
next;
}
}
elsif ($command eq 'q') {
last;
}
else {
$self->error("unknown command $command");
$self->prompt('fix');
next;
}
}
$line = "$buffer$line" if length $buffer;
if (length $line) {
my ($fixes,$keep_reading,$error) = $self->parse_fixes($line,$keep_reading);
if ($error) {
$buffer = '';
}
elsif ($keep_reading == 0) {
my $fixer = Catmandu::Fix->new(fixes => $fixes);
$self->data( $fixer->fix($self->data) );
$self->export;
push(@{$self->_history},$line);
$buffer = '';
}
else {
$buffer = $line;
$self->prompt('...');
next;
}
}
$self->prompt('fix');
}
}
sub cmd_history {
my ($self) = @_;
$self->out->printf(join("",@{$self->_history}));
}
sub head {
my ($self) = @_;
$self->out->printf("%s\n" , $self->header) unless $self->silent;
}
sub error {
my ($self,$txt) = @_;
$self->out->print("ERROR: $txt\n") unless $self->silent;
}
sub prompt {
my ($self,$txt) = @_;
$txt //= 'fix';
$self->out->printf("\e[35m%s > \e[0m" , $txt) unless $self->silent;
}
sub export {
my ($self) = @_;
my $exporter = Catmandu->exporter(
$self->exporter, %{$self->exporter_args} , fh => $self->out
);
$exporter->add($self->data);
$exporter->commit;
}
sub parse_fixes {
my ($self,$string,$keep_reading) = @_;
my $parser = Catmandu::Fix::Parser->new;
my $fixes;
my $error =0;
try {
$fixes = $parser->parse($string);
$keep_reading = 0;
}
catch {
if (ref($_) eq 'Catmandu::FixParseError' && $_->message =~ /Can't use an undefined value as a SCALAR reference at/) {
$keep_reading = 1;
}
else {
$_ =~ s/\n.*//g;
$self->error($_);
$error = 1;
}
};
return ($fixes,$keep_reading,$error);
}
1;
__END__
=pod
=head1 NAME
Catmandu::Interactive - An interactive command line interpreter of the Fix language
=head1 SYNOPSIS
# On the command line
catmandu run
# Or, in Perl
use Catmandu::Interactive;
use Getopt::Long;
my $exporter = 'YAML';
GetOptions("exporter=s" => \$exporter);
my $app = Catmandu::Interactive->new(exporter => $exporter);
$app->run();
=head1 DESCRIPTION
This module provide a simple interactive interface to the Catmandu Fix language.
=head1 CONFIGURATION
=over
=item in
Read input from an IO::Handle
=item out
Write output to an IO::Handle
=item silent
If set true, then no headers or prompts are printed
=item data
A hash containing the input record
=item exporter
The name of an exporter package
=item exporter_args
The options for the exporter
=back
=head1 METHODS
=head2 run
Run the interactive environment.
=head1 SEE ALSO
L<Catmandu>
=cut
|