« blib/lib/Term/Fabulous/Widget/PolarAreaChart.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/RadarChart.pm »
| 1 | package Term::Fabulous::Widget::ProgressBar; | ||
| 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::Role::HasRange; | ||
| 11 | use Term::Fabulous::Widget::Display; | ||
| 12 | |||
| 13 | our $VERSION = '0.05'; | ||
| 14 | |||
| 15 | class Term::Fabulous::Widget::ProgressBar | ||
| 16 | :isa(Term::Fabulous::Widget::Display) | ||
| 17 | :does(Term::Fabulous::Role::HasRange) | ||
| 18 | :strict(params) | ||
| 19 | { | ||
| 20 | use List::Util (); # max and min are methods here | ||
| 21 | use Term::Fabulous::Check qw(boolean cell_color describe number one_of optional positive_integer); | ||
| 22 | use Term::Fabulous::Range; | ||
| 23 | use Term::Fabulous::Unicode qw(grapheme_clusters cluster_columns string_columns); | ||
| 24 | |||
| 25 | use constant FULL_BLOCK => "\x{2588}"; | ||
| 26 | |||
| 27 | # How often the stripes move and the indeterminate runner steps. | ||
| 28 | use constant STRIPE_SECONDS => 0.15; | ||
| 29 | use constant RUNNER_SECONDS => 1 / 15; | ||
| 30 | |||
| 31 | # The left-aligned partial blocks, by eighths: one eighth (U+258F) to | ||
| 32 | # seven eighths (U+2589). | ||
| 33 | my @EIGHTH_BLOCK = map { chr( 0x2590 - $_ ) } 1 .. 7; | ||
| 34 | |||
| 35 | # The glyphs of the ready-made styles: [ fill, track, stripe ]. | ||
| 36 | my %GLYPHS_OF_STYLE = ( | ||
| 37 | block => [ FULL_BLOCK, "\x{2591}", "\x{2593}" ], | ||
| 38 | line => [ "\x{2501}", "\x{2500}", "\x{2505}" ], | ||
| 39 | ascii => [ '#', '-', '=' ], | ||
| 40 | ); | ||
| 41 | |||
| 42 | my @VALUE_POSITIONS = qw(left right inside); | ||
| 43 | |||
| 44 | field $indeterminate :param = 0; | ||
| 45 | field $show_value :param = 1; | ||
| 46 | field $value_position :param = 'right'; | ||
| 47 | field $preferred_columns :param = 20; | ||
| 48 | field $style :param = 'block'; | ||
| 49 | field $fill_glyph :param = undef; | ||
| 50 | field $track_glyph :param = undef; | ||
| 51 | field $stripe_glyph :param = undef; | ||
| 52 | field $fractional :param = 1; | ||
| 53 | field $striped :param = 0; | ||
| 54 | field $animated :param = 0; | ||
| 55 | field $separated :param = 0; | ||
| 56 | |||
| 57 | field $range; # a Term::Fabulous::Range without steps: min, max and the value | ||
| 58 | field @segments; # { value, color }, or empty for a single value | ||
| 59 | |||
| 60 | ADJUST :params ( :$min = 0, :$max = 100, :$value_format = undef, :$value = undef, :$segments = undef ) { | ||
| 61 | die ref($self) . ": give 'value' or 'segments', not both" | ||
| 62 | if defined $value && defined $segments; | ||
| 63 | $range = Term::Fabulous::Range->new( | ||
| 64 | owner => ref $self, | ||
| 65 | min => $min, | ||
| 66 | max => $max, | ||
| 67 | value_format => $value_format, | ||
| 68 | format_percent => 1, | ||
| 69 | default_format => sub ( $number, $range ) { sprintf '%.0f%%', 100 * $range->fraction_of($number) }, | ||
| 70 | ); | ||
| 71 | $indeterminate = boolean( $self, indeterminate => $indeterminate ); | ||
| 72 | $show_value = boolean( $self, show_value => $show_value ); | ||
| 73 | $value_position = one_of( $self, value_position => $value_position, @VALUE_POSITIONS ); | ||
| 74 | $preferred_columns = positive_integer( $self, preferred_columns => $preferred_columns ); | ||
| 75 | $style = one_of( $self, style => $style, keys %GLYPHS_OF_STYLE ); | ||
| 76 | $fill_glyph = $self->_checked_glyph( fill_glyph => $fill_glyph ); | ||
| 77 | $track_glyph = $self->_checked_glyph( track_glyph => $track_glyph ); | ||
| 78 | $stripe_glyph = $self->_checked_glyph( stripe_glyph => $stripe_glyph ); | ||
| 79 | $fractional = boolean( $self, fractional => $fractional ); | ||
| 80 | $striped = boolean( $self, striped => $striped ); | ||
| 81 | $animated = boolean( $self, animated => $animated ); | ||
| 82 | $separated = boolean( $self, separated => $separated ); | ||
| 83 | $self->value($value) if defined $value; | ||
| 84 | $self->segments($segments) if defined $segments; | ||
| 85 | } | ||
| 86 | |||
| 87 | method _checked_glyph ( $name, $value ) { | ||
| 88 | return optional( \&Term::Fabulous::Check::glyph, $self, $name => $value ); | ||
| 89 | } | ||
| 90 | |||
| 91 | # A segment is { value => ..., color => ... }; the color defaults to | ||
| 92 | # the bar's. | ||
| 93 | method _checked_segment ($segment) { | ||
| 94 | die ref($self) . ": a segment must be a hash reference with 'value' and an optional 'color', got " . describe($segment) unless ref $segment eq 'HASH'; | ||
| 95 | my @unknown = grep { $_ ne 'value' && $_ ne 'color' } sort keys %$segment; | ||
| 96 | die ref($self) . ": a segment takes only 'value' and 'color', got @unknown" if @unknown; | ||
| 97 | my $amount = number( $self, 'segment value' => $segment->{value} ); | ||
| 98 | die ref($self) . ": a segment value must not be negative, got $amount" if $amount < 0; | ||
| 99 | return { value => $amount, color => optional( \&cell_color, $self, 'segment color' => $segment->{color} ) }; | ||
| 100 | } | ||
| 101 | |||
| 102 | # --------------------------------------------------------------------- | ||
| 103 | # Value | ||
| 104 | # --------------------------------------------------------------------- | ||
| 105 | |||
| 106 | # The value of a bar with segments is their sum, from min on. | ||
| 107 | method value (@new) { | ||
| 108 | return @segments ? List::Util::min( $range->max, $range->min + List::Util::sum0( map { $_->{value} } @segments ) ) : $range->value unless @new; | ||
| 109 | $range->set_value( $new[0] ); | ||
| 110 | @segments = (); | ||
| 111 | $self->mark_changed; | ||
| 112 | return $range->value; | ||
| 113 | } | ||
| 114 | |||
| 115 | method fraction () { | ||
| 116 | return $range->fraction_of( $self->value ); | ||
| 117 | } | ||
| 118 | |||
| 119 | method percent () { | ||
| 120 | return 100 * $self->fraction; | ||
| 121 | } | ||
| 122 | |||
| 123 | method segments (@new) { | ||
| 124 | return [ map { +{%$_} } @segments ] unless @new; | ||
| 125 | my ($list) = @new; | ||
| 126 | if ( !defined $list ) { | ||
| 127 | @segments = (); | ||
| 128 | $self->mark_changed; | ||
| 129 | return []; | ||
| 130 | } | ||
| 131 | die ref($self) . ": segments must be an array reference, got " . describe($list) unless ref $list eq 'ARRAY'; | ||
| 132 | my @checked = map { $self->_checked_segment($_) } @$list; | ||
| 133 | @segments = @checked; | ||
| 134 | $range->set_value( $range->min ); | ||
| 135 | $self->mark_changed; | ||
| 136 | return [ map { +{%$_} } @segments ]; | ||
| 137 | } | ||
| 138 | |||
| 139 | method add_segment ($segment) { | ||
| 140 | push @segments, $self->_checked_segment($segment); | ||
| 141 | $range->set_value( $range->min ); | ||
| 142 | $self->mark_changed; | ||
| 143 | return $self; | ||
| 144 | } | ||
| 145 | |||
| 146 | method set_range (%range) { | ||
| 147 | $range->set_range(%range); | ||
| 148 | $self->mark_changed; | ||
| 149 | return $self; | ||
| 150 | } | ||
| 151 | |||
| 152 | method range_properties :common () { | ||
| 153 | return qw(min max); | ||
| 154 | } | ||
| 155 | |||
| 156 | method min (@new) { | ||
| 157 | $self->set_range( min => $new[0] ) if @new; | ||
| 158 | return $range->min; | ||
| 159 | } | ||
| 160 | |||
| 161 | method max (@new) { | ||
| 162 | $self->set_range( max => $new[0] ) if @new; | ||
| 163 | return $range->max; | ||
| 164 | } | ||
| 165 | |||
| 166 | method format_value ($number) { | ||
| 167 | return $range->format_value($number); | ||
| 168 | } | ||
| 169 | |||
| 170 | # --------------------------------------------------------------------- | ||
| 171 | # Accessors | ||
| 172 | # --------------------------------------------------------------------- | ||
| 173 | |||
| 174 | method _set ( $field_ref, $value ) { | ||
| 175 | $$field_ref = $value; | ||
| 176 | $self->mark_changed; | ||
| 177 | return $$field_ref; | ||
| 178 | } | ||
| 179 | |||
| 180 | method _set_value_format ($format) { | ||
| 181 | $range->set_value_format($format); | ||
| 182 | $self->mark_changed; | ||
| 183 | return $range->value_format; | ||
| 184 | } | ||
| 185 | |||
| 186 | method indeterminate (@new) { return @new ? $self->_set( \$indeterminate, boolean( $self, indeterminate => $new[0] ) ) : $indeterminate } | ||
| 187 | method show_value (@new) { return @new ? $self->_set( \$show_value, boolean( $self, show_value => $new[0] ) ) : $show_value } | ||
| 188 | method value_position (@new) { return @new ? $self->_set( \$value_position, one_of( $self, value_position => $new[0], @VALUE_POSITIONS ) ) : $value_position } | ||
| 189 | method value_format (@new) { return @new ? $self->_set_value_format( $new[0] ) : $range->value_format } | ||
| 190 | method preferred_columns (@new) { return @new ? $self->_set( \$preferred_columns, positive_integer( $self, preferred_columns => $new[0] ) ) : $preferred_columns } | ||
| 191 | method style (@new) { return @new ? $self->_set( \$style, one_of( $self, style => $new[0], keys %GLYPHS_OF_STYLE ) ) : $style } | ||
| 192 | method fill_glyph (@new) { return @new ? $self->_set( \$fill_glyph, $self->_checked_glyph( fill_glyph => $new[0] ) ) : $fill_glyph } | ||
| 193 | method track_glyph (@new) { return @new ? $self->_set( \$track_glyph, $self->_checked_glyph( track_glyph => $new[0] ) ) : $track_glyph } | ||
| 194 | method stripe_glyph (@new) { return @new ? $self->_set( \$stripe_glyph, $self->_checked_glyph( stripe_glyph => $new[0] ) ) : $stripe_glyph } | ||
| 195 | method fractional (@new) { return @new ? $self->_set( \$fractional, boolean( $self, fractional => $new[0] ) ) : $fractional } | ||
| 196 | method striped (@new) { return @new ? $self->_set( \$striped, boolean( $self, striped => $new[0] ) ) : $striped } | ||
| 197 | method animated (@new) { return @new ? $self->_set( \$animated, boolean( $self, animated => $new[0] ) ) : $animated } | ||
| 198 | method separated (@new) { return @new ? $self->_set( \$separated, boolean( $self, separated => $new[0] ) ) : $separated } | ||
| 199 | method color (@new) { return @new ? $self->set_look( color => $new[0] ) : $self->look_value('color') } | ||
| 200 | method track_color (@new) { return @new ? $self->set_look( track_color => $new[0] ) : $self->look_value('track_color') } | ||
| 201 | method text_color (@new) { return @new ? $self->set_look( text_color => $new[0] ) : $self->look_value('text_color') } | ||
| 202 | method inside_text_color (@new) { return @new ? $self->set_look( inside_text_color => $new[0] ) : $self->look_value('inside_text_color') } | ||
| 203 | |||
| 204 | method theme_family :common () { | ||
| 205 | return 'progress'; | ||
| 206 | } | ||
| 207 | |||
| 208 | method themed_params :common () { | ||
| 209 | return ( | ||
| 210 | $class->SUPER::themed_params, | ||
| 211 | color => [ 'color', 'normal', 'cell_color' ], | ||
| 212 | track_color => [ 'track', 'normal', 'cell_color' ], | ||
| 213 | text_color => [ 'text', 'normal', 'cell_color' ], | ||
| 214 | inside_text_color => [ 'inside_text', 'normal', 'cell_color' ], | ||
| 215 | ); | ||
| 216 | } | ||
| 217 | |||
| 218 | # The glyphs in use: the style's, unless given one by one. | ||
| 219 | method glyphs () { | ||
| 220 | my ( $fill, $track, $stripe ) = $GLYPHS_OF_STYLE{$style}->@*; | ||
| 221 | return ( $fill_glyph // $fill, $track_glyph // $track, $stripe_glyph // $stripe ); | ||
| 222 | } | ||
| 223 | |||
| 224 | method layout_properties :common () { | ||
| 225 | return ( | ||
| 226 | $class->SUPER::layout_properties, | ||
| 227 | ( map { $_ => 'scalar' } qw(min max value value_position value_format preferred_columns style fill_glyph track_glyph stripe_glyph) ), | ||
| 228 | ( map { $_ => 'boolean' } qw(indeterminate show_value fractional striped animated separated) ), | ||
| 229 | segment => \&_parse_segment, | ||
| 230 | ); | ||
| 231 | } | ||
| 232 | |||
| 233 | # segment value=30 color="#98c379" | ||
| 234 | method _parse_segment ($kid) { | ||
| 235 | my $props = $self->kdl_properties( $kid, qw(value color) ); | ||
| 236 | die ref($self) . ": layout property 'segment' needs value=..." unless exists $props->{value}; | ||
| 237 | return $self->add_segment($props); | ||
| 238 | } | ||
| 239 | |||
| 240 | # --------------------------------------------------------------------- | ||
| 241 | # Painting | ||
| 242 | # --------------------------------------------------------------------- | ||
| 243 | |||
| 244 | method _shows_label () { | ||
| 245 | return $show_value && !$indeterminate; | ||
| 246 | } | ||
| 247 | |||
| 248 | # The columns of the value label: wide enough for any value, so the | ||
| 249 | # bar keeps its length while the value changes. A label inside the bar | ||
| 250 | # takes no columns of its own. | ||
| 251 | method _label_columns () { | ||
| 252 | return 0 unless $self->_shows_label && $value_position ne 'inside'; | ||
| 253 | return List::Util::max map { string_columns( $self->format_value($_) ) } $range->min, $range->max, $self->value; | ||
| 254 | } | ||
| 255 | |||
| 256 | # Where the bar starts and how many columns it has. | ||
| 257 | method _bar_span () { | ||
| 258 | my $label = $self->_label_columns; | ||
| 259 | my $bar = List::Util::max( 1, $self->columns - ( $label ? $label + 1 : 0 ) ); | ||
| 260 | return ( $value_position eq 'left' && $label ? $label + 1 : 0, $bar ); | ||
| 261 | } | ||
| 262 | |||
| 263 | method natural_size () { | ||
| 264 | my $label = $self->_label_columns; | ||
| 265 | return ( $preferred_columns + ( $label ? $label + 1 : 0 ), 1 ); | ||
| 266 | } | ||
| 267 | |||
| 268 | # The frame of the running animation, if any, which the paint key | ||
| 269 | # includes; asking for it keeps the frames coming. | ||
| 270 | method _animation_frame () { | ||
| 271 | return $self->animation_frame( RUNNER_SECONDS, $self->_runner_frames ) if $indeterminate; | ||
| 272 | return $self->animation_frame( STRIPE_SECONDS, 4 ) if $striped && $animated; | ||
| 273 | return -1; | ||
| 274 | } | ||
| 275 | |||
| 276 | # The indeterminate runner bounces across the bar: as many frames as | ||
| 277 | # positions there and back. | ||
| 278 | method _runner_width ($bar) { | ||
| 279 | return List::Util::max( 1, int( $bar / 4 ) ); | ||
| 280 | } | ||
| 281 | |||
| 282 | method _runner_frames () { | ||
| 283 | my ( undef, $bar ) = $self->_bar_span; | ||
| 284 | my $travel = $bar - $self->_runner_width($bar); | ||
| 285 | return $travel > 0 ? 2 * $travel : 1; | ||
| 286 | } | ||
| 287 | |||
| 288 | method paint_key :override () { | ||
| 289 | return ( $self->SUPER::paint_key, $self->_animation_frame ); | ||
| 290 | } | ||
| 291 | |||
| 292 | # The cells of the bar, left to right, as [ glyph, fg, bg, on_fill ], | ||
| 293 | # for the current value: the filled part in the colors of its | ||
| 294 | # segments, the rest in the track. | ||
| 295 | method _bar_cells ($bar) { | ||
| 296 | my ( $fill, $track, $stripe ) = $self->glyphs; | ||
| 297 | my $frame = $self->_animation_frame; | ||
| 298 | my $inside = $self->_shows_label && $value_position eq 'inside'; | ||
| 299 | my @cells = map { [ $inside ? ' ' : $track, $self->color_attr( $self->track_color ), $inside ? $self->color_attr( $self->track_color ) : undef, 0 ] } 1 .. $bar; | ||
| 300 | |||
| 301 | my @parts = @segments ? @segments : ( { value => $range->value - $range->min, color => undef } ); | ||
| 302 | my ( $edge, $at ) = ( 0, 0 ); | ||
| 303 | foreach my $index ( 0 .. $#parts ) { | ||
| 304 | my $part = $parts[$index]; | ||
| 305 | my $fg = $self->color_attr( $part->{color} // $self->color ); | ||
| 306 | my $bg = $inside ? $fg : undef; | ||
| 307 | my $start = $at; | ||
| 308 | $edge += $part->{value} / ( $range->max - $range->min ) * $bar; | ||
| 309 | $edge = $bar if $edge > $bar; | ||
| 310 | my $full = int( $edge + 1e-9 ); | ||
| 311 | foreach my $x ( $start .. $full - 1 ) { | ||
| 312 | my $glyph = $inside ? ' ' : $striped && ( int( ( $x + ( $animated ? $frame : 0 ) ) / 2 ) % 2 ) ? $stripe : $fill; | ||
| 313 | $cells[$x] = [ $glyph, $fg, $bg, 1 ]; | ||
| 314 | } | ||
| 315 | $at = $full; | ||
| 316 | if ( $separated && $index < $#parts && $at < $bar ) { | ||
| 317 | $at++; | ||
| 318 | $edge = $at if $edge < $at; | ||
| 319 | } | ||
| 320 | next if $index < $#parts || $at >= $bar || !$fractional || $fill ne FULL_BLOCK; | ||
| 321 | my $eighths = int( ( $edge - $full ) * 8 ); | ||
| 322 | $cells[$at] = [ $EIGHTH_BLOCK[ $eighths - 1 ], $fg, $inside ? $self->color_attr( $self->track_color ) : undef, 0 ] if $eighths > 0; | ||
| 323 | } | ||
| 324 | return @cells; | ||
| 325 | } | ||
| 326 | |||
| 327 | # The cells of an indeterminate bar: a runner bouncing across the track. | ||
| 328 | method _runner_cells ( $bar, $frame ) { | ||
| 329 | my ( $fill, $track ) = $self->glyphs; | ||
| 330 | my $width = $self->_runner_width($bar); | ||
| 331 | my $travel = $bar - $width; | ||
| 332 | my $at = $travel > 0 ? ( $frame <= $travel ? $frame : 2 * $travel - $frame ) : 0; | ||
| 333 | return map { $_ >= $at && $_ < $at + $width ? [ $fill, $self->color_attr( $self->color ), undef, 1 ] : [ $track, $self->color_attr( $self->track_color ), undef, 0 ] } 0 .. $bar - 1; | ||
| 334 | } | ||
| 335 | |||
| 336 | method paint () { | ||
| 337 | my ( $start, $bar ) = $self->_bar_span; | ||
| 338 | my $rows = $self->rows; | ||
| 339 | my @cells = $indeterminate ? $self->_runner_cells( $bar, $self->_animation_frame ) : $self->_bar_cells($bar); | ||
| 340 | foreach my $y ( 0 .. $rows - 1 ) { | ||
| 341 | $self->put_attrs( $start + $_, $y, @{ $cells[$_] }[ 0 .. 2 ] ) foreach 0 .. $#cells; | ||
| 342 | } | ||
| 343 | return unless $self->_shows_label; | ||
| 344 | |||
| 345 | my $label = $self->format_value( $self->value ); | ||
| 346 | my $y = int( ( $rows - 1 ) / 2 ); | ||
| 347 | if ( $value_position eq 'inside' ) { | ||
| 348 | my $x = List::Util::max( 0, int( ( $bar - string_columns($label) ) / 2 ) ); | ||
| 349 | foreach my $cluster ( grapheme_clusters($label) ) { | ||
| 350 | my $cell = $cells[$x] // last; | ||
| 351 | $self->put_attrs( $start + $x, $y, $cluster, $self->color_attr( $cell->[3] ? $self->inside_text_color : $self->text_color ), $cell->[2] ); | ||
| 352 | $x += cluster_columns($cluster); | ||
| 353 | } | ||
| 354 | return; | ||
| 355 | } | ||
| 356 | $self->paint_text( $value_position eq 'left' ? 0 : $self->columns - string_columns($label), $y, $label, $self->color_attr( $self->text_color ), undef ); | ||
| 357 | return; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | 1; | ||
| 362 | |||
| 363 | __END__ |