« blib/lib/Term/Fabulous/Widget/Sparkline.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/StarRating.pm »
| 1 | package Term::Fabulous::Widget::Spinner; | ||
| 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::Spinner | ||
| 15 | :isa(Term::Fabulous::Widget::Display) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use List::Util (); # max is a method elsewhere in the family | ||
| 19 | use Term::Fabulous::Check qw(boolean describe number one_of string); | ||
| 20 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 21 | |||
| 22 | # A ring of blocks with a gap that runs around it: the frames remove | ||
| 23 | # one of the eight cells around the center in turn. | ||
| 24 | my @RING_CELLS = ( [ 0, 0 ], [ 0, 1 ], [ 0, 2 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ], [ 2, 0 ], [ 1, 0 ] ); | ||
| 25 | |||
| 26 | sub _ring_frames () { | ||
| 27 | my @frames; | ||
| 28 | foreach my $gap (@RING_CELLS) { | ||
| 29 | my @rows = map { [ ("\x{2588}") x 3 ] } 1 .. 3; | ||
| 30 | $rows[1][1] = ' '; | ||
| 31 | $rows[ $gap->[0] ][ $gap->[1] ] = ' '; | ||
| 32 | push @frames, join "\n", map { join '', @$_ } @rows; | ||
| 33 | } | ||
| 34 | return \@frames; | ||
| 35 | } | ||
| 36 | |||
| 37 | # The ready-made styles: frames and how long each one shows. | ||
| 38 | my %STYLE = ( | ||
| 39 | dots => { interval => 0.08, frames => [ map { chr } 0x280B, 0x2819, 0x2839, 0x2838, 0x283C, 0x2834, 0x2826, 0x2827, 0x2807, 0x280F ] }, | ||
| 40 | line => { interval => 0.13, frames => [ '-', '\\', '|', '/' ] }, | ||
| 41 | arc => { interval => 0.10, frames => [ map { chr } 0x25DC, 0x25E0, 0x25DD, 0x25DE, 0x25E1, 0x25DF ] }, | ||
| 42 | circle => { interval => 0.12, frames => [ map { chr } 0x25D0, 0x25D3, 0x25D1, 0x25D2 ] }, | ||
| 43 | arrow => { interval => 0.10, frames => [ map { chr } 0x2190, 0x2196, 0x2191, 0x2197, 0x2192, 0x2198, 0x2193, 0x2199 ] }, | ||
| 44 | box => { interval => 0.12, frames => [ map { chr } 0x2596, 0x2598, 0x259D, 0x2597 ] }, | ||
| 45 | pulse => { interval => 0.15, frames => [ map { chr } 0x00B7, 0x2022, 0x25CF, 0x2022 ] }, | ||
| 46 | bar => { interval => 0.08, frames => [ map { chr } 0x2581, 0x2583, 0x2584, 0x2585, 0x2586, 0x2587, 0x2588, 0x2587, 0x2586, 0x2585, 0x2584, 0x2583 ] }, | ||
| 47 | dots3 => { interval => 0.30, frames => [ ' ', '. ', '.. ', '...' ] }, | ||
| 48 | bounce => { interval => 0.12, frames => [ '[= ]', '[ = ]', '[ = ]', '[ =]', '[ = ]', '[ = ]' ] }, | ||
| 49 | wave => { | ||
| 50 | interval => 0.10, | ||
| 51 | frames => [ | ||
| 52 | map { | ||
| 53 | my $i = $_; | ||
| 54 | join '', map { chr( $_ == $i ? 0x2588 : abs( $_ - $i ) == 1 ? 0x2593 : 0x2591 ) } 0 .. 4 | ||
| 55 | } 0 .. 4, | ||
| 56 | 3, 2, | ||
| 57 | 1 | ||
| 58 | ] | ||
| 59 | }, | ||
| 60 | ring => { interval => 0.10, frames => _ring_frames() }, | ||
| 61 | ); | ||
| 62 | |||
| 63 | my @LABEL_POSITIONS = qw(left right); | ||
| 64 | |||
| 65 | field $style :param = 'dots'; | ||
| 66 | field $frames :param = undef; | ||
| 67 | field $interval :param = undef; | ||
| 68 | field $label :param = ''; | ||
| 69 | field $label_position :param = 'right'; | ||
| 70 | field $running :param = 1; | ||
| 71 | |||
| 72 | ADJUST { | ||
| 73 | $style = one_of( $self, style => $style, keys %STYLE ); | ||
| 74 | $frames = $self->_checked_frames($frames); | ||
| 75 | $interval = $self->_checked_interval($interval); | ||
| 76 | $label = string( $self, label => $label ); | ||
| 77 | $label_position = one_of( $self, label_position => $label_position, @LABEL_POSITIONS ); | ||
| 78 | $running = boolean( $self, running => $running ); | ||
| 79 | } | ||
| 80 | |||
| 81 | method theme_family :common () { | ||
| 82 | return 'spinner'; | ||
| 83 | } | ||
| 84 | |||
| 85 | method themed_params :common () { | ||
| 86 | return ( $class->SUPER::themed_params, color => [ 'color', 'normal', 'cell_color' ], label_color => [ 'label', 'normal', 'cell_color' ] ); | ||
| 87 | } | ||
| 88 | |||
| 89 | # Frames of your own: a non-empty array of strings, copied. | ||
| 90 | method _checked_frames ($list) { | ||
| 91 | return undef unless defined $list; | ||
| 92 | die ref($self) . ": frames must be an array reference of one or more strings, got " . describe($list) unless ref $list eq 'ARRAY' && @$list; | ||
| 93 | foreach my $frame (@$list) { | ||
| 94 | die ref($self) . ": every frame must be a string, got " . describe($frame) unless defined $frame && !ref $frame; | ||
| 95 | } | ||
| 96 | return [@$list]; | ||
| 97 | } | ||
| 98 | |||
| 99 | method _checked_interval ($seconds) { | ||
| 100 | return undef unless defined $seconds; | ||
| 101 | $seconds = number( $self, interval => $seconds ); | ||
| 102 | die ref($self) . ": interval must be positive, got $seconds" unless $seconds > 0; | ||
| 103 | return $seconds; | ||
| 104 | } | ||
| 105 | |||
| 106 | # --------------------------------------------------------------------- | ||
| 107 | # Accessors | ||
| 108 | # --------------------------------------------------------------------- | ||
| 109 | |||
| 110 | method _set ( $field_ref, $value ) { | ||
| 111 | $$field_ref = $value; | ||
| 112 | $self->mark_changed; | ||
| 113 | return $$field_ref; | ||
| 114 | } | ||
| 115 | |||
| 116 | method style (@new) { return @new ? $self->_set( \$style, one_of( $self, style => $new[0], keys %STYLE ) ) : $style } | ||
| 117 | method interval (@new) { return @new ? $self->_set( \$interval, $self->_checked_interval( $new[0] ) ) : $interval // $STYLE{$style}{interval} } | ||
| 118 | method label (@new) { return @new ? $self->_set( \$label, string( $self, label => $new[0] ) ) : $label } | ||
| 119 | method label_position (@new) { return @new ? $self->_set( \$label_position, one_of( $self, label_position => $new[0], @LABEL_POSITIONS ) ) : $label_position } | ||
| 120 | method running (@new) { return @new ? $self->_set( \$running, boolean( $self, running => $new[0] ) ) : $running } | ||
| 121 | method color (@new) { return @new ? $self->set_look( color => $new[0] ) : $self->look_value('color') } | ||
| 122 | method label_color (@new) { return @new ? $self->set_look( label_color => $new[0] ) : $self->look_value('label_color') } | ||
| 123 | |||
| 124 | method frames (@new) { | ||
| 125 | return [ ( $frames // $STYLE{$style}{frames} )->@* ] unless @new; | ||
| 126 | $self->_set( \$frames, $self->_checked_frames( $new[0] ) ); | ||
| 127 | return $self->frames; | ||
| 128 | } | ||
| 129 | |||
| 130 | method start () { $self->running(1); return $self } | ||
| 131 | method stop () { $self->running(0); return $self } | ||
| 132 | |||
| 133 | method styles :common () { | ||
| 134 | my @styles = sort keys %STYLE; | ||
| 135 | return @styles; | ||
| 136 | } | ||
| 137 | |||
| 138 | # frames "-" "\\" "|" "/" | ||
| 139 | method _parse_frames ($kid) { | ||
| 140 | my @frames = map { $_->as_perl } $kid->args->@*; | ||
| 141 | die ref($self) . ": layout property 'frames' takes one or more strings and no properties" if !@frames || $kid->props->@* || $kid->children->@*; | ||
| 142 | $self->frames( \@frames ); | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | method layout_properties :common () { | ||
| 147 | return ( | ||
| 148 | $class->SUPER::layout_properties, | ||
| 149 | ( map { $_ => 'scalar' } qw(style interval label label_position) ), | ||
| 150 | running => 'boolean', | ||
| 151 | frames => \&_parse_frames, | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | |||
| 155 | # --------------------------------------------------------------------- | ||
| 156 | # Painting | ||
| 157 | # --------------------------------------------------------------------- | ||
| 158 | |||
| 159 | # The rows of a frame, and the size every frame needs: the widest row | ||
| 160 | # and the most rows of any frame, so the label stays in place. | ||
| 161 | sub _rows_of ($frame) { | ||
| 162 | return split /\n/, $frame, -1; | ||
| 163 | } | ||
| 164 | |||
| 165 | method _frame_size () { | ||
| 166 | my @all = map { [ _rows_of($_) ] } $self->frames->@*; | ||
| 167 | return ( | ||
| 168 | List::Util::max( 1, map { string_columns($_) } map { @$_ } @all ), | ||
| 169 | List::Util::max( 1, map { scalar @$_ } @all ), | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | |||
| 173 | method _label_columns () { | ||
| 174 | return length $label ? string_columns($label) + 1 : 0; | ||
| 175 | } | ||
| 176 | |||
| 177 | method natural_size () { | ||
| 178 | my ( $columns, $rows ) = $self->_frame_size; | ||
| 179 | return ( $columns + $self->_label_columns, $rows ); | ||
| 180 | } | ||
| 181 | |||
| 182 | # The frame shown now, which the paint key includes; asking for it | ||
| 183 | # keeps the frames coming while the spinner runs. | ||
| 184 | method frame_index () { | ||
| 185 | my $frames = $self->frames; | ||
| 186 | return $running ? $self->animation_frame( $self->interval, scalar @$frames ) : 0; | ||
| 187 | } | ||
| 188 | |||
| 189 | method paint_key :override () { | ||
| 190 | return ( $self->SUPER::paint_key, $self->frame_index ); | ||
| 191 | } | ||
| 192 | |||
| 193 | method paint () { | ||
| 194 | my ( $frame_columns, $frame_rows ) = $self->_frame_size; | ||
| 195 | my $label_columns = $self->_label_columns; | ||
| 196 | my $frame_x = $label_position eq 'left' ? $label_columns : 0; | ||
| 197 | my @rows = _rows_of( $self->frames->[ $self->frame_index ] ); | ||
| 198 | my $fg = $self->color_attr( $self->color ); | ||
| 199 | $self->paint_text( $frame_x, $_, $rows[$_], $fg, undef, $frame_x + $frame_columns ) foreach 0 .. $#rows; | ||
| 200 | return unless $label_columns; | ||
| 201 | |||
| 202 | my $y = int( ( $frame_rows - 1 ) / 2 ); | ||
| 203 | $self->paint_text( $label_position eq 'left' ? 0 : $frame_columns + 1, $y, $label, $self->color_attr( $self->label_color ), undef ); | ||
| 204 | return; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | 1; | ||
| 209 | |||
| 210 | __END__ |