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

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 163 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::Sparkline;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.05';
9  
10 use Object::Pad 0.825;
11  
12 use Term::Fabulous::Widget::XYChart;
13  
14 class Term::Fabulous::Widget::Sparkline
15 :isa(Term::Fabulous::Widget::XYChart)
16 :strict(params)
17 {
18 use Carp qw(croak);
19 use Clay::XS qw(sizing_grow);
20 use List::Util ();
21 use Term::Fabulous::Check qw(boolean one_of);
22  
23 use constant SERIES => 'values';
24  
25 my @TYPES = qw(line area bar);
26  
27 field $type :param = 'line';
28 field $values :param = [];
29 field $color :param = undef;
30 field $min :param = undef;
31 field $max :param = undef;
32 field $zero :param = undef; # undef: by type
33  
34 ADJUST {
35 one_of( $self, type => $type, @TYPES );
36 $zero = $self->_checked_zero($zero);
37 $self->legend('none');
38 $self->x_axis( { visible => 0 } );
39 $self->_set_value_axis( $type, $min, $max, $zero );
40 $self->add_series( { name => SERIES, type => $type, data => $values, ( defined $color ? ( color => $color ) : () ) } );
41 $values = undef;
42 }
43  
44 method default_series_type () { return 'line' }
45  
46 # A bar takes at least a cell: with more values than columns, the bars
47 # show the newest values that fit.
48 method fit_prepared :override ( $width, $kind, $categories, $prepared ) {
49 return ( $kind, $categories, $prepared ) unless $type eq 'bar' && $kind eq 'category';
50 foreach my $entry (@$prepared) {
51 my $count = $entry->{xs}->@*;
52 next if $count <= $width;
53 my @kept = $count - $width .. $count - 1;
54 $entry->{$_} = [ $entry->{$_}->@[@kept] ] foreach grep { $entry->{$_} } qw(ys lows highs);
55 $entry->{xs} = [ 0 .. $#kept ];
56 }
57 my $shown = List::Util::min( scalar @$categories, $width );
58 return ( $kind, [ @$categories[ @$categories - $shown .. $#$categories ] ], $prepared );
59 }
60 method value_axis_edges :override () { return 1 }
61 method draws_baseline :override () { return 0 }
62  
63 method _checked_zero ($value) {
64 return defined $value ? boolean( $self, zero => $value ) : undef;
65 }
66  
67 # The value axis for a type and its ends; dies (changing nothing) for
68 # ends that are no numbers. Areas and bars include zero unless told
69 # otherwise, lines never.
70 method _set_value_axis ( $kind, $low, $high, $from_zero ) {
71 $self->y_axis( { visible => 0, grid => 0, zero => $from_zero // ( $kind eq 'line' ? 0 : 1 ), ( defined $low ? ( min => $low ) : () ), ( defined $high ? ( max => $high ) : () ) } );
72 return;
73 }
74  
75 # A sparkline is one row high, and as wide as its parent lets it be.
76 method natural_size :override () {
77 return ( sizing_grow(), 1 );
78 }
79  
80 method values (@new) {
81 return [ $self->series(SERIES)->{data}->@* ] unless @new;
82 $self->set_data( SERIES, $new[0] );
83 return $self->values;
84 }
85  
86 # Adds values at the end; with max_points, the oldest go.
87 method add_values (@new) {
88 $self->add_points( SERIES, @new );
89 return $self;
90 }
91  
92 method type (@new) {
93 return $type unless @new;
94 one_of( $self, type => $new[0], @TYPES );
95 $self->check_series_type( SERIES, $new[0] );
96 $self->_set_value_axis( $new[0], $min, $max, $zero );
97 $self->set_series( SERIES, type => $new[0] );
98 $type = $new[0];
99 return $type;
100 }
101  
102 method color (@new) {
103 return $color unless @new;
104 $self->set_series( SERIES, color => $new[0] );
105 $color = $new[0];
106 return $color;
107 }
108  
109 method min (@new) {
110 return $min unless @new;
111 $self->_set_value_axis( $type, $new[0], $max, $zero );
112 $min = $new[0];
113 return $min;
114 }
115  
116 method max (@new) {
117 return $max unless @new;
118 $self->_set_value_axis( $type, $min, $new[0], $zero );
119 $max = $new[0];
120 return $max;
121 }
122  
123 method zero (@new) {
124 return $zero unless @new;
125 my $from_zero = $self->_checked_zero( $new[0] );
126 $self->_set_value_axis( $type, $min, $max, $from_zero );
127 $zero = $from_zero;
128 return $zero;
129 }
130  
131 method layout_properties :common () {
132 return ( $class->SUPER::layout_properties, type => 'scalar', color => 'color', min => 'scalar', max => 'scalar', zero => 'boolean', values => \&_parse_values );
133 }
134  
135 # values 3 5 2 8
136 method _parse_values ($kid) {
137 my @values = map { $_->as_perl } $kid->args->@*;
138 croak ref($self) . ": layout property 'values' takes numbers" if $kid->props->@* || $kid->children->@*;
139 $self->values( \@values );
140 return;
141 }
142 }
143  
144 1;
145  
146 __END__