blib/lib/Term/Fabulous/Widget/Text.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/Widget/Canvas.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Palette.pm »
1 package Term::Fabulous::Widget::Text;
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 1 ▶use Clay::UI::Role::Events::Emitter;
Subroutine BEGIN: called CC=1 SCAR=2.6
11  
12 our $VERSION = '0.02';
13  
14 class Term::Fabulous::Widget::Text
15 :isa(Term::Fabulous::Widget::TextNode)
16 :does(Term::Fabulous::Role::CanParseLayout)
17 :does(Term::Fabulous::Role::Themed)
18 :does(Clay::UI::Role::Events::Emitter)
19 :strict(params)
20 {
21 use Clay::XS qw(CLAY_TEXT_WRAP_WORDS CLAY_TEXT_WRAP_NEWLINES CLAY_TEXT_WRAP_NONE CLAY_TEXT_ALIGN_LEFT CLAY_TEXT_ALIGN_CENTER CLAY_TEXT_ALIGN_RIGHT);
22 use Clay::UI::Revision qw(bump_revision);
23 use Term::Fabulous::Check qw(boolean class_names color non_negative_integer one_of);
24 use Term::Fabulous::Event::TextClick;
25 use Term::Fabulous::Termbox qw(TB_BOLD TB_ITALIC TB_UNDERLINE);
26  
27 my %WRAP_MODE_BY_NAME = ( words => CLAY_TEXT_WRAP_WORDS, newlines => CLAY_TEXT_WRAP_NEWLINES, none => CLAY_TEXT_WRAP_NONE );
28 my %TEXT_ALIGNMENT_BY_NAME = ( left => CLAY_TEXT_ALIGN_LEFT, center => CLAY_TEXT_ALIGN_CENTER, right => CLAY_TEXT_ALIGN_RIGHT );
29  
30 field $id :param :reader = undef;
31 field $bold :param = 0;
32 field $italic :param = 0;
33 field $underline :param = 0;
34 field $classes :param = [];
35  
36 # Whether the program gave a text_color (BUILDARGS says so); without
37 # one the theme's text color is drawn. The Clay::UI::Text field always
38 # holds a color; the role knows whether it is an explicit one.
39 field $_has_text_color :param(_has_text_color) = 0;
40  
41 ADJUST {
42 $bold = boolean( $self, bold => $bold );
43 $italic = boolean( $self, italic => $italic );
44 $underline = boolean( $self, underline => $underline );
45 $classes = class_names( $self, classes => $classes );
46 $self->set_look( text_color => $self->SUPER::text_color ) if $_has_text_color;
47 }
48  
49 method _set_style ( $name, $field_ref, @new ) {
50 return $$field_ref unless @new;
51 $$field_ref = boolean( $self, $name => $new[0] );
52 bump_revision();
53 return $$field_ref;
54 }
55  
56 method bold (@new) { return $self->_set_style( bold => \$bold, @new ) }
57 method italic (@new) { return $self->_set_style( italic => \$italic, @new ) }
58 method underline (@new) { return $self->_set_style( underline => \$underline, @new ) }
59  
60 # The termbox2 style bits the renderer adds to the text color.
61 method style_attrs () {
62 return ( $bold ? TB_BOLD : 0 ) | ( $italic ? TB_ITALIC : 0 ) | ( $underline ? TB_UNDERLINE : 0 );
63 }
64  
65 # Clay::UI validates text_color before any ADJUST of this class runs,
66 # so it is converted while the arguments are still a plain list.
67 sub BUILDARGS ( $class, %params ) {
68 return %params unless defined $params{text_color};
69 $params{text_color} = color( $class, text_color => $params{text_color} );
70 $params{_has_text_color} = 1;
71 return %params;
72 }
73  
74 # The value a name stands for in %$value_by_name; an unknown name dies
75 # with the known ones.
76 sub _named ( $what, $value_by_name, $name ) {
77 return $value_by_name->{ one_of( 'Term::Fabulous::Widget::Text', $what, $name, keys %$value_by_name ) };
78 }
79  
80 # The reader returns the color the text is drawn in: the explicit one,
81 # or the theme's. undef is not a color; reset_look returns to the theme.
82 method text_color :override (@new) {
83 return $self->_explicit_text_color // $self->look('color') unless @new;
84 return $self->set_look( text_color => $self->SUPER::text_color( color( $self, text_color => $new[0] ) ) );
85 }
86  
87 method _explicit_text_color () {
88 return $self->has_look_override('text_color') ? $self->SUPER::text_color : undef;
89 }
90  
91 # ---------------------------------------------------------------------
92 # The theme (Term::Fabulous::Role::Themed)
93 # ---------------------------------------------------------------------
94  
95 method theme_family :common () {
96 return 'text';
97 }
98  
99 method themed_params :common () {
100 return ( text_color => [ 'color', 'normal' ] );
101 }
102  
103 method look_state () {
104 return 'normal';
105 }
106  
107 method look_reset ($name) {
108 return;
109 }
110  
111 method classes (@new) {
112 return [@$classes] unless @new;
113 $classes = class_names( $self, classes => $new[0] );
114 $self->forget_looks;
115 $self->mark_changed;
116 return [@$classes];
117 }
118  
119 # A text that joins or leaves a tree may be in another UI, with
120 # another theme, from now on.
121 method tree_changed :override () {
122 $self->SUPER::tree_changed;
123 $self->_forget_own_looks;
124 return;
125 }
126  
127 # The color the text is drawn in. The nearest ancestor that colors the
128 # text inside it (a Button, say) decides, given the explicit color if
129 # there is one; otherwise the explicit color, or the theme's.
130 method text_config :override () {
131 my $config = $self->SUPER::text_config;
132 my $explicit = $self->_explicit_text_color;
133 for ( my $node = $self->parent; defined $node; $node = $node->parent ) {
134 next unless $node->can('child_text_color');
135 my $color = $node->child_text_color($explicit) // last;
136 return { %$config, text_color => $color };
137 }
138 return { %$config, text_color => $explicit // $self->look('color') };
139 }
140  
141 # ---------------------------------------------------------------------
142 # Clicks
143 # ---------------------------------------------------------------------
144  
145 method click_at ( $offset, $button, $x, $y ) {
146 $offset = non_negative_integer( $self, offset => $offset );
147 my $text = $self->text;
148 die ref($self) . ": offset $offset lies past the text (" . length($text) . " characters)" if $offset >= length $text;
149 $self->fire_event(
150 Term::Fabulous::Event::TextClick->new(
151 button => $button,
152 x => $x,
153 y => $y,
154 offset => $offset,
155 _word_around( $text, $offset ),
156 $self->_click_details($offset),
157 )
158 );
159 return $self;
160 }
161  
162 # The run of non-blank characters around the offset, as the word,
163 # word_start and word_end parameters of a TextClick; none on a blank.
164 sub _word_around ( $text, $offset ) {
165 return () if substr( $text, $offset, 1 ) =~ /\s/;
166 my ($head) = substr( $text, 0, $offset ) =~ /(\S*)\z/;
167 my ($tail) = substr( $text, $offset ) =~ /\A(\S*)/;
168 return ( word => $head . $tail, word_start => $offset - length $head, word_end => $offset + length $tail );
169 }
170  
171 # What a subclass adds to a TextClick at the offset: a RichText its
172 # spans and its link there.
173 method _click_details ($offset) {
174 return ();
175 }
176  
177 method layout_properties :common () {
178 return (
179 font_id => 'scalar',
180 font_size => 'scalar',
181 letter_spacing => 'scalar',
182 line_height => 'scalar',
183 text_color => 'color',
184 classes => \&_parse_classes,
185 bold => 'boolean',
186 italic => 'boolean',
187 underline => 'boolean',
188 text => \&_parse_text,
189 wrap_mode => \&_parse_wrap_mode,
190 text_alignment => \&_parse_text_alignment,
191 );
192 }
193  
194 method _parse_classes ($kid) {
195 $self->classes( $self->kdl_strings($kid) );
196 return;
197 }
198  
199 method _parse_text ($kid) {
200 my $text = $self->kdl_argument($kid);
201 die "Term::Fabulous::Widget::Text: 'text' needs a string argument" unless $text->is_string;
202 $self->text( $text->value );
203 return;
204 }
205  
206 method _parse_wrap_mode ($kid) {
207 $self->wrap_mode( _named( wrap_mode => \%WRAP_MODE_BY_NAME, $self->kdl_argument($kid)->as_perl ) );
208 return;
209 }
210  
211 method _parse_text_alignment ($kid) {
212 $self->text_alignment( _named( text_alignment => \%TEXT_ALIGNMENT_BY_NAME, $self->kdl_argument($kid)->as_perl ) );
213 return;
214 }
215 }
216  
217 1;
218  
219 __END__
« blib/lib/Term/Fabulous/Widget/Canvas.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Palette.pm »