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/OptionList.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.03';
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);
12  
13 3 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Term::Fabulous::Check qw(color 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  
18 my %BIT_BY_WORD = (
19 bold => TB_BOLD,
20 italic => TB_ITALIC,
21 underline => TB_UNDERLINE,
22 reverse => TB_REVERSE,
23 dim => TB_DIM,
24 blink => TB_BLINK,
25 strike => TB_STRIKEOUT,
26 strikethrough => TB_STRIKEOUT,
27 overline => TB_OVERLINE,
28 conceal => TB_INVISIBLE,
29 );
30 my $KNOWN_WORDS = join ', ', sort( keys %BIT_BY_WORD ), 'not WORD', 'on COLOR', 'default';
31  
32 # Alpha 0 is the terminal's own color everywhere in Term::Fabulous.
33 my $DEFAULT_COLOR = [ 0, 0, 0, 0 ];
34  
35 my @STYLE_KEYS = qw(set clear color background);
36  
37 sub _fail ( $what, $style ) {
38 die "Term::Fabulous::Text::Style: $what in '$style' (known: $KNOWN_WORDS, and any color)";
39 }
40  
41 # The [r, g, b, a] a word of a style string names (any string
42 # Term::Fabulous::Color reads, web color names included), or undef when
43 # it is not a color.
44 sub _color_of_word ($word) { ## no critic (Subroutines::RequireFinalReturn) PPI does not parse try/catch
45 return $DEFAULT_COLOR if $word eq 'default';
46  
47 my $parsed;
48 try {
49 $parsed = [ Term::Fabulous::Color->new( color => $word )->to_rgba ];
50 }
51 catch ($error) {
52 $parsed = undef; # not a color either
53 }
54 return $parsed;
55 }
56  
57 sub parse_style ($string) {
58 die 'Term::Fabulous::Text::Style: a style is a string' if !defined $string || ref $string;
59  
60 my $style = _neutral();
61 my @words = $string =~ /\w+\([^)]*\)|\S+/g; # rgb(1, 2, 3) is one word
62 while (@words) {
63 my $word = shift @words;
64 my %word_style
65 = $word eq 'not' ? ( clear => $BIT_BY_WORD{ shift(@words) // '' } // _fail( "'not' needs a style word after it", $string ) )
66 : $word eq 'on' ? ( background => _color_of_word( shift(@words) // '' ) // _fail( "'on' needs a color after it", $string ) )
67 : $BIT_BY_WORD{$word} ? ( set => $BIT_BY_WORD{$word} )
68 : ( color => _color_of_word($word) // _fail( "unknown style word '$word'", $string ) );
69 $style = _compose( $style, { %{ _neutral() }, %word_style } );
70 }
71 return $style;
72 }
73  
74 sub _neutral () {
75 return { set => 0, clear => 0, color => undef, background => undef };
76 }
77  
78 # The style $later stacked on $earlier: later bits win over earlier ones
79 # (set after clear sets, clear after set clears), a later color replaces
80 # an earlier one.
81 sub _compose ( $earlier, $later ) {
82 return {
83 set => ( $earlier->{set} & ~$later->{clear} ) | $later->{set},
84 clear => ( $earlier->{clear} & ~$later->{set} ) | $later->{clear},
85 color => $later->{color} // $earlier->{color},
86 background => $later->{background} // $earlier->{background},
87 };
88 }
89  
90 sub style ($value) {
91 return parse_style($value) unless ref $value;
92 die "Term::Fabulous::Text::Style: a style is a string or a hash reference, not $value" unless ref $value eq 'HASH';
93  
94 my @unknown = sort grep { !( $_ eq 'set' || $_ eq 'clear' || $_ eq 'color' || $_ eq 'background' ) } keys %$value;
95 die "Term::Fabulous::Text::Style: unknown style key(s) @unknown (known: " . join( ', ', @STYLE_KEYS ) . ')' if @unknown;
96  
97 my $owner = __PACKAGE__;
98 return {
99 set => non_negative_integer( $owner, set => $value->{set} // 0 ),
100 clear => non_negative_integer( $owner, clear => $value->{clear} // 0 ),
101 color => optional( \&color, $owner, color => $value->{color} ),
102 background => optional( \&color, $owner, background => $value->{background} ),
103 };
104 }
105  
106 sub compose_styles (@styles) {
107 my $composed = _neutral();
108 $composed = _compose( $composed, $_ ) foreach @styles;
109 return $composed;
110 }
111  
112 # The look's bits are a style that sets them and clears nothing, so
113 # applying is composing.
114 sub apply_style ( $look, $style ) {
115 my $applied = _compose( { set => $look->{attrs}, clear => 0, color => $look->{color}, background => $look->{background} }, $style );
116 return { attrs => $applied->{set}, color => $applied->{color}, background => $applied->{background} };
117 }
118  
119 1;
120  
121 __END__
« blib/lib/Term/Fabulous/OptionList.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Series.pm »