Catmandu/Interactive.pm

filter: Statementstmt 88.3% 83 / 94 Branchbran 66.6% 20 / 30 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 90.0% 18 / 20 Podpod 14.2% 1 / 7 totaltotal 79.2% 126 / 159 CC 18 SCAR 17.5
CC
18
Cov
82%
run
13.9
__ANON__
3.6
prompt
3.0
CRAP 5.7
« Catmandu/Emit.pm ↑ Summary Catmandu/Cmd/import.pm »
1 package Catmandu::Interactive;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.2025';
6  
7 1 ▶use Catmandu;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 has in => (
12 is => 'ro',
13 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 0 Catmandu::Util::io(\*STDIN);
15 }
16 1 );
17  
18 has out => (
19 is => 'ro',
20 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 Catmandu::Util::io(
22 \*STDOUT,
23 mode => 'w',
24 binmode => ':encoding(utf-8)'
25 );
26 }
27 1 );
28  
29 1 has silent => (is => 'ro');
30  
31 1 ▶has exporter => (is => 'ro', default => sub {'YAML'});
Subroutine __ANON__: called CC=1 SCAR=0.0
32  
33 1 ▶has exporter_args => (is => 'ro', default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
34  
35 has header => (
36 is => 'ro',
37 5 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 5 "\e[36m\n"
39 . " A_A ____ _ _ \n"
40 . " (-.-) / ___|__ _| |_ _ __ ___ __ _ _ __ __| |_ _ \n"
41 . " |-| | | / _` | __| '_ ` _ \\ / _` | '_ \\ / _` | | | | \n"
42 . " / \\ | |__| (_| | |_| | | | | | (_| | | | | (_| | |_| | \n"
43 . " | | \\____\\__,_|\\__|_| |_| |_|\\__,_|_| |_|\\__,_|\\__,_|\n"
44 . " | || | | \\___ version: $Catmandu::VERSION \n"
45 . " \\_||_/_/ \e[0m\n"
46 . " \n"
47 . "Commands: | Interactive support is still \n"
48 . " \\h - the fix history | experimental. Run: \n"
49 . " \\r - repeat the previous fix | \$ catmandu run <your_fix_script> \n"
50 . " \\q - quit | to access all Catmandu features \n";
51 }
52 1 );
53  
54 5 ▶has data => (is => 'rw', default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
55  
56 5 ▶has _history => (is => 'ro', default => sub {[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
57  
58 5 ▶sub run {
Subroutine run: called CC=12 SCAR=26.3
Pod documented
59 5 my $self = shift;
60  
61 5 my $keep_reading = 0;
62 5 my $buffer = '';
63  
64 5 $self->head;
65  
66 5 $self->prompt;
67  
68 5 ▶ while (my $line = $self->in->getline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->in->getline) 10 4 14
69 10 ▶ if ($line =~ /^\\(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\\(.*)/) 3 7 10
70 3 ▶ next if length $buffer;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer 0 3 3
71  
72 3 my ($command, $args) = split(/\s+/, $1, 2);
73  
74 3 ▶ if ($command eq 'h') {
Branch: 3 branches
BranchTrueFalseTotal
if ($command eq 'h') { } 1 2 3
elsif ($command eq 'r') { } 1 1 2
elsif ($command eq 'q') { } 1 0 1
75 1 $self->cmd_history;
76 1 $self->prompt('fix');
77 1 next;
78 }
79 elsif ($command eq 'r') {
80 1 ▶ if (@{$self->_history} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_history;} > 0) { } 1 0 1
81 1 $line = $self->_history->[-1];
82 }
83 else {
84 0 $self->prompt('fix');
85 0 next;
86 }
87 }
88 elsif ($command eq 'q') {
89 1 last;
90 }
91 else {
92 0 $self->error("unknown command $command");
93 0 $self->prompt('fix');
94 0 next;
95 }
96 }
97  
98 8 ▶ $line = "$buffer$line" if length $buffer;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer 0 8 8
99  
100 8 ▶ if (length $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line) 8 0 8
101 8 my ($fixes, $keep_reading, $error)
102 = $self->parse_fixes($line, $keep_reading);
103  
104 8 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 2 6 8
elsif ($keep_reading == 0) { } 6 0 6
105 2 $buffer = '';
106 }
107 elsif ($keep_reading == 0) {
108 6 my $fixer = Catmandu::Fix->new(fixes => $fixes);
109  
110 6 $self->data($fixer->fix($self->data));
111 6 $self->export;
112  
113 6 push(@{$self->_history}, $line);
114  
115 6 $buffer = '';
116 }
117 else {
118 0 $buffer = $line;
119 0 $self->prompt('...');
120 0 next;
121 }
122 }
123  
124 8 $self->prompt('fix');
125 }
126 }
127  
128 1 ▶sub cmd_history {
Subroutine cmd_history: called CC=1 SCAR=0.0
Pod undocumented
129 1 my ($self) = @_;
130  
131 1 $self->out->printf(join("", @{$self->_history}));
132 }
133  
134 5 ▶sub head {
Subroutine head: called CC=2 SCAR=7.2
Pod undocumented
135 5 my ($self) = @_;
136  
137 5 ▶ $self->out->printf("%s\n", $self->header) unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 5 5
138 }
139  
140 2 ▶sub error {
Subroutine error: called CC=2 SCAR=7.2
Pod undocumented
141 2 my ($self, $txt) = @_;
142 2 ▶ $self->out->print("ERROR: $txt\n") unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 2 2
143 }
144  
145 14 ▶sub prompt {
Subroutine prompt: called CC=3 SCAR=11.1
Pod undocumented
146 14 my ($self, $txt) = @_;
147 14 ▶ $txt //= 'fix';
Condition100%2/2
$txt //= "fix"
l!l
95
MC/DC100%1/1
$txt //= "fix"
$txt
Truth table100%2/2
A
A= $txt
Aresult
11
00
148  
149 14 ▶ $self->out->printf("\e[35m%s > \e[0m", $txt) unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 14 14
150 }
151  
152 6 ▶sub export {
Subroutine export: called CC=1 SCAR=0.0
Pod undocumented
153 6 my ($self) = @_;
154 my $exporter = Catmandu->exporter(
155 $self->exporter,
156 6 %{$self->exporter_args},
157 fh => $self->out
158 );
159 6 $exporter->add($self->data);
160 6 $exporter->commit;
161 }
162  
163 8 ▶sub parse_fixes {
Subroutine parse_fixes: called CC=1 SCAR=0.1
Pod undocumented
164 8 my ($self, $string, $keep_reading) = @_;
165  
166 8 my $parser = Catmandu::Fix::Parser->new;
167  
168 8 my $fixes;
169 8 my $error = 0;
170  
171 8 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 8 $fixes = $parser->parse($string);
173 6 $keep_reading = 0;
174 }
175 2 ▶ catch {
Subroutine __ANON__: called CC=3 SCAR=12.7
176 2 ▶ if (ref($_) eq 'Catmandu::FixParseError'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'Catmandu::FixParseError' and $_->message =~ /Can't use an undefined value as a SCALAR reference at/) { } 0 2 2
Condition33%1/3
ref $_ eq "Catmandu::FixParseError" and $_->message =~ /Can't use an undefined value as a SCALAR reference at/
!ll&&!rl&&r
020
MC/DC0%0/2
ref $_ eq "Catmandu::FixParseError" and $_->message =~ /Can't use an undefined value as a SCALAR reference at/
ref $_ eq "Catmandu::FixParseError" $_->message =~ /Can't use an undefined value as a SCALAR reference at/
Truth table33%1/3
A and B
A= ref $_ eq "Catmandu::FixParseError"B= $_->message =~ /Can't use an undefined value as a SCALAR reference at/
ABresult
0X0
100
111
177 && $_->message
178 =~ /Can't use an undefined value as a SCALAR reference at/)
179 {
180 0 $keep_reading = 1;
181 }
182 else {
183 2 $_ =~ s/\n.*//g;
184 2 $self->error($_);
185 2 $error = 1;
186 }
187 8 };
188  
189 8 return ($fixes, $keep_reading, $error);
190 }
191  
192 1 1;
193  
194 __END__
« Catmandu/Emit.pm ↑ Summary Catmandu/Cmd/import.pm »