blib/lib/Term/Fabulous/Widget/StatusBar.pm untested

filter: Statementstmt 0.0% 0 / 157 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 14 Podpod n/a totaltotal 0.0% 0 / 238 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::StatusBar;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 use Object::Pad 0.825;
9  
10 use Term::Fabulous::Widget::Display;
11  
12 our $VERSION = '0.05';
13  
14 class Term::Fabulous::Widget::StatusBar
15 :isa(Term::Fabulous::Widget::Display)
16 :strict(params)
17 {
18 use Clay::XS qw(sizing_grow);
19 use IO::Async::Loop;
20 use IO::Async::Timer::Countdown;
21 use List::Util qw(max sum0);
22 use Scalar::Util qw(weaken);
23 use Term::Fabulous::Check qw(describe number one_of string);
24 use Term::Fabulous::Unicode qw(fit_columns string_columns);
25  
26 use constant PART_GAP => 2; # columns between two parts
27  
28 # The kinds of a message, and the kinds a part may have besides them,
29 # each with the themed parameter that colors it.
30 my %COLOR_OF_MESSAGE_KIND = ( info => 'text_color', success => 'success_color', warning => 'warning_color', danger => 'danger_color' );
31 my %COLOR_OF_PART_KIND = ( %COLOR_OF_MESSAGE_KIND, dim => 'dim_color', accent => 'accent_color' );
32  
33 field $message :param = '';
34 field $kind :param = 'info';
35 field $timeout :param = undef;
36  
37 field $_parts; # [ [ text, kind ], ... ], or a code reference that returns parts
38 field @_shown_parts; # the parts the next paint draws, read by paint_key
39 field $_timer;
40  
41 ADJUST :params ( :$parts = [] ) {
42 $message = _one_row( string( $self, message => $message ) );
43 $kind = one_of( $self, kind => $kind, keys %COLOR_OF_MESSAGE_KIND );
44 $timeout = $self->_checked_timeout($timeout);
45 $_parts = $self->_checked_parts($parts);
46 }
47  
48 method theme_family :common () {
49 return 'status_bar';
50 }
51  
52 method themed_params :common () {
53 return (
54 $class->SUPER::themed_params,
55 text_color => [ 'text', 'normal', 'cell_color' ],
56 dim_color => [ 'dim', 'normal', 'cell_color' ],
57 accent_color => [ 'accent', 'normal', 'cell_color' ],
58 success_color => [ 'success', 'normal', 'cell_color' ],
59 warning_color => [ 'warning', 'normal', 'cell_color' ],
60 danger_color => [ 'danger', 'normal', 'cell_color' ],
61 );
62 }
63  
64 method layout_properties :common () {
65 return ( $class->SUPER::layout_properties, map { $_ => 'scalar' } qw(message kind timeout) );
66 }
67  
68 # A status bar is one row: line breaks and runs of white space become
69 # one space, and white space at the ends goes.
70 sub _one_row ($text) {
71 return $text =~ s/\s+/ /gr =~ s/\A | \z//gr;
72 }
73  
74 method _checked_timeout ($seconds) {
75 return undef unless defined $seconds;
76 $seconds = number( $self, timeout => $seconds );
77 die ref($self) . ": timeout must be positive, or undef for no timeout, got $seconds" unless $seconds > 0;
78 return $seconds;
79 }
80  
81 # The parts as given to new or parts: an array reference of parts, or
82 # a code reference that returns them whenever a frame is drawn.
83 method _checked_parts ($parts) {
84 return $parts if ref $parts eq 'CODE';
85 die ref($self) . ": parts must be an array reference or a code reference, got " . describe($parts) unless ref $parts eq 'ARRAY';
86 return [ map { $self->_checked_part($_) } @$parts ];
87 }
88  
89 # A part is a string, drawn like an info message, or [ text, kind ].
90 method _checked_part ($part) {
91 return [ _one_row($part), 'info' ] if defined $part && !ref $part;
92 die ref($self) . ": a part must be a string or [ text, kind ], got " . describe($part) unless ref $part eq 'ARRAY' && @$part == 2;
93 my ( $text, $part_kind ) = @$part;
94 return [ _one_row( string( $self, 'the text of a part' => $text ) ), one_of( $self, 'the kind of a part' => $part_kind, keys %COLOR_OF_PART_KIND ) ];
95 }
96  
97 # ---------------------------------------------------------------------
98 # The message
99 # ---------------------------------------------------------------------
100  
101 method message (@new) {
102 return $message unless @new;
103 my ( $text, $new_kind ) = @new;
104 $text = _one_row( string( $self, message => $text ) );
105 $new_kind = one_of( $self, kind => $new_kind // 'info', keys %COLOR_OF_MESSAGE_KIND );
106 if ( !length $text ) {
107 $self->clear_message;
108 return '';
109 }
110 ( $message, $kind ) = ( $text, $new_kind );
111 $self->_restart_timer;
112 $self->mark_changed;
113 return $message;
114 }
115  
116 method kind (@new) {
117 return $kind unless @new;
118 $kind = one_of( $self, kind => $new[0], keys %COLOR_OF_MESSAGE_KIND );
119 $self->mark_changed;
120 return $kind;
121 }
122  
123 method clear_message () {
124 $self->_stop_timer;
125 ( $message, $kind ) = ( '', 'info' );
126 $self->mark_changed;
127 return $self;
128 }
129  
130 method timeout (@new) {
131 return $timeout unless @new;
132 $timeout = $self->_checked_timeout( $new[0] );
133 $self->_restart_timer if length $message;
134 return $timeout;
135 }
136  
137 # The timeout runs on the process-wide IO::Async loop, which run
138 # drives; without a running loop (step in tests) the message stays.
139 method _restart_timer () {
140 $self->_stop_timer;
141 return unless defined $timeout;
142 weaken( my $weak_self = $self );
143 $_timer = IO::Async::Timer::Countdown->new( delay => $timeout, remove_on_expire => 1, on_expire => sub { $weak_self->clear_message if $weak_self; return } );
144 IO::Async::Loop->new->add($_timer);
145 $_timer->start;
146 return;
147 }
148  
149 method _stop_timer () {
150 return unless defined $_timer;
151 $_timer->stop if $_timer->is_running;
152 $_timer->remove_from_parent if defined $_timer->loop;
153 $_timer = undef;
154 return;
155 }
156  
157 # Lets the timeout run out now, as the loop would: for tests.
158 method expire () {
159 return $self unless defined $_timer;
160 return $self->clear_message;
161 }
162  
163 # ---------------------------------------------------------------------
164 # The parts on the right
165 # ---------------------------------------------------------------------
166  
167 method parts (@new) {
168 return ref $_parts eq 'CODE' ? $_parts : [ map { [@$_] } @$_parts ] unless @new;
169 $_parts = $self->_checked_parts( $new[0] );
170 $self->mark_changed;
171 return $self->parts;
172 }
173  
174 # The parts to draw now, checked: a code reference is called.
175 method current_parts () {
176 return map { [@$_] } @$_parts unless ref $_parts eq 'CODE';
177 return map { $self->_checked_part($_) } $_parts->();
178 }
179  
180 # ---------------------------------------------------------------------
181 # Colors
182 # ---------------------------------------------------------------------
183  
184 method text_color (@new) { return @new ? $self->set_look( text_color => $new[0] ) : $self->look_value('text_color') }
185 method dim_color (@new) { return @new ? $self->set_look( dim_color => $new[0] ) : $self->look_value('dim_color') }
186 method accent_color (@new) { return @new ? $self->set_look( accent_color => $new[0] ) : $self->look_value('accent_color') }
187 method success_color (@new) { return @new ? $self->set_look( success_color => $new[0] ) : $self->look_value('success_color') }
188 method warning_color (@new) { return @new ? $self->set_look( warning_color => $new[0] ) : $self->look_value('warning_color') }
189 method danger_color (@new) { return @new ? $self->set_look( danger_color => $new[0] ) : $self->look_value('danger_color') }
190  
191 method _attr_of_kind ($part_kind) {
192 return $self->color_attr( $self->look_value( $COLOR_OF_PART_KIND{$part_kind} ) );
193 }
194  
195 # ---------------------------------------------------------------------
196 # Painting
197 # ---------------------------------------------------------------------
198  
199 method natural_size () {
200 return ( sizing_grow(), 1 );
201 }
202  
203 # The parts are read here, once per frame, and painted from what was
204 # read.
205 method paint_key :override () {
206 @_shown_parts = $self->current_parts;
207 return ( $self->SUPER::paint_key, $message, $kind, map { @$_ } @_shown_parts );
208 }
209  
210 # The message from the left, a column in; the parts against the right
211 # edge, a column in, with a gap between them. A message that runs into
212 # the parts is cut and ends in an ellipsis.
213 method paint () {
214 my $columns = $self->columns;
215 my $parts_width = sum0( map { string_columns( $_->[0] ) } @_shown_parts ) + PART_GAP * max( 0, $#_shown_parts );
216 my $parts_start = max( 1, $columns - 1 - $parts_width );
217 my $x = $parts_start;
218 foreach my $part (@_shown_parts) {
219 $x = $self->paint_text( $x, 0, $part->[0], $self->_attr_of_kind( $part->[1] ), undef, $columns - 1 ) + PART_GAP;
220 }
221 my $room = ( @_shown_parts ? $parts_start - PART_GAP : $columns - 1 ) - 1;
222 $self->paint_text( 1, 0, fit_columns( $message, $room ), $self->_attr_of_kind($kind), undef );
223 return;
224 }
225 }
226  
227 1;
228  
229 __END__