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

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