blib/lib/Term/Fabulous/Text/Style.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/Text/Marks.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Series.pm »
1 package Term::Fabulous::Text::Style;
2  
3 3 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.05';
9  
10 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 our @EXPORT_OK = qw(parse_style style compose_styles apply_style resolve_style);
12  
13 3 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶use Term::Fabulous::Check qw(color describe non_negative_integer optional);
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use Term::Fabulous::Color;
16 use Term::Fabulous::Termbox qw(TB_BOLD TB_ITALIC TB_UNDERLINE TB_REVERSE TB_DIM TB_BLINK TB_STRIKEOUT TB_OVERLINE TB_INVISIBLE);
17 use Term::Fabulous::Theme ();
18  
19 my %BIT_BY_WORD = (
20 bold => TB_BOLD,
21 italic => TB_ITALIC,
22 underline => TB_UNDERLINE,
23 reverse => TB_REVERSE,
24 dim => TB_DIM,
25 blink => TB_BLINK,
26 strike => TB_STRIKEOUT,
27 strikethrough => TB_STRIKEOUT,
28 overline => TB_OVERLINE,
29 conceal => TB_INVISIBLE,
30 );
31 my $KNOWN_WORDS = join ', ', sort( keys %BIT_BY_WORD ), 'not WORD', 'on COLOR', 'default';
32  
33 # Alpha 0 is the terminal's own color everywhere in Term::Fabulous.
34 my $DEFAULT_COLOR = [ 0, 0, 0, 0 ];
35  
36 my @STYLE_KEYS = qw(set clear color background);
37  
38 sub _fail ( $what, $style ) {
39 die "Term::Fabulous::Text::Style: $what in '$style' (known: $KNOWN_WORDS, any color, and the palette tokens of the theme)";
40 }
41  
42 # Whether a word names a color of the theme's palette, which the widget
43 # that paints the style turns into a color (see resolve_style).
44 sub _is_token ($word) {
45 return defined $word && !ref $word && Term::Fabulous::Theme::has_token($word) ? 1 : 0;
46 }
47  
48 # The [r, g, b, a] a word of a style string names (any string
49 # Term::Fabulous::Color reads, web color names included), the word
50 # itself when it is a palette token, or undef when it is neither.
51 sub _color_of_word ($word) { ## no critic (Subroutines::RequireFinalReturn) PPI does not parse try/catch
52 return $DEFAULT_COLOR if $word eq 'default';
53 return $word if _is_token($word);
54  
55 my $parsed;
56 try {
57 $parsed = [ Term::Fabulous::Color->new( color => $word )->to_rgba ];
58 }
59 catch ($error) {
60 $parsed = undef; # not a color either
61 }
62 return $parsed;
63 }
64  
65 # A color of a style hash: a color, or a palette token kept as the word.
66 sub _color_or_token ( $owner, $name, $value ) {
67 return $value if _is_token($value);
68 return color( $owner, $name, $value );
69 }
70  
71 # The style with its palette tokens replaced by the colors the theme
72 # gives them; a style without tokens comes back as it is.
73 sub resolve_style ( $style, $theme ) {
74 return $style unless grep { defined $_ && !ref $_ } @{$style}{qw(color background)};
75 return { %$style, map { $_ => ( ref $style->{$_} || !defined $style->{$_} ? $style->{$_} : $theme->token( $style->{$_} ) ) } qw(color background) };
76 }
77  
78 sub parse_style ($string) {
79 die 'Term::Fabulous::Text::Style: a style is a string' if !defined $string || ref $string;
80  
81 my $style = _neutral();
82 my @words = $string =~ /\w+\([^)]*\)|\S+/g; # rgb(1, 2, 3) is one word
83 while (@words) {
84 my $word = shift @words;
85 my %word_style
86 = $word eq 'not' ? ( clear => $BIT_BY_WORD{ shift(@words) // '' } // _fail( "'not' needs a style word after it", $string ) )
87 : $word eq 'on' ? ( background => _color_of_word( shift(@words) // '' ) // _fail( "'on' needs a color after it", $string ) )
88 : $BIT_BY_WORD{$word} ? ( set => $BIT_BY_WORD{$word} )
89 : ( color => _color_of_word($word) // _fail( "unknown style word '$word'", $string ) );
90 $style = _compose( $style, { %{ _neutral() }, %word_style } );
91 }
92 return $style;
93 }
94  
95 sub _neutral () {
96 return { set => 0, clear => 0, color => undef, background => undef };
97 }
98  
99 # The style $later stacked on $earlier: later bits win over earlier ones
100 # (set after clear sets, clear after set clears), a later color replaces
101 # an earlier one.
102 sub _compose ( $earlier, $later ) {
103 return {
104 set => ( $earlier->{set} & ~$later->{clear} ) | $later->{set},
105 clear => ( $earlier->{clear} & ~$later->{set} ) | $later->{clear},
106 color => $later->{color} // $earlier->{color},
107 background => $later->{background} // $earlier->{background},
108 };
109 }
110  
111 sub style ($value) {
112 return parse_style($value) unless ref $value;
113 die "Term::Fabulous::Text::Style: a style is a string or a hash reference, not $value" unless ref $value eq 'HASH';
114  
115 my @unknown = sort grep { !( $_ eq 'set' || $_ eq 'clear' || $_ eq 'color' || $_ eq 'background' ) } keys %$value;
116 die "Term::Fabulous::Text::Style: unknown style key(s) @unknown (known: " . join( ', ', @STYLE_KEYS ) . ')' if @unknown;
117  
118 my $owner = __PACKAGE__;
119 return {
120 set => non_negative_integer( $owner, set => $value->{set} // 0 ),
121 clear => non_negative_integer( $owner, clear => $value->{clear} // 0 ),
122 color => optional( \&_color_or_token, $owner, color => $value->{color} ),
123 background => optional( \&_color_or_token, $owner, background => $value->{background} ),
124 };
125 }
126  
127 sub compose_styles (@styles) {
128 my $composed = _neutral();
129 $composed = _compose( $composed, $_ ) foreach @styles;
130 return $composed;
131 }
132  
133 # The look's bits are a style that sets them and clears nothing, so
134 # applying is composing.
135 sub apply_style ( $look, $style ) {
136 my $applied = _compose( { set => $look->{attrs}, clear => 0, color => $look->{color}, background => $look->{background} }, $style );
137 return { attrs => $applied->{set}, color => $applied->{color}, background => $applied->{background} };
138 }
139  
140 1;
141  
142 __END__
« blib/lib/Term/Fabulous/Text/Marks.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Series.pm »