blib/lib/Term/Fabulous/Widget/Table/Style.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 89 CC 7 SCAR 33.0
CC
7
Cov
0%
style_hash
56.0
merge_styles
2.0
BEGIN
2.0
CRAP 27.2
1 package Term::Fabulous::Widget::Table::Style;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.05';
9  
10 use Exporter 'import';
11 our @EXPORT_OK = qw(style_hash merge_styles);
12  
13 use Term::Fabulous::Check qw(boolean border_style color);
14 use Term::Fabulous::Enum::BorderStyle;
15  
16 # What each kind of style may hold. Borders name the lines around a cell,
17 # a row or a column; 'column_lines' are the lines between the cells of a
18 # row, 'row_lines' those between the cells of a column.
19 my @LOOK = qw(text_color background_color bold italic underline border_color);
20 my %KEYS_OF = (
21 cell => [ @LOOK, qw(border_top border_right border_bottom border_left) ],
22 row => [ @LOOK, qw(border_top border_right border_bottom border_left column_lines) ],
23 column => [ @LOOK, qw(border_left border_right row_lines) ],
24 header => [@LOOK],
25 );
26  
27 my %KIND_OF_KEY = (
28 text_color => 'color',
29 background_color => 'color',
30 border_color => 'color',
31 bold => 'boolean',
32 italic => 'boolean',
33 underline => 'boolean',
34 map { $_ => 'border' } qw(border_top border_right border_bottom border_left column_lines row_lines),
35 );
36  
37 # A validated copy of a style hash of the given kind: colors as
38 # [r, g, b, a], booleans as 1 or 0, borders as BorderStyle items. Keys
39 # with an undef value are left out (they inherit).
40 sub style_hash ( $owner, $name, $kind, $style ) {
41 my $keys = $KEYS_OF{$kind} // die "Term::Fabulous::Widget::Table::Style: unknown style kind '$kind'";
42 return {} unless defined $style;
43 die( ( ref $owner || $owner ) . ": $name must be a hash reference, got " . ( ref $style ? ref($style) . ' reference' : "'$style'" ) )
44 unless ref $style eq 'HASH';
45 my %allowed = map { $_ => 1 } @$keys;
46 my @unknown = grep { !$allowed{$_} } sort keys %$style;
47 die( ( ref $owner || $owner ) . ": $name does not know @unknown (known: " . join( ', ', sort @$keys ) . ")" ) if @unknown;
48  
49 my %copy;
50 foreach my $key ( grep { defined $style->{$_} } sort keys %$style ) {
51 my $kind_of_key = $KIND_OF_KEY{$key};
52 $copy{$key}
53 = $kind_of_key eq 'color' ? color( $owner, "$name $key", $style->{$key} )
54 : $kind_of_key eq 'boolean' ? boolean( $owner, "$name $key", $style->{$key} )
55 : border_style( $owner, "$name $key", $style->{$key}, none => Term::Fabulous::Enum::BorderStyle->Hidden );
56 }
57 return \%copy;
58 }
59  
60 # The first defined value of each key, from the most specific style to
61 # the least.
62 sub merge_styles (@styles) {
63 my %merged;
64 foreach my $style ( reverse grep { defined } @styles ) {
65 $merged{$_} = $style->{$_} foreach keys %$style;
66 }
67 return \%merged;
68 }
69  
70 1;
71  
72 __END__