File Coverage

blib/lib/Sieve/Generator/Lines/PrettyCommand.pm
Criterion Covered Total %
statement 35 35 100.0
branch 9 10 90.0
condition 1 2 50.0
subroutine 7 7 100.0
pod 2 3 66.6
total 54 57 94.7


line stmt bran cond sub pod time code
1 1     1   1815 use v5.36.0;
  1         5  
2             package Sieve::Generator::Lines::PrettyCommand 0.001;
3             # ABSTRACT: a Sieve command statement with arguments aligned across multiple lines
4              
5 1     1   6 use Moo;
  1         2  
  1         8  
6             with 'Sieve::Generator::Lines';
7              
8 1     1   490 use Params::Util qw(_ARRAY0);
  1         2  
  1         504  
9              
10             #pod =head1 DESCRIPTION
11             #pod
12             #pod A C is like a L, but renders
13             #pod its arguments in groups, with each group on its own line and arguments within
14             #pod a group aligned to the column after the command identifier. This is useful
15             #pod for commands with many tagged arguments, such as C.
16             #pod
17             #pod =attr identifier
18             #pod
19             #pod This attribute holds the name of the Sieve command.
20             #pod
21             #pod =cut
22              
23             has identifier => (is => 'ro', required => 1);
24              
25             #pod =attr arg_groups
26             #pod
27             #pod This attribute holds the list of argument groups. Each group is either an
28             #pod arrayref of arguments (rendered together on one line) or a single argument.
29             #pod Groups are rendered on successive lines, aligned after the command name.
30             #pod
31             #pod =cut
32              
33             has _arg_groups => (is => 'ro', required => 1, init_arg => 'arg_groups');
34 2     2 1 9 sub arg_groups { $_[0]->_arg_groups->@* }
35              
36             #pod =method args
37             #pod
38             #pod my @args = $cmd->args;
39             #pod
40             #pod This method returns the flat list of all arguments, with arrayref groups
41             #pod expanded in place.
42             #pod
43             #pod =cut
44              
45 1     1 1 90 sub args ($self) {
  1         2  
  1         3  
46 1 100       4 return map {; _ARRAY0($_) ? @$_ : $_ } $self->arg_groups;
  2         15  
47             }
48              
49 1     1 0 3 sub as_sieve ($self, $i = undef) {
  1         2  
  1         2  
  1         1  
50 1   50     5 my $indent = q{ } x ($i // 0);
51 1         5 my $indent2 = q{ } x (1 + length $self->identifier);
52              
53 1         4 my $str = $indent . $self->identifier;
54 1         1 my $n = 0;
55              
56 1         4 my @queue = $self->arg_groups;
57 1         4 while (@queue) {
58 5         9 my @next = shift @queue;
59 5 50       19 @next = $next[0]->@* if _ARRAY0($next[0]);
60              
61 5 100       11 my $hunk = join q{ }, map {; ref ? $_->as_sieve(0) : $_ } @next;
  10         27  
62 5 100       12 $hunk .= ";" unless @queue;
63 5         10 $hunk .= "\n";
64              
65 5 100       21 $str .= $n++ ? "$indent$indent2$hunk" : " $hunk";
66             }
67              
68 1         5 return $str;
69             }
70              
71 1     1   9 no Moo;
  1         1  
  1         26  
72             1;
73              
74             __END__