blib/lib/Term/Fabulous/Range.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/Placement.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Curve.pm »
1 package Term::Fabulous::Range;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.05';
9  
10 1 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 class Term::Fabulous::Range :strict(params) {
13 1 ▶ use List::Util (); # min and max are methods here
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶ use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶ use Term::Fabulous::Check qw(boolean number optional);
Subroutine BEGIN: called CC=1 SCAR=2.6
16  
17 # How far past the middle between two grid values, in steps, a number
18 # still rounds up: the division by the step carries the rounding noise
19 # of the platform's floating point, so a number exactly halfway
20 # would otherwise go up on some perls and down on others.
21 use constant HALF_STEP_TOLERANCE => 1e-9;
22  
23 # Key name => [ unit, direction ]: steps, pages or an end of the range.
24 my %MOVE_BY_KEY = (
25 Left => [ step => -1 ],
26 Down => [ step => -1 ],
27 Right => [ step => 1 ],
28 Up => [ step => 1 ],
29 PageDown => [ page => -1 ],
30 PageUp => [ page => 1 ],
31 Home => [ end => -1 ],
32 End => [ end => 1 ],
33 );
34  
35 field $owner :param; # the widget class the messages name
36 field $min :param :reader = 0;
37 field $max :param :reader;
38 field $step :param :reader = undef; # undef: any value, no grid
39 field $page_step :param = undef; # undef: a tenth of the range
40 field $paging :param = 1; # whether PageUp and PageDown move
41 field $value_format :param :reader = undef;
42 field $default_format :param = undef; # code: ( number, range ) => text
43 field $format_percent :param = 0; # a format gets the percent (and the fraction)
44 field $current;
45  
46 ADJUST :params ( :$value = undef ) {
47 ( $min, $max, $step ) = $self->_checked_range( defined $step, min => $min, max => $max, step => $step );
48 $page_step = $self->_checked_page_step($page_step);
49 $paging = boolean( $owner, paging => $paging );
50 $value_format = Term::Fabulous::Check::value_format( $owner, value_format => $value_format );
51 $format_percent = boolean( $owner, format_percent => $format_percent );
52 die "Term::Fabulous::Range: default_format must be a code reference" if defined $default_format && ref $default_format ne 'CODE';
53 $current = $min;
54 $self->set_value( $value // $min );
55 }
56  
57 method _fail ($message) {
58 die "$owner: $message";
59 }
60  
61 # The range with the given parts changed, checked as a whole; a
62 # stepped range keeps a step.
63 method _checked_range ( $stepped, %range ) {
64 my ( $low, $high ) = map { number( $owner, $_ => $range{$_} ) } qw(min max);
65 $self->_fail("min ($low) must be less than max ($high)") unless $low < $high;
66 return ( $low, $high, undef ) unless $stepped;
67 my $increment = number( $owner, step => $range{step} );
68 $self->_fail("step must be positive, got $increment") unless $increment > 0;
69 return ( $low, $high, $increment );
70 }
71  
72 method _checked_page_step ($size) {
73 $size = optional( \&number, $owner, page_step => $size );
74 $self->_fail("page_step must be positive, got $size") if defined $size && $size <= 0;
75 return $size;
76 }
77  
78 # ---------------------------------------------------------------------
79 # The grid
80 # ---------------------------------------------------------------------
81  
82 # Digits after the decimal point in the shortest form of a number:
83 # 0.25 has 2, 1e-12 has 12, 1500 has 0.
84 sub _decimal_places ($number) {
85 my ( $mantissa, $exponent ) = sprintf( '%.15g', $number ) =~ /\A-?(\d+(?:\.\d+)?)(?:e([-+]\d+))?\z/
86 or die "Term::Fabulous::Range: cannot read the decimal places of $number";
87 my $places = $mantissa =~ /\.(\d+)\z/ ? length $1 : 0;
88 return List::Util::max( 0, $places - ( $exponent // 0 ) );
89 }
90  
91 # Digits after the decimal point that values need: enough for the step
92 # and for min, so values stay on the grid and print without
93 # floating-point noise.
94 method decimals () {
95 return List::Util::max( _decimal_places( $step // 1 ), _decimal_places($min) );
96 }
97  
98 # The whole number of steps nearest to a count of steps; a count
99 # halfway between two goes up.
100 sub _whole_steps ($steps) {
101 return floor( $steps + 0.5 + HALF_STEP_TOLERANCE );
102 }
103  
104 # The nearest value on the grid min, min + step, ... inside the range;
105 # without a step, the number limited to the range.
106 method snapped ($number) {
107 my $inside = List::Util::min( List::Util::max( $number, $min ), $max );
108 return $inside unless defined $step;
109 my $snapped = $min + _whole_steps( ( $inside - $min ) / $step ) * $step;
110 $snapped -= $step while $snapped > $max + $step / 1e6;
111 $snapped = $min if $snapped < $min;
112 return 0 + sprintf '%.*f', $self->decimals, $snapped;
113 }
114  
115 # The highest value on the grid: max itself when the range is a whole
116 # number of steps.
117 method top_value () {
118 return $self->snapped($max);
119 }
120  
121 method page_step () {
122 return $page_step if defined $page_step;
123 my $unit = $step // ( $max - $min ) / 100;
124 return List::Util::max( $unit, $unit * _whole_steps( ( $max - $min ) / $unit / 10 ) );
125 }
126  
127 method set_page_step ($size) {
128 $page_step = $self->_checked_page_step($size);
129 return $self;
130 }
131  
132 # ---------------------------------------------------------------------
133 # The value
134 # ---------------------------------------------------------------------
135  
136 method value () {
137 return $current;
138 }
139  
140 method fraction_of ($number) {
141 return ( $number - $min ) / ( $max - $min );
142 }
143  
144 method fraction () {
145 return $self->fraction_of($current);
146 }
147  
148 # Sets the value from the program: it must lie in the range.
149 method set_value ($number) {
150 $number = number( $owner, value => $number );
151 $self->_fail("value must be in $min..$max, got $number") if $number < $min || $number > $max;
152 $current = $self->snapped($number);
153 return $current;
154 }
155  
156 # Changes min, max and step together, so a new range can be set in one
157 # go whatever the old one was; the value moves into it.
158 method set_range (%range) {
159 my @known = defined $step ? qw(min max step) : qw(min max);
160 my @unknown = grep {
161 my $part = $_;
162 !grep { $_ eq $part } @known
163 } sort keys %range;
164 $self->_fail( "set_range takes " . join( ', ', @known[ 0 .. $#known - 1 ] ) . " and $known[-1], got @unknown" ) if @unknown;
165 ( $min, $max, $step ) = $self->_checked_range( defined $step, min => $min, max => $max, step => $step, %range );
166 $current = $self->snapped($current);
167 return $self;
168 }
169  
170 # Moves the value to the grid value nearest to a number, limited to
171 # the range; returns 1 when the value changed, else 0. This is how the
172 # user moves it: the widget fires its Change event on a change.
173 method move_to ($number) {
174 my $moved = $self->snapped($number);
175 return 0 if $moved == $current;
176 $current = $moved;
177 return 1;
178 }
179  
180 # Moves by a number of steps (pages: page_step); returns as move_to.
181 method move_by ( $steps, $unit = 'step' ) {
182 my $size = $unit eq 'page' ? $self->page_step : $step // $self->page_step / 10;
183 return $self->move_to( $current + $steps * $size );
184 }
185  
186 # Moves as a key asks: the arrows by a step, PageUp and PageDown by a
187 # page (unless paging is off), Home and End to an end. Returns undef
188 # for a key that does not move a range, else as move_to.
189 method move_by_key ($name) {
190 my $move = $MOVE_BY_KEY{ $name // '' } // return undef;
191 my ( $unit, $direction ) = @$move;
192 return undef if $unit eq 'page' && !$paging;
193 return $self->move_to( $direction < 0 ? $min : $self->top_value ) if $unit eq 'end';
194 return $self->move_by( $direction, $unit );
195 }
196  
197 # ---------------------------------------------------------------------
198 # Writing a value
199 # ---------------------------------------------------------------------
200  
201 method set_value_format ($format) {
202 $value_format = Term::Fabulous::Check::value_format( $owner, value_format => $format );
203 return $self;
204 }
205  
206 # A value as text: with the code reference (which gets the value, and
207 # the fraction with format_percent), with the sprintf format (of the
208 # value, or of the percent with format_percent), else the widget's
209 # default, else the value with the decimals of the grid.
210 method format_value ($number) {
211 if ( ref $value_format eq 'CODE' ) {
212 return $format_percent ? $value_format->( $number, $self->fraction_of($number) ) : $value_format->($number);
213 }
214 return sprintf $value_format, ( $format_percent ? 100 * $self->fraction_of($number) : $number ) if defined $value_format;
215 return $default_format->( $number, $self ) if defined $default_format;
216 return sprintf '%.*f', $self->decimals, $number;
217 }
218 }
219  
220 1;
221  
222 __END__
« blib/lib/Term/Fabulous/Placement.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Curve.pm »