File Coverage

blib/lib/App/sdview/Style.pm
Criterion Covered Total %
statement 46 52 88.4
branch 5 14 35.7
condition n/a
subroutine 9 9 100.0
pod 0 3 0.0
total 60 78 76.9


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2023 -- leonerd@leonerd.org.uk
5              
6 3     3   231364 use v5.26;
  3         21  
7 3     3   16 use warnings;
  3         19  
  3         94  
8 3     3   471 use experimental 'signatures';
  3         3429  
  3         39  
9              
10             package App::sdview::Style 0.12;
11              
12 3     3   1984 use Convert::Color;
  3         104938  
  3         161  
13 3     3   1516 use Convert::Color::XTerm 0.06;
  3         9420  
  3         2872  
14              
15             =head1 NAME
16              
17             C - store formatting style information for C
18              
19             =head1 DESCRIPTION
20              
21             This module stores formatting style information for L text output
22             formatters, such a L or
23             L.
24              
25             =head2 Config File
26              
27             Style information can be overridden by the user, supplying a
28             L-style file at F<$HOME/.sdviewrc>. Formatting for each kind of
29             paragraph is provided in a section called C, and each individual
30             key gives formatting values.
31              
32             [Para head1]
33             bold = 0|1
34             italic = 0|1
35             monospace = 0|1
36             blank_after = 0|1
37             under = NUM
38             margin = NUM
39              
40             [Para head2]
41             ...
42              
43             The value for keys that set colours should be a string suitable for
44             L<< Convert::Color->new >>:
45              
46             [Para head1]
47             fg = vga:red
48             bg = xterm:184
49              
50             =cut
51              
52             my %FORMATSTYLES = (
53             B => { bold => 1 },
54             I => { italic => 1 },
55             F => { italic => 1, under => 1 },
56             C => { monospace => 1, bg => Convert::Color->new( "xterm:235" ) },
57             L => { under => 1, fg => Convert::Color->new( "xterm:rgb(3,3,5)" ) }, # light blue
58             );
59              
60 34         48 sub convert_str ( $pkg, $s )
61 34     34 0 48 {
  34         40  
  34         41  
62             return $s->clone(
63             convert_tags => {
64 34     7   93 ( map { $_ => do { my $k = $_; sub { $FORMATSTYLES{$k}->%* } } } keys %FORMATSTYLES ),
  170         224  
  170         201  
  170         520  
  7         610  
65             },
66             );
67             }
68              
69             my %PARASTYLES = (
70             head1 => { fg => Convert::Color->new( "vga:yellow" ), bold => 1 },
71             head2 => { fg => Convert::Color->new( "vga:cyan" ), bold => 1, margin => 2 },
72             head3 => { fg => Convert::Color->new( "vga:green" ), bold => 1, margin => 4 },
73             head4 => { fg => Convert::Color->new( "xterm:217" ), under => 1, margin => 5 },
74             plain => { margin => 6, blank_after => 1 },
75             verbatim => { margin => 8, blank_after => 1, $FORMATSTYLES{C}->%* },
76             list => { margin => 6 },
77             item => { blank_after => 1 },
78             leader => { bold => 1 },
79             table => { margin => 8 },
80             "table-heading" => { bold => 1 },
81             );
82              
83 1         2 sub load_config ( $pkg, $path )
84 1     1 0 7548 {
  1         2  
  1         2  
85 1         520 require Config::Tiny;
86              
87             # For unit testing, also accept a globref
88 1 50       1122 my $config = ( ref $path ) ? Config::Tiny->read_string( do { local $/; <$path> } )
  1         6  
  1         48  
89             : Config::Tiny->read( $path );
90              
91 1         74 foreach my $section ( sort keys %$config ) {
92 1 50       9 if( $section =~ m/^Para (.*)$/ ) {
93 1         2 my $para = $1;
94              
95 1 50       5 unless( $PARASTYLES{$para} ) {
96 0         0 warn "Unrecognised [Para $para] style in $path\n";
97 0         0 next;
98             }
99              
100 1         6 foreach my $stylekey ( sort keys $config->{$section}->%* ) {
101 1         10 my $val = $config->{$section}{$stylekey};
102 1 50       7 if( $stylekey =~ m/^(fg|bg)$/ ) {
    0          
    0          
103 1         5 $val = Convert::Color->new( $val );
104             }
105             elsif( $stylekey =~ m/^(bold|italic|monospace|blank_after)$/ ) {
106 0         0 $val = !!$val;
107             }
108             elsif( $stylekey =~ m/^(under|margin)$/ ) {
109 0         0 $val = 0+$val;
110             }
111             else {
112 0         0 warn "Unrecognised [Para $para] key $stylekey in $path\n";
113 0         0 next;
114             }
115              
116 1         73 $PARASTYLES{$para}{$stylekey} = $val;
117             }
118             }
119             }
120             }
121              
122 44         65 sub para_style ( $pkg, $type )
123 44     44 0 178 {
  44         69  
  44         61  
124 44 50       120 $PARASTYLES{$type} or
125             die "Unrecognised paragraph style for $type";
126              
127 44         198 return $PARASTYLES{$type};
128             }
129              
130             =head1 AUTHOR
131              
132             Paul Evans
133              
134             =cut
135              
136             0x55AA;