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