blib/lib/Term/Fabulous/Document/Look.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/Check.pm ↑ Summary blib/lib/Term/Fabulous/OptionList.pm »
1 package Term::Fabulous::Document::Look;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 our $VERSION = '0.05';
11  
12 class Term::Fabulous::Document::Look :strict(params) {
13 1 ▶ use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶ use Term::Fabulous::Check qw(describe);
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use Term::Fabulous::Text::Style ();
16 use Term::Fabulous::Theme;
17  
18 # Every kind of styled text a document view shows, as the style words
19 # it sets and the palette tokens of its text color and background.
20 # A subclass adds kinds by overriding recipes.
21 method recipes :common () {
22 return (
23 bold => { words => 'bold' },
24 italic => { words => 'italic' },
25 strike => { words => 'strike' },
26 code => { color => 'warning' },
27 file => { words => 'italic', color => 'success' },
28 match => { background => 'selection' },
29 match_current => { color => 'text_inverse', background => 'warning' },
30 heading1 => { words => 'bold', color => 'accent' },
31 heading2 => { words => 'bold', color => 'text_bright' },
32 heading3 => { words => 'bold', color => 'text' },
33 heading4 => { words => 'bold italic', color => 'text' },
34 heading5 => { words => 'italic', color => 'text_bright' },
35 heading6 => { words => 'italic', color => 'text_muted' },
36 term => { words => 'bold', color => 'text_bright' },
37 marker => { color => 'accent' },
38 quote => { color => 'text_muted' },
39 code_block => { color => 'text', background => 'surface_low' },
40 clipped => { color => 'text_muted', background => 'surface_low' },
41 );
42 }
43  
44 field $theme :param :reader;
45 field %style_of;
46  
47 ADJUST {
48 die ref($self) . ": theme must be a Term::Fabulous::Theme, got " . describe($theme) unless blessed $theme && $theme->isa('Term::Fabulous::Theme');
49 my %recipe = __CLASS__->recipes;
50 %style_of = map { $_ => _style_of_recipe( $theme, $recipe{$_} ) } keys %recipe;
51 }
52  
53 sub _style_of_recipe ( $theme, $recipe ) {
54 my $style = Term::Fabulous::Text::Style::style( $recipe->{words} // {} );
55 $style->{color} = $theme->token( $recipe->{color} ) if defined $recipe->{color};
56 $style->{background} = $theme->token( $recipe->{background} ) if defined $recipe->{background};
57 return $style;
58 }
59  
60 method kinds :common () {
61 my %recipe = $class->recipes;
62 my @kinds = sort keys %recipe;
63 return @kinds;
64 }
65  
66 # The style of a kind with the palette tokens of its recipe as they
67 # are: a widget that paints it asks its own theme for the colors, so
68 # a theme switch needs no new style.
69 method token_style :common ($kind) {
70 my %recipe = $class->recipes;
71 my $recipe = $recipe{ $kind // '' } // die "$class: unknown kind " . describe($kind) . "; the kinds are " . join( ', ', $class->kinds );
72 my $style = Term::Fabulous::Text::Style::style( $recipe->{words} // {} );
73 $style->{color} = $recipe->{color} if defined $recipe->{color};
74 $style->{background} = $recipe->{background} if defined $recipe->{background};
75 return $style;
76 }
77  
78 method style ($kind) {
79 my $style = $style_of{ $kind // '' } // die ref($self) . ": unknown kind " . describe($kind) . "; the kinds are " . join( ', ', __CLASS__->kinds );
80 return { %$style, map { ref $style->{$_} ? ( $_ => [ @{ $style->{$_} } ] ) : () } keys %$style };
81 }
82  
83 # The look of the theme a widget draws with, as
84 # Term::Fabulous::Role::Themed decides (drawing_theme); the default
85 # theme for a widget that is not themed. The last look of each class is
86 # kept until the theme or the theme generation changes. The entry holds
87 # the theme itself, so that its address cannot be reused by another
88 # theme while the look is kept.
89 my %last_of_class; # class => [ look, theme, generation ]
90  
91 method of :common ($widget) {
92 my $theme = $widget->can('drawing_theme') ? $widget->drawing_theme : Term::Fabulous::Theme->default;
93 my $generation = Term::Fabulous::Theme::generation();
94 my $last = $last_of_class{$class};
95 return $last->[0] if defined $last && refaddr( $last->[1] ) == refaddr($theme) && $last->[2] == $generation;
96 my $look = $class->new( theme => $theme );
97 $last_of_class{$class} = [ $look, $theme, $generation ];
98 return $look;
99 }
100 }
101  
102 1;
103  
104 __END__
« blib/lib/Term/Fabulous/Check.pm ↑ Summary blib/lib/Term/Fabulous/OptionList.pm »