lib/MCP/Run/Compress.pm

filter: Statementstmt 59.3% 191 / 322 Branchbran 42.2% 65 / 154 Conditioncond 47.8% 44 / 92 MC/DCmcdc 28.5% 14 / 49 Subroutinesub 55.5% 15 / 27 Podpod 20.0% 1 / 5 totaltotal 50.8% 330 / 649 CC 131 SCAR 62.5
CC
131
Cov
53%
process
1806.0
compress
117.2
__ANON__
30.0
CRAP 518.6
« blib/lib/MCP/Run/Compress.pm ↑ Summary blib/lib/MCP/Run.pm »
1 package MCP::Run::Compress;
2 1 our $VERSION = '0.103';
3 1 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Output compression for LLMs
6  
7  
8 1 ▶use Text::Trim qw(trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶has filters => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14  
15 sub _parse_command {
16 16 ▶ my ($self, $command) = @_;
Subroutine _parse_command: called CC=5 SCAR=17.8
17 16 my @words = split /\s+/, $command;
18 16 ▶ return { program => '', subcommand => undef, flags => {}, args => [] } unless @words;
Branch: 1 branch
BranchTrueFalseTotal
unless @words 0 16 16
19  
20 16 my $program = shift @words;
21 16 my ($subcommand, @remaining);
22  
23 # Find the subcommand (first word not starting with -)
24 16 for my $i (0 .. $#words) {
25 15 ▶ if ($words[$i] !~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($words[$i] =~ /^-/u) 9 6 15
26 9 $subcommand = $words[$i];
27 9 @remaining = @words[$i+1 .. $#words];
28 9 last;
29 }
30 }
31 16 ▶ @remaining = @words unless defined $subcommand;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subcommand 7 9 16
32  
33 # Parse flags with Getopt::Long
34 16 my %flags;
35  
36 # Build a Getopt::Long spec for common flag types
37 my @spec = (
38 0 ▶ 'stat' => sub { $flags{stat} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 ▶ 'numstat' => sub { $flags{numstat} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 ▶ 'shortstat' => sub { $flags{shortstat} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 'w=i' => \$flags{w},
42 'width=i' => \$flags{width},
43 'stat-width=i' => \$flags{'stat-width'},
44 'stat-name-width=i' => \$flags{'stat-name-width'},
45 'M=s' => \$flags{M},
46 0 ▶ 'ignore-space-change' => sub { $flags{'ignore-space-change'} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 ▶ 'ignore-all-space' => sub { $flags{'ignore-all-space'} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 ▶ 'ignore-blank-lines' => sub { $flags{'ignore-blank-lines'} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 'U=i' => \$flags{U},
50 'unified=i' => \$flags{unified},
51 0 ▶ 'color' => sub { $flags{color} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 ▶ 'no-color' => sub { $flags{color} = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 0 ▶ 'cached' => sub { $flags{cached} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 ▶ 'no-pager' => sub { $flags{'no-pager'} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 16 );
56  
57 # Use GetOptionsFromArray to parse - suppress warnings for unknown flags
58 16 my $warn_handler = $SIG{__WARN__};
59 16 ▶ local $SIG{__WARN__} = sub { }; # Suppress warnings during parsing
Subroutine __ANON__: called CC=1 SCAR=0.0
60 16 GetOptionsFromArray(\@remaining, @spec);
61  
62 return {
63 16 program => $program,
64 subcommand => $subcommand,
65 flags => \%flags,
66 args => \@remaining,
67 };
68 }
69  
70 sub register_filter {
71 480 ▶ my $self = shift;
Subroutine register_filter: called CC=21 SCAR=30.6
Pod undocumented
72 480 my %args = @_;
73  
74 480 my $command = delete $args{command};
75 480 my $parsed_command = delete $args{parsed_command};
76  
77 # Determine storage key: parsed commands use a special key format
78 480 my $key;
79 480 ▶ if ($parsed_command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed_command) { } 370 110 480
80 370 my @flag_parts;
81 370 ▶ for my $flag (sort keys %{$parsed_command->{flags} // {}}) {
Condition100%2/2
$parsed_command->{"flags"} // {}
l!l
10360
MC/DC100%1/1
$parsed_command->{"flags"} // {}
$parsed_command->{"flags"}
Truth table100%2/2
A
A= $parsed_command->{"flags"}
Aresult
11
00
82 10 ▶ push @flag_parts, "$flag=" . ($parsed_command->{flags}{$flag} // 1);
Condition50%1/2
$parsed_command->{"flags"}{$flag} // 1
l!l
100
MC/DC0%0/1
$parsed_command->{"flags"}{$flag} // 1
$parsed_command->{"flags"}{$flag}
Truth table50%1/2
A
A= $parsed_command->{"flags"}{$flag}
Aresult
11
00
83 }
84 370 ▶ $key = 'parsed:' . ($parsed_command->{program} // '') . ':' . ($parsed_command->{subcommand} // '') . ':' . join(',', @flag_parts);
Condition75%3/4
$parsed_command->{"program"} // ""
l!l
3700
$parsed_command->{"subcommand"} // ""
l!l
260110
MC/DC0%0/1
$parsed_command->{"program"} // ""
$parsed_command->{"program"}
MC/DC100%1/1
$parsed_command->{"subcommand"} // ""
$parsed_command->{"subcommand"}
Truth table50%1/2
A
A= $parsed_command->{"program"}
Aresult
11
00
Truth table100%2/2
A
A= $parsed_command->{"subcommand"}
Aresult
11
00
85 } else {
86 110 $key = $command;
87 }
88  
89 $self->filters->{$key} = {
90 command => $command,
91 parsed_command => $parsed_command,
92 strip_ansi => $args{strip_ansi} // 0,
93 strip_lines_matching => $args{strip_lines_matching} // [],
94 keep_lines_matching => $args{keep_lines_matching} // [],
95 truncate_lines_at => $args{truncate_lines_at} // 0,
96 max_lines => $args{max_lines} // 0,
97 tail_lines => $args{tail_lines} // 0,
98 head_lines => $args{head_lines} // 0,
99 on_empty => $args{on_empty} // '',
100 replace => $args{replace} // [],
101 match_output => $args{match_output} // [],
102 filter_stderr => $args{filter_stderr} // 0,
103 output_detect => $args{output_detect} // undef,
104 transform => $args{transform} // undef,
105 command_transform => $args{command_transform} // undef,
106 480 ▶ };
Condition89%25/28
$args{"strip_ansi"} // 0
l!l
0480
$args{"strip_lines_matching"} // []
l!l
46020
$args{"keep_lines_matching"} // []
l!l
0480
$args{"truncate_lines_at"} // 0
l!l
200280
$args{"max_lines"} // 0
l!l
47010
$args{"tail_lines"} // 0
l!l
10470
$args{"head_lines"} // 0
l!l
10470
$args{"on_empty"} // ""
l!l
10470
$args{"replace"} // []
l!l
0480
$args{"match_output"} // []
l!l
20460
$args{"filter_stderr"} // 0
l!l
10470
$args{"output_detect"} // undef
l!l
10470
$args{"transform"} // undef
l!l
20460
$args{"command_transform"} // undef
l!l
10470
MC/DC0%0/1
$args{"strip_ansi"} // 0
$args{"strip_ansi"}
MC/DC100%1/1
$args{"strip_lines_matching"} // []
$args{"strip_lines_matching"}
MC/DC0%0/1
$args{"keep_lines_matching"} // []
$args{"keep_lines_matching"}
MC/DC100%1/1
$args{"truncate_lines_at"} // 0
$args{"truncate_lines_at"}
MC/DC100%1/1
$args{"max_lines"} // 0
$args{"max_lines"}
MC/DC100%1/1
$args{"tail_lines"} // 0
$args{"tail_lines"}
MC/DC100%1/1
$args{"head_lines"} // 0
$args{"head_lines"}
MC/DC100%1/1
$args{"on_empty"} // ""
$args{"on_empty"}
MC/DC0%0/1
$args{"replace"} // []
$args{"replace"}
MC/DC100%1/1
$args{"match_output"} // []
$args{"match_output"}
MC/DC100%1/1
$args{"filter_stderr"} // 0
$args{"filter_stderr"}
MC/DC100%1/1
$args{"output_detect"} // undef
$args{"output_detect"}
MC/DC100%1/1
$args{"transform"} // undef
$args{"transform"}
MC/DC100%1/1
$args{"command_transform"} // undef
$args{"command_transform"}
Truth table50%1/2
A
A= $args{"strip_ansi"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"strip_lines_matching"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"keep_lines_matching"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"truncate_lines_at"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"max_lines"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"tail_lines"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"head_lines"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"on_empty"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"replace"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"match_output"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"filter_stderr"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"output_detect"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"transform"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"command_transform"}
Aresult
11
00
107  
108 480 return;
109 }
110  
111 sub _match_parsed_command {
112 354 ▶ my ($self, $filter_spec, $parsed) = @_;
Subroutine _match_parsed_command: called CC=13 SCAR=29.9
113 354 ▶ return 0 unless $parsed && $filter_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and $filter_spec 0 354 354
Condition33%1/3
$parsed and $filter_spec
!ll&&!rl&&r
00354
MC/DC0%0/2
$parsed and $filter_spec
$parsed $filter_spec
Truth table33%1/3
A and B
A= $parsedB= $filter_spec
ABresult
0X0
100
111
114  
115 # Must match program
116 354 ▶ return 0 if ($filter_spec->{program} // '') ne ($parsed->{program} // '');
Branch: 1 branch
BranchTrueFalseTotal
if ($filter_spec->{"program"} // "") ne ($parsed->{"program"} // "") 330 24 354
Condition50%2/4
$filter_spec->{"program"} // ""
l!l
3540
$parsed->{"program"} // ""
l!l
3540
MC/DC0%0/1
$filter_spec->{"program"} // ""
$filter_spec->{"program"}
MC/DC0%0/1
$parsed->{"program"} // ""
$parsed->{"program"}
Truth table50%1/2
A
A= $filter_spec->{"program"}
Aresult
11
00
Truth table50%1/2
A
A= $parsed->{"program"}
Aresult
11
00
117  
118 # Subcommand must match if specified
119 24 ▶ if (defined $filter_spec->{subcommand}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_spec->{"subcommand"}) 24 0 24
120 24 ▶ return 0 if ($filter_spec->{subcommand} // '') ne ($parsed->{subcommand} // '');
Branch: 1 branch
BranchTrueFalseTotal
if ($filter_spec->{"subcommand"} // "") ne ($parsed->{"subcommand"} // "") 23 1 24
Condition50%2/4
$filter_spec->{"subcommand"} // ""
l!l
240
$parsed->{"subcommand"} // ""
l!l
240
MC/DC0%0/1
$filter_spec->{"subcommand"} // ""
$filter_spec->{"subcommand"}
MC/DC0%0/1
$parsed->{"subcommand"} // ""
$parsed->{"subcommand"}
Truth table50%1/2
A
A= $filter_spec->{"subcommand"}
Aresult
11
00
Truth table50%1/2
A
A= $parsed->{"subcommand"}
Aresult
11
00
121 }
122  
123 # All specified flags must be present and truthy
124 1 ▶ for my $flag (keys %{$filter_spec->{flags} // {}}) {
Condition50%1/2
$filter_spec->{"flags"} // {}
l!l
10
MC/DC0%0/1
$filter_spec->{"flags"} // {}
$filter_spec->{"flags"}
Truth table50%1/2
A
A= $filter_spec->{"flags"}
Aresult
11
00
125 1 ▶ return 0 unless $parsed->{flags}{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed->{"flags"}{$flag} 1 0 1
126 }
127  
128 0 return 1;
129 }
130  
131 sub _build_default_filters {
132 10 ▶ my $self = shift;
Subroutine _build_default_filters: called CC=1 SCAR=0.1
133  
134 # ls: ultra-compact for long listing - only keep type + filename
135 $self->register_filter(
136 command => '^ls\b',
137 output_detect => qr(^[d-][rwx-]{9}.*\s+\d+\s+),
138 strip_lines_matching => [
139 qr(^\s*$),
140 qr(^total\s+\d+),
141 qr(^\s*Device:),
142 qr(^\s*Inode:),
143 qr(^\s*Birth:),
144 qr(^/node_modules/),
145 qr(^/\.git/),
146 qr(^/\.target/),
147 qr(^/\.next/),
148 qr(^/\.nuxt/),
149 qr(^/\.cache/),
150 qr(^/__pycache__/),
151 qr(^/\.DS_Store/),
152 qr(^/vendor/bundle/),
153 ],
154 transform => sub {
155 9 ▶ my ($line) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
156 9 ▶ if ($line =~ m{^([d-])[rwx-]{9}\s+\d+\s+\S+\s+\S+\s+\d+\s+\w+\s+\d+\s+[\d:]+\s+(.+)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([d-])[rwx-]{9}\s+\d+\s+\S+\s+\S+\s+\d+\s+\w+\s+\d+\s+[\d:]+\s+(.+)$/u) 6 3 9
157 6 return $1 . " " . $2;
158 }
159 3 return $line;
160 },
161 10 truncate_lines_at => 100,
162 max_lines => 50,
163 );
164  
165 # stat: strip device/inode/birth (Linux)
166 10 $self->register_filter(
167 command => '^stat\b',
168 strip_lines_matching => [
169 qr(^\s*$),
170 qr(^\s*Device:),
171 qr(^\s*Inode:),
172 qr(^\s*Birth:),
173 ],
174 truncate_lines_at => 120,
175 max_lines => 20,
176 );
177  
178 # grep: truncate lines, group by file
179 10 $self->register_filter(
180 command => '^grep\b',
181 truncate_lines_at => 150,
182 max_lines => 100,
183 );
184  
185 # make: strip entering/leaving directory
186 10 $self->register_filter(
187 command => '^make\b',
188 strip_lines_matching => [
189 qr(^\s*$),
190 qr(^make\[\d+\]:),
191 qr(^Entering directory),
192 qr(^Leaving directory),
193 qr(Nothing to be done),
194 qr(^make:\s+Nothing to be done),
195 ],
196 match_output => [
197 { pattern => qr(^make\[\d+\]:.*?make\[\d+\]:), message => 'make: circular dependency detected' },
198 { pattern => qr(^gcc.*?Error), message => 'make: compilation error' },
199 ],
200 max_lines => 50,
201 on_empty => 'make: ok',
202 );
203  
204 # git status: compact output
205 10 $self->register_filter(
206 parsed_command => {
207 program => 'git',
208 subcommand => 'status',
209 },
210 strip_lines_matching => [
211 qr(^\s*$),
212 qr(^On branch),
213 qr(^Your branch is),
214 qr(^Initial commit),
215 ],
216 max_lines => 30,
217 );
218  
219 # git diff: keep only diff content
220 10 $self->register_filter(
221 command => '^git\s+diff\b',
222 strip_lines_matching => [
223 qr(^\s*$),
224 qr(^diff --git),
225 qr(^index ),
226 qr(^---\s+a/),
227 qr(^\+\+\+\s+b/),
228 ],
229 truncate_lines_at => 150,
230 max_lines => 200,
231 );
232  
233 # git diff --stat: compact format "N+M- filename"
234 $self->register_filter(
235 parsed_command => {
236 program => 'git',
237 subcommand => 'diff',
238 flags => { stat => 1 },
239 },
240 transform => sub {
241 0 ▶ my ($line) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
242 # Format: " 1 file changed, 3 insertions(+), 2 deletions(-)" or
243 # " file1.txt | 5 +++ ---"
244 # Transform to "N+M- filename" for file lines
245 0 ▶ if ($line =~ /^\s*(\S+)\s*\|\s*(\d+)\s*\+(\d+)\s*-\s*(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\S+)\s*\|\s*(\d+)\s*\+(\d+)\s*-\s*(\d+)/u) 0 0 0
246 # "| 5 +++ ---" format -> "3+2- filename"
247 0 return "$2+$3-$1";
248 }
249 0 ▶ if ($line =~ /^\s*(\S+)\s*\|\s*(\d+)\s+\+(\d+),?\s*(\d+)?\s*-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\S+)\s*\|\s*(\d+)\s+\+(\d+),?\s*(\d+)?\s*-/u) 0 0 0
250 # "| 5 +3 -2" format
251 0 ▶ my ($file, $changes, $add, $del) = ($1, $2, $3, $4 // 0);
Condition0%0/2
$4 // 0
l!l
00
MC/DC0%0/1
$4 // 0
$4
Truth table0%0/2
A
A= $4
Aresult
11
00
252 0 return "$add+$del-$file";
253 }
254 # Remove summary line "X files changed"
255 0 ▶ if ($line =~ /^\s*\d+\s+files?\s+changed/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*\d+\s+files?\s+changed/u) 0 0 0
256 0 return undef; # Skip this line
257 }
258 0 return $line;
259 },
260 10 strip_lines_matching => [
261 qr(^\s*$),
262 qr(^\s*\d+\s+files?\s+changed),
263 qr(^\s*\d+\s+insertions?\(\+\)),
264 qr(^\s*\d+\s+deletions?\(\-\)),
265 ],
266 max_lines => 100,
267 );
268  
269 # cat: detect and filter code
270 10 $self->register_filter(
271 command => '^cat\b',
272 strip_lines_matching => [qr(^\s*$)],
273 truncate_lines_at => 500,
274 max_lines => 100,
275 );
276  
277 # find: strip permission denied, limit results
278 10 $self->register_filter(
279 command => '^find\b',
280 strip_lines_matching => [
281 qr(^\s*$),
282 qr(^find:.*permission denied),
283 ],
284 max_lines => 50,
285 );
286  
287 # ps: compact output
288 10 $self->register_filter(
289 command => '^ps\b',
290 strip_lines_matching => [
291 qr(^\s*$),
292 ],
293 truncate_lines_at => 120,
294 max_lines => 30,
295 );
296  
297 # df: truncate wide columns
298 10 $self->register_filter(
299 command => '^df\b',
300 strip_lines_matching => [qr(^\s*$)],
301 truncate_lines_at => 80,
302 max_lines => 20,
303 );
304  
305 # docker ps: compact output
306 10 $self->register_filter(
307 parsed_command => {
308 program => 'docker',
309 subcommand => 'ps',
310 },
311 strip_lines_matching => [qr(^\s*$)],
312 truncate_lines_at => 120,
313 max_lines => 30,
314 );
315  
316 # docker images: compact output
317 10 $self->register_filter(
318 parsed_command => {
319 program => 'docker',
320 subcommand => 'images',
321 },
322 strip_lines_matching => [qr(^\s*$)],
323 truncate_lines_at => 120,
324 max_lines => 30,
325 );
326  
327 # terraform plan: strip refresh progress
328 10 $self->register_filter(
329 parsed_command => {
330 program => 'terraform',
331 subcommand => 'plan',
332 },
333 strip_lines_matching => [
334 qr(^\s*$),
335 qr(^Refreshing state\.\.\.),
336 qr(^Terraform used the),
337 qr(^tfe-outputs:),
338 ],
339 max_lines => 100,
340 );
341  
342 # terraform apply: strip progress
343 10 $self->register_filter(
344 parsed_command => {
345 program => 'terraform',
346 subcommand => 'apply',
347 },
348 strip_lines_matching => [
349 qr(^\s*$),
350 qr(^Refreshing state\.\.\.),
351 qr(^Terraform will perform),
352 qr(^Proceeding with the following),
353 qr(^tfe-outputs:),
354 ],
355 max_lines => 100,
356 );
357  
358 # docker build: strip build progress
359 10 $self->register_filter(
360 parsed_command => {
361 program => 'docker',
362 subcommand => 'build',
363 },
364 strip_lines_matching => [
365 qr(^\s*$),
366 qr(^#\s*\d+\s+\[\s*\d+\s+/\s*\d+\]),
367 qr(^Step \d+/\d+:),
368 ],
369 max_lines => 50,
370 );
371  
372 # docker run: compact output
373 10 $self->register_filter(
374 parsed_command => {
375 program => 'docker',
376 subcommand => 'run',
377 },
378 strip_lines_matching => [
379 qr(^\s*$),
380 qr(^Unable to find image),
381 qr(^Pulling from library/),
382 qr(^Digest:),
383 qr(^Status:),
384 ],
385 max_lines => 30,
386 );
387  
388 # kubectl get: compact output
389 10 $self->register_filter(
390 parsed_command => {
391 program => 'kubectl',
392 subcommand => 'get',
393 },
394 strip_lines_matching => [qr(^\s*$)],
395 truncate_lines_at => 150,
396 max_lines => 50,
397 );
398  
399 # kubectl describe: strip noise
400 10 $self->register_filter(
401 parsed_command => {
402 program => 'kubectl',
403 subcommand => 'describe',
404 },
405 strip_lines_matching => [
406 qr(^\s*$),
407 qr(^Name:\s+\w+),
408 qr(^Namespace:\s+\w+),
409 qr(^Labels:\s*$),
410 qr(^Annotations:\s*$/),
411 ],
412 truncate_lines_at => 200,
413 max_lines => 100,
414 );
415  
416 # cargo build: strip compile progress
417 10 $self->register_filter(
418 parsed_command => {
419 program => 'cargo',
420 subcommand => 'build',
421 },
422 strip_lines_matching => [
423 qr(^\s*$),
424 qr(^Compiling\s+\w+),
425 qr(^Fresh\s+\w+),
426 qr(^Finished\s+),
427 ],
428 max_lines => 50,
429 );
430  
431 # cargo test: compact output
432 10 $self->register_filter(
433 parsed_command => {
434 program => 'cargo',
435 subcommand => 'test',
436 },
437 strip_lines_matching => [
438 qr(^\s*$),
439 qr(^Compiling\s+\w+),
440 qr(^Running\s+/),
441 qr(^test result:),
442 ],
443 max_lines => 100,
444 );
445  
446 # cpanm: strip cpanm noise
447 10 $self->register_filter(
448 command => '^cpanm\b',
449 strip_lines_matching => [
450 qr(^\s*$),
451 qr(^--> ),
452 qr(^OK$),
453 qr(^FAIL$),
454 qr(^Working on),
455 qr(^Fetching),
456 qr(^Configuring),
457 qr(^Building and testing),
458 ],
459 match_output => [
460 { pattern => qr{^\s*Successfully installed}, message => 'cpanm: ok' },
461 ],
462 max_lines => 30,
463 );
464  
465 # npm install: strip noise
466 10 $self->register_filter(
467 parsed_command => {
468 program => 'npm',
469 subcommand => 'install',
470 },
471 strip_lines_matching => [
472 qr(^\s*$),
473 qr(^added\s+\d+\s+packages?),
474 qr(^found\s+\d+\s+packages?),
475 qr(^npm warn),
476 ],
477 max_lines => 30,
478 );
479  
480 # yarn install: similar to npm
481 10 $self->register_filter(
482 parsed_command => {
483 program => 'yarn',
484 subcommand => 'install',
485 },
486 strip_lines_matching => [
487 qr(^\s*$),
488 qr(^Done in\s+),
489 qr(^Resolving completed),
490 qr(^Linking completed),
491 ],
492 max_lines => 30,
493 );
494  
495 # yarn add: similar to npm
496 10 $self->register_filter(
497 parsed_command => {
498 program => 'yarn',
499 subcommand => 'add',
500 },
501 strip_lines_matching => [
502 qr(^\s*$),
503 qr(^Done in\s+),
504 qr(^Resolving completed),
505 qr(^Linking completed),
506 ],
507 max_lines => 30,
508 );
509  
510 # pnpm install: similar to npm
511 10 $self->register_filter(
512 parsed_command => {
513 program => 'pnpm',
514 subcommand => 'install',
515 },
516 strip_lines_matching => [
517 qr(^\s*$),
518 qr(^Done in\s+),
519 qr(^Resolving completed),
520 qr(^Linking completed),
521 ],
522 max_lines => 30,
523 );
524  
525 # pnpm add: similar to npm
526 10 $self->register_filter(
527 parsed_command => {
528 program => 'pnpm',
529 subcommand => 'add',
530 },
531 strip_lines_matching => [
532 qr(^\s*$),
533 qr(^Done in\s+),
534 qr(^Resolving completed),
535 qr(^Linking completed),
536 ],
537 max_lines => 30,
538 );
539  
540 # pip install: strip progress
541 10 $self->register_filter(
542 parsed_command => {
543 program => 'pip',
544 subcommand => 'install',
545 },
546 strip_lines_matching => [
547 qr(^\s*$),
548 qr(^Collecting\s+),
549 qr(^Downloading\s+),
550 qr(^Installing collected packages:),
551 qr(^Successfully installed),
552 ],
553 max_lines => 50,
554 );
555  
556 # pytest: compact output
557 10 $self->register_filter(
558 parsed_command => {
559 program => 'pytest',
560 },
561 strip_lines_matching => [
562 qr(^\s*$),
563 qr(^=+.*=+$/),
564 qr(^Coverage report:),
565 qr(^HTML report:),
566 ],
567 truncate_lines_at => 150,
568 max_lines => 100,
569 );
570  
571 # curl: strip headers
572 10 $self->register_filter(
573 parsed_command => {
574 program => 'curl',
575 },
576 filter_stderr => 1,
577 strip_lines_matching => [
578 qr(^\s*$),
579 qr(^ % Total),
580 qr(^ Resolving),
581 qr(^Connected to),
582 qr(^HTTP/\d[\d.]*\s+\d+),
583 ],
584 truncate_lines_at => 200,
585 max_lines => 50,
586 );
587  
588 # wget: strip progress
589 10 $self->register_filter(
590 parsed_command => {
591 program => 'wget',
592 },
593 strip_lines_matching => [
594 qr(^\s*$),
595 qr(^--\d{4}-\d{2}-\d{2}),
596 qr(^Resolving),
597 qr(^Connecting to),
598 qr(^Length:\s+\d+),
599 qr(^Saving to:),
600 qr(^\d+%\s+\[),
601 ],
602 truncate_lines_at => 200,
603 max_lines => 50,
604 );
605  
606 # helm install: strip progress
607 10 $self->register_filter(
608 parsed_command => {
609 program => 'helm',
610 subcommand => 'install',
611 },
612 strip_lines_matching => [
613 qr(^\s*$),
614 qr(^NAME:\s+\w+),
615 qr(^NAMESPACE:\s+\w+),
616 qr(^STATUS:\s+),
617 qr(^REVISION:\s+\d+),
618 qr(^NOTES:$),
619 ],
620 max_lines => 50,
621 );
622  
623 # helm upgrade: strip progress
624 10 $self->register_filter(
625 parsed_command => {
626 program => 'helm',
627 subcommand => 'upgrade',
628 },
629 strip_lines_matching => [
630 qr(^\s*$),
631 qr(^NAME:\s+\w+),
632 qr(^NAMESPACE:\s+\w+),
633 qr(^STATUS:\s+),
634 qr(^REVISION:\s+\d+),
635 qr(^NOTES:$),
636 ],
637 max_lines => 50,
638 );
639  
640 # ansible-playbook: strip progress
641 10 $self->register_filter(
642 parsed_command => {
643 program => 'ansible-playbook',
644 },
645 strip_lines_matching => [
646 qr(^\s*$),
647 qr(^PLAY\s+\[),
648 qr(^TASK\s+\[),
649 qr(^RUNNING HANDLER),
650 qr(^changed:\s+\[\d+\]),
651 qr(^ok:\s+\[\d+\]),
652 qr(^fatal:\s+\[\d+\]),
653 qr(^PLAY RECAP),
654 ],
655 max_lines => 100,
656 );
657  
658 # rsync: strip progress
659 10 $self->register_filter(
660 parsed_command => {
661 program => 'rsync',
662 },
663 strip_lines_matching => [
664 qr(^\s*$),
665 qr(^sent\s+\d+\s+bytes),
666 qr(^received\s+\d+\s+bytes),
667 qr(^total size is),
668 ],
669 max_lines => 30,
670 );
671  
672 # iptables -L: compact
673 10 $self->register_filter(
674 parsed_command => {
675 program => 'iptables',
676 },
677 strip_lines_matching => [qr(^\s*$)],
678 truncate_lines_at => 150,
679 max_lines => 50,
680 );
681  
682 # ping: strip progress
683 10 $self->register_filter(
684 parsed_command => {
685 program => 'ping',
686 },
687 strip_lines_matching => [
688 qr(^\s*$),
689 qr(^PING\s+),
690 qr(^64 bytes from),
691 qr(^---.*ping statistics---),
692 ],
693 max_lines => 20,
694 );
695  
696 # netstat: compact output for -tulpn
697 10 $self->register_filter(
698 parsed_command => {
699 program => 'netstat',
700 },
701 strip_lines_matching => [qr(^\s*$)],
702 truncate_lines_at => 150,
703 max_lines => 50,
704 );
705  
706 # ip addr: compact
707 10 $self->register_filter(
708 parsed_command => {
709 program => 'ip',
710 subcommand => 'addr',
711 },
712 strip_lines_matching => [qr(^\s*$)],
713 truncate_lines_at => 150,
714 max_lines => 50,
715 );
716  
717 # ip route: compact
718 10 $self->register_filter(
719 parsed_command => {
720 program => 'ip',
721 subcommand => 'route',
722 },
723 strip_lines_matching => [qr(^\s*$)],
724 max_lines => 30,
725 );
726  
727 # ip link: compact
728 10 $self->register_filter(
729 parsed_command => {
730 program => 'ip',
731 subcommand => 'link',
732 },
733 strip_lines_matching => [qr(^\s*$)],
734 max_lines => 30,
735 );
736  
737 # mount: strip noise
738 10 $self->register_filter(
739 parsed_command => {
740 program => 'mount',
741 },
742 strip_lines_matching => [qr(^\s*$)],
743 truncate_lines_at => 200,
744 max_lines => 50,
745 );
746  
747 # lsblk: compact block device listing
748 10 $self->register_filter(
749 parsed_command => {
750 program => 'lsblk',
751 },
752 strip_lines_matching => [qr(^\s*$)],
753 truncate_lines_at => 150,
754 max_lines => 50,
755 );
756  
757 # blkid: compact block device attributes
758 10 $self->register_filter(
759 parsed_command => {
760 program => 'blkid',
761 },
762 strip_lines_matching => [qr(^\s*$)],
763 truncate_lines_at => 200,
764 max_lines => 50,
765 );
766  
767 # git log: compact
768 10 $self->register_filter(
769 parsed_command => {
770 program => 'git',
771 subcommand => 'log',
772 },
773 strip_lines_matching => [
774 qr(^\s*$),
775 qr(^commit\s+[a-f0-9]+),
776 qr(^Author:\s+),
777 qr(^Date:\s+),
778 ],
779 head_lines => 20,
780 tail_lines => 10,
781 max_lines => 30,
782 );
783  
784 # git branch: compact
785 10 $self->register_filter(
786 parsed_command => {
787 program => 'git',
788 subcommand => 'branch',
789 },
790 strip_lines_matching => [qr(^\s*$)],
791 max_lines => 30,
792 );
793  
794 # git stash: compact
795 10 $self->register_filter(
796 parsed_command => {
797 program => 'git',
798 subcommand => 'stash',
799 },
800 strip_lines_matching => [qr(^\s*$)],
801 max_lines => 30,
802 );
803  
804 # git commit: transform Co-Authored-By. Match the raw string so we
805 # also catch compound commands like `cd foo && git add bar && git
806 # commit -m "..."`, where the parsed top-level program isn't `git`.
807 $self->register_filter(
808 command => '\bgit\b[^&|;]*?(?<![=])\bcommit\b',
809 command_transform => sub {
810 5 ▶ my ($cmd) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.2
811 5 ▶ my $model = $ENV{CO_AUTHORED_BY} || $ENV{ANTHROPIC_MODEL};
Condition33%1/3
$ENV{'CO_AUTHORED_BY'} || $ENV{'ANTHROPIC_MODEL'}
l!l&&r!l&&!r
050
MC/DC0%0/2
$ENV{'CO_AUTHORED_BY'} || $ENV{'ANTHROPIC_MODEL'}
$ENV{'CO_AUTHORED_BY'} $ENV{'ANTHROPIC_MODEL'}
Truth table33%1/3
A || B
A= $ENV{'CO_AUTHORED_BY'}B= $ENV{'ANTHROPIC_MODEL'}
ABresult
1X1
011
000
812 5 ▶ return $cmd unless $model;
Branch: 1 branch
BranchTrueFalseTotal
unless $model 0 5 5
813 5 ▶ if ($cmd =~ /Co-Authored-By:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /Co-Authored-By:/iu) { } 3 2 5
814 3 $cmd =~ s/Co-Authored-By: [^\n]+/Co-Authored-By: $model/g;
815 } else {
816 # Inject inside the closing quote, otherwise the Co-Authored-By
817 # line ends up as a separate shell command and `<model@host>`
818 # parses as a redirection, breaking the whole call.
819 2 $cmd =~ s/(")\s*$/\n\nCo-Authored-By: $model$1/m;
820 }
821 5 return $cmd;
822 },
823 10 );
824  
825 10 return;
826 }
827  
828 sub new {
829 10 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=1 SCAR=0.0
Pod documented
830 10 $self->_build_default_filters;
831 10 return $self;
832 }
833  
834 sub compress {
835 9 ▶ my ($self, $command, $stdout, $stderr) = @_;
Subroutine compress: called CC=39 SCAR=47.6
Pod undocumented
836  
837 9 my $matched_filter;
838 9 my $parsed = $self->_parse_command($command);
839  
840 9 for my $key (keys %{$self->filters}) {
841 225 my $filter = $self->filters->{$key};
842  
843 225 my $matches = 0;
844  
845 # Check parsed_command first if present
846 225 ▶ if (my $pc = $filter->{parsed_command}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $pc = $filter->{'parsed_command'}) { } 170 55 225
elsif ($command =~ /$key/u) { } 8 47 55
847 170 ▶ $matches = 1 if $self->_match_parsed_command($pc, $parsed);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_match_parsed_command($pc, $parsed) 0 170 170
848 }
849 # Fall back to regex matching for legacy filters
850 elsif ($command =~ /$key/) {
851 8 $matches = 1;
852 }
853  
854 225 ▶ next unless $matches;
Branch: 1 branch
BranchTrueFalseTotal
unless $matches 217 8 225
855  
856 # Check output_detect if present - only apply filter if output matches
857 8 ▶ if (my $detect = $filter->{output_detect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $detect = $filter->{"output_detect"}) 1 7 8
858 1 my @lines = split(/\n/, $stdout);
859 1 my $has_match = grep { /$detect/ } @lines;
860 1 ▶ unless ($has_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_match) 0 1 1
861 0 next; # Skip this filter, try next one
862 }
863 }
864  
865 8 $matched_filter = $filter;
866 8 last; # Found matching filter
867 }
868  
869 # Apply command_transform if any filter has it and env var allows it
870 9 ▶ if ($matched_filter && $matched_filter->{command_transform} && !$ENV{MCP_RUN_COMPRESS_NO_CO_AUTHORED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter and $matched_filter->{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}) 0 9 9
Condition50%3/6
$matched_filter and $matched_filter->{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
!ll&&!rl&&r
900
$matched_filter and $matched_filter->{"command_transform"}
!ll&&!rl&&r
180
MC/DC0%0/3
$matched_filter and $matched_filter->{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
$matched_filter $matched_filter->{"command_transform"} not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
Truth table50%2/4
A and B and not(C)
A= $matched_filterB= $matched_filter->{"command_transform"}C= not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
ABCresult
0XX0
10X0
1100
1111
871 0 $command = $matched_filter->{command_transform}->($command);
872 }
873  
874 9 ▶ if (!$matched_filter) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched_filter) 1 8 9
875 1 return ($stdout, $stderr);
876 }
877  
878 8 ▶ my ($out, $err) = ($stdout, $stderr // '');
Condition50%1/2
$stderr // ""
l!l
80
MC/DC0%0/1
$stderr // ""
$stderr
Truth table50%1/2
A
A= $stderr
Aresult
11
00
879  
880 # Filter stderr into stdout if configured
881 8 ▶ if ($matched_filter->{filter_stderr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter->{"filter_stderr"}) 0 8 8
882 0 ▶ $out .= "\n$err" if length $err;
Branch: 1 branch
BranchTrueFalseTotal
if length $err 0 0 0
883 0 $err = '';
884 }
885  
886 # Stage 1: Strip ANSI
887 8 ▶ if ($matched_filter->{strip_ansi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter->{"strip_ansi"}) 0 8 8
888 0 $out =~ s/\x1b\[[0-9;]*[a-zA-Z]//g;
889 0 $err =~ s/\x1b\[[0-9;]*[a-zA-Z]//g;
890 }
891  
892 # Stage 2: Match output short-circuit
893 8 for my $match (@{$matched_filter->{match_output}}) {
894 4 ▶ if ($out =~ /$match->{pattern}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /$match->{"pattern"}/u) 0 4 4
895 0 return ($match->{message}, $err);
896 }
897 }
898  
899 # Stage 3: Transform lines (if transform coderef provided)
900 8 ▶ if ($matched_filter->{transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter->{"transform"}) 1 7 8
901 1 $out = join("\n", map { $matched_filter->{transform}->($_) } split(/\n/, $out));
902 }
903  
904 # Stage 4: Strip lines matching
905 8 ▶ if (@{$matched_filter->{strip_lines_matching}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$matched_filter->{"strip_lines_matching"};}) 7 1 8
906 7 my @out_lines = split(/\n/, $out);
907 @out_lines = grep {
908 7 my $keep = 1;
909 100 for my $pattern (@{$matched_filter->{strip_lines_matching}}) {
910 317 ▶ if (/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern/u) 15 302 317
911 15 $keep = 0;
912 15 last;
913 }
914 }
915 100 $keep;
916 } @out_lines;
917 7 $out = join("\n", @out_lines);
918 }
919  
920 # Stage 5: Keep lines matching (if any)
921 8 ▶ if (@{$matched_filter->{keep_lines_matching}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$matched_filter->{"keep_lines_matching"};}) 0 8 8
922 0 my @out_lines = split(/\n/, $out);
923 @out_lines = grep {
924 0 my $keep = 0;
925 0 for my $pattern (@{$matched_filter->{keep_lines_matching}}) {
926 0 ▶ if (/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern/u) 0 0 0
927 0 $keep = 1;
928 0 last;
929 }
930 }
931 0 $keep;
932 } @out_lines;
933 0 $out = join("\n", @out_lines);
934 }
935  
936 # Stage 6: Truncate lines at N chars
937 8 ▶ if ($matched_filter->{truncate_lines_at} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter->{"truncate_lines_at"} > 0) 5 3 8
938 5 my $max = $matched_filter->{truncate_lines_at};
939 5 ▶ $out = join("\n", map { length $_ > $max ? substr($_, 0, $max) . '...' : $_ } split(/\n/, $out));
Branch: 1 branch
BranchTrueFalseTotal
length $_ > $max ? : 1 27 28
940 5 ▶ $err = join("\n", map { length $_ > $max ? substr($_, 0, $max) . '...' : $_ } split(/\n/, $err)) if length $err;
Branch: 2 branches
BranchTrueFalseTotal
if length $err 0 5 5
length $_ > $max ? : 0 0 0
941 }
942  
943 # Stage 7: Head/Tail lines
944 8 ▶ if ($matched_filter->{head_lines} > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($matched_filter->{'head_lines'} > 0) { } 0 8 8
elsif ($matched_filter->{'tail_lines'} > 0) { } 0 8 8
945 0 my @lines = split(/\n/, $out);
946 0 my $head = $matched_filter->{head_lines};
947 0 ▶ my $tail = $matched_filter->{tail_lines} // 0;
Condition0%0/2
$matched_filter->{"tail_lines"} // 0
l!l
00
MC/DC0%0/1
$matched_filter->{"tail_lines"} // 0
$matched_filter->{"tail_lines"}
Truth table0%0/2
A
A= $matched_filter->{"tail_lines"}
Aresult
11
00
948 0 my $omit = @lines - $head - $tail;
949 0 ▶ if ($omit > 0 && $tail > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($omit > 0 and $tail > 0) { } 0 0 0
elsif ($omit > 0) { } 0 0 0
Condition0%0/3
$omit > 0 and $tail > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$omit > 0 and $tail > 0
$omit > 0 $tail > 0
Truth table0%0/3
A and B
A= $omit > 0B= $tail > 0
ABresult
0X0
100
111
950 0 $out = (join("\n", @lines[0..$head-1])) . "\n... $omit lines omitted ...\n" . (join("\n", @lines[-$tail..-1]));
951 }
952 elsif ($omit > 0) {
953 0 $out = join("\n", @lines[0..min($head, @lines)-1]);
954 }
955 }
956 elsif ($matched_filter->{tail_lines} > 0) {
957 0 my @lines = split(/\n/, $out);
958 0 $out = join("\n", @lines[-min($matched_filter->{tail_lines}, @lines)..-1]);
959 }
960  
961 # Stage 8: Max lines
962 8 ▶ if ($matched_filter->{max_lines} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter->{"max_lines"} > 0) 8 0 8
963 8 my @out_lines = split(/\n/, $out);
964 8 ▶ if (@out_lines > $matched_filter->{max_lines}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out_lines > $matched_filter->{"max_lines"}) 1 7 8
965 1 $out = join("\n", @out_lines[0..$matched_filter->{max_lines}-1]) . "\n... " . (@out_lines - $matched_filter->{max_lines}) . " more lines ...";
966 }
967 }
968  
969 # Stage 9: On empty
970 8 ▶ if (!length trim($out) && $matched_filter->{on_empty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length trim($out) and $matched_filter->{"on_empty"}) 1 7 8
Condition66%2/3
not length trim($out) and $matched_filter->{"on_empty"}
!ll&&!rl&&r
701
MC/DC50%1/2
not length trim($out) and $matched_filter->{"on_empty"}
not length trim($out) $matched_filter->{"on_empty"}
Truth table66%2/3
not(A) and B
A= not length trim($out)B= $matched_filter->{"on_empty"}
ABresult
0X0
100
111
971 1 $out = $matched_filter->{on_empty};
972 }
973  
974 8 return ($out, $err);
975 }
976  
977 sub process {
978 0 ▶ my ($self, $command, $stdout, $stderr) = @_;
Subroutine process: not called CC=42 SCAR=75.0
Pod undocumented
979  
980 0 my $matched_filter;
981 0 my $parsed = $self->_parse_command($command);
982  
983 0 for my $key (keys %{$self->filters}) {
984 0 my $filter = $self->filters->{$key};
985  
986 0 my $matches = 0;
987  
988 0 ▶ if (my $pc = $filter->{parsed_command}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $pc = $filter->{'parsed_command'}) { } 0 0 0
elsif ($command =~ /$key/u) { } 0 0 0
989 0 ▶ $matches = 1 if $self->_match_parsed_command($pc, $parsed);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_match_parsed_command($pc, $parsed) 0 0 0
990 }
991 elsif ($command =~ /$key/) {
992 0 $matches = 1;
993 }
994  
995 0 ▶ next unless $matches;
Branch: 1 branch
BranchTrueFalseTotal
unless $matches 0 0 0
996  
997 0 ▶ if (my $detect = $filter->{output_detect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $detect = $filter->{"output_detect"}) 0 0 0
998 0 my @lines = split(/\n/, $stdout);
999 0 my $has_match = grep { /$detect/ } @lines;
1000 0 ▶ unless ($has_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_match) 0 0 0
1001 0 next;
1002 }
1003 }
1004  
1005 0 $matched_filter = $filter;
1006 0 last;
1007 }
1008  
1009 0 ▶ my ($out, $err) = ($stdout, $stderr // '');
Condition0%0/2
$stderr // ""
l!l
00
MC/DC0%0/1
$stderr // ""
$stderr
Truth table0%0/2
A
A= $stderr
Aresult
11
00
1010  
1011 0 ▶ if ($matched_filter && $matched_filter->{command_transform} && !$ENV{MCP_RUN_COMPRESS_NO_CO_AUTHORED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter and $matched_filter->{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}) 0 0 0
Condition0%0/6
$matched_filter and $matched_filter->{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
!ll&&!rl&&r
000
$matched_filter and $matched_filter->{"command_transform"}
!ll&&!rl&&r
000
MC/DC0%0/3
$matched_filter and $matched_filter->{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
$matched_filter $matched_filter->{"command_transform"} not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
Truth table0%0/4
A and B and not(C)
A= $matched_filterB= $matched_filter->{"command_transform"}C= not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
ABCresult
0XX0
10X0
1100
1111
1012 0 $command = $matched_filter->{command_transform}->($command);
1013 }
1014  
1015 0 ▶ if (!$matched_filter) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched_filter) 0 0 0
1016 0 return { command => $command, stdout => $out, stderr => $err };
1017 }
1018  
1019 0 ▶ if ($matched_filter->{filter_stderr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter->{"filter_stderr"}) 0 0 0
1020 0 ▶ $out .= "\n$err" if length $err;
Branch: 1 branch
BranchTrueFalseTotal
if length $err 0 0 0
1021 0 $err = '';
1022 }
1023  
1024 0 ▶ if ($matched_filter->{strip_ansi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter->{"strip_ansi"}) 0 0 0
1025 0 $out =~ s/\x1b\[[0-9;]*[a-zA-Z]//g;
1026 0 $err =~ s/\x1b\[[0-9;]*[a-zA-Z]//g;
1027 }
1028  
1029 0 ▶ for my $match (@{$matched_filter->{match_output} // []}) {
Condition0%0/2
$matched_filter->{"match_output"} // []
l!l
00
MC/DC0%0/1
$matched_filter->{"match_output"} // []
$matched_filter->{"match_output"}
Truth table0%0/2
A
A= $matched_filter->{"match_output"}
Aresult
11
00
1030 0 ▶ if ($out =~ /$match->{pattern}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /$match->{"pattern"}/u) 0 0 0
1031 0 return { command => $command, stdout => $match->{message}, stderr => $err };
1032 }
1033 }
1034  
1035 0 ▶ if ($matched_filter->{transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter->{"transform"}) 0 0 0
1036 0 $out = join("\n", map { $matched_filter->{transform}->($_) } split(/\n/, $out));
1037 }
1038  
1039 0 ▶ if (@{$matched_filter->{strip_lines_matching} // []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$matched_filter->{"strip_lines_matching"} // [];}) 0 0 0
Condition0%0/2
$matched_filter->{"strip_lines_matching"} // []
l!l
00
MC/DC0%0/1
$matched_filter->{"strip_lines_matching"} // []
$matched_filter->{"strip_lines_matching"}
Truth table0%0/2
A
A= $matched_filter->{"strip_lines_matching"}
Aresult
11
00
1040 0 my @out_lines = split(/\n/, $out);
1041 @out_lines = grep {
1042 0 my $keep = 1;
1043 0 for my $pattern (@{$matched_filter->{strip_lines_matching}}) {
1044 0 ▶ if (/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern/u) 0 0 0
1045 0 $keep = 0;
1046 0 last;
1047 }
1048 }
1049 0 $keep;
1050 } @out_lines;
1051 0 $out = join("\n", @out_lines);
1052 }
1053  
1054 0 ▶ if (@{$matched_filter->{keep_lines_matching} // []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$matched_filter->{"keep_lines_matching"} // [];}) 0 0 0
Condition0%0/2
$matched_filter->{"keep_lines_matching"} // []
l!l
00
MC/DC0%0/1
$matched_filter->{"keep_lines_matching"} // []
$matched_filter->{"keep_lines_matching"}
Truth table0%0/2
A
A= $matched_filter->{"keep_lines_matching"}
Aresult
11
00
1055 0 my @out_lines = split(/\n/, $out);
1056 @out_lines = grep {
1057 0 my $keep = 0;
1058 0 for my $pattern (@{$matched_filter->{keep_lines_matching}}) {
1059 0 ▶ if (/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern/u) 0 0 0
1060 0 $keep = 1;
1061 0 last;
1062 }
1063 }
1064 0 $keep;
1065 } @out_lines;
1066 0 $out = join("\n", @out_lines);
1067 }
1068  
1069 0 ▶ if ($matched_filter->{truncate_lines_at} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter->{"truncate_lines_at"} > 0) 0 0 0
1070 0 my $max = $matched_filter->{truncate_lines_at};
1071 0 ▶ $out = join("\n", map { length $_ > $max ? substr($_, 0, $max) . '...' : $_ } split(/\n/, $out));
Branch: 1 branch
BranchTrueFalseTotal
length $_ > $max ? : 0 0 0
1072 0 ▶ $err = join("\n", map { length $_ > $max ? substr($_, 0, $max) . '...' : $_ } split(/\n/, $err)) if length $err;
Branch: 2 branches
BranchTrueFalseTotal
if length $err 0 0 0
length $_ > $max ? : 0 0 0
1073 }
1074  
1075 0 ▶ if ($matched_filter->{head_lines} > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($matched_filter->{'head_lines'} > 0) { } 0 0 0
elsif ($matched_filter->{'tail_lines'} > 0) { } 0 0 0
1076 0 my @lines = split(/\n/, $out);
1077 0 my $head = $matched_filter->{head_lines};
1078 0 ▶ my $tail = $matched_filter->{tail_lines} // 0;
Condition0%0/2
$matched_filter->{"tail_lines"} // 0
l!l
00
MC/DC0%0/1
$matched_filter->{"tail_lines"} // 0
$matched_filter->{"tail_lines"}
Truth table0%0/2
A
A= $matched_filter->{"tail_lines"}
Aresult
11
00
1079 0 my $omit = @lines - $head - $tail;
1080 0 ▶ if ($omit > 0 && $tail > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($omit > 0 and $tail > 0) { } 0 0 0
elsif ($omit > 0) { } 0 0 0
Condition0%0/3
$omit > 0 and $tail > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$omit > 0 and $tail > 0
$omit > 0 $tail > 0
Truth table0%0/3
A and B
A= $omit > 0B= $tail > 0
ABresult
0X0
100
111
1081 0 $out = (join("\n", @lines[0..$head-1])) . "\n... $omit lines omitted ...\n" . (join("\n", @lines[-$tail..-1]));
1082 }
1083 elsif ($omit > 0) {
1084 0 $out = join("\n", @lines[0..min($head, @lines)-1]);
1085 }
1086 }
1087 elsif ($matched_filter->{tail_lines} > 0) {
1088 0 my @lines = split(/\n/, $out);
1089 0 $out = join("\n", @lines[-min($matched_filter->{tail_lines}, @lines)..-1]);
1090 }
1091  
1092 0 ▶ if ($matched_filter->{max_lines} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter->{"max_lines"} > 0) 0 0 0
1093 0 my @out_lines = split(/\n/, $out);
1094 0 ▶ if (@out_lines > $matched_filter->{max_lines}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out_lines > $matched_filter->{"max_lines"}) 0 0 0
1095 0 $out = join("\n", @out_lines[0..$matched_filter->{max_lines}-1]) . "\n... " . (@out_lines - $matched_filter->{max_lines}) . " more lines ...";
1096 }
1097 }
1098  
1099 0 ▶ if (!length trim($out) && $matched_filter->{on_empty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length trim($out) and $matched_filter->{"on_empty"}) 0 0 0
Condition0%0/3
not length trim($out) and $matched_filter->{"on_empty"}
!ll&&!rl&&r
000
MC/DC0%0/2
not length trim($out) and $matched_filter->{"on_empty"}
not length trim($out) $matched_filter->{"on_empty"}
Truth table0%0/3
not(A) and B
A= not length trim($out)B= $matched_filter->{"on_empty"}
ABresult
0X0
100
111
1100 0 $out = $matched_filter->{on_empty};
1101 }
1102  
1103 0 return { command => $command, stdout => $out, stderr => $err };
1104 }
1105  
1106 sub transform_command {
1107 8 ▶ my ($self, $command) = @_;
Subroutine transform_command: called CC=8 SCAR=20.8
Pod undocumented
1108 8 ▶ return $command if $ENV{MCP_RUN_COMPRESS_NO_CO_AUTHORED};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"} 1 7 8
1109  
1110 7 my $parsed = $self->_parse_command($command);
1111  
1112 7 for my $key (keys %{$self->filters}) {
1113 246 my $filter = $self->filters->{$key};
1114  
1115 246 my $matches = 0;
1116  
1117 246 ▶ if (my $pc = $filter->{parsed_command}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $pc = $filter->{'parsed_command'}) { } 184 62 246
elsif ($command =~ /$key/u) { } 6 56 62
1118 184 ▶ $matches = 1 if $self->_match_parsed_command($pc, $parsed);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_match_parsed_command($pc, $parsed) 0 184 184
1119 }
1120 elsif ($command =~ /$key/) {
1121 6 $matches = 1;
1122 }
1123  
1124 246 ▶ next unless $matches;
Branch: 1 branch
BranchTrueFalseTotal
unless $matches 240 6 246
1125  
1126 6 ▶ if ($filter->{command_transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter->{"command_transform"}) 5 1 6
1127 5 $command = $filter->{command_transform}->($command);
1128 5 last; # Only apply first matching command_transform
1129 }
1130 }
1131  
1132 7 return $command;
1133 }
1134  
1135 1 1;
1136  
1137 __END__
« blib/lib/MCP/Run/Compress.pm ↑ Summary blib/lib/MCP/Run.pm »