| 1 | package Term::Fabulous::Widget::Table::Filter; | ||
| 2 | |||
| 3 | 2 | ▶ | use v5.32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | 2 | ▶ | use Object::Pad 0.825; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | class Term::Fabulous::Widget::Table::Filter :strict(params) { | ||
| 13 | 2 | ▶ | use Feature::Compat::Try; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 2 | ▶ | use List::Util (); # all and any are methods of this class |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 2 | ▶ | use Scalar::Util qw(blessed); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 0 | ▶ | use Term::Fabulous::Check qw(describe one_of optional); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 17 | use Term::Fabulous::Widget::Table::Value qw(is_blank number_of date_epoch date_interval); | ||
| 18 | |||
| 19 | my %TEXT_OP = map { $_ => 1 } qw(contains not_contains equals not_equals starts_with ends_with matches); | ||
| 20 | my %BLANK_OP = map { $_ => 1 } qw(empty not_empty); | ||
| 21 | my %COMPARE_OP = map { $_ => 1 } ( '=', '!=', '<', '<=', '>', '>=', 'between', 'in' ); | ||
| 22 | my %ALIAS = ( '==' => '=', eq => '=', ne => '!=', lt => '<', le => '<=', gt => '>', ge => '>=' ); | ||
| 23 | my @TYPES = qw(string number date); | ||
| 24 | my @ONS = qw(value display); | ||
| 25 | |||
| 26 | my $NUMBER = qr/[-+]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][-+]?[0-9]+)?/; | ||
| 27 | |||
| 28 | # A condition on one column, a test, or a combination of filters. | ||
| 29 | field $column :param :reader = undef; | ||
| 30 | field $op :param :reader = undef; | ||
| 31 | field $value :param = undef; | ||
| 32 | field $on :param :reader = 'value'; | ||
| 33 | field $type :param :reader = undef; | ||
| 34 | field $case_sensitive :param :reader = 0; | ||
| 35 | field $test :param :reader = undef; | ||
| 36 | field $combine :param :reader = undef; # all, any or not | ||
| 37 | field $filters :param = undef; # the filters combine joins | ||
| 38 | |||
| 39 | # What the comparison operand turned into: a number, [ first, after ) | ||
| 40 | # epoch spans, a compiled pattern or folded text; built on first use | ||
| 41 | # for the column's type. | ||
| 42 | field %_operand_for_type; | ||
| 43 | |||
| 44 | ADJUST { | ||
| 45 | die "Term::Fabulous::Widget::Table::Filter: give 'op', 'test' or a combination ('all', 'any', 'not'), not several" | ||
| 46 | if 1 < grep { defined } $op, $test, $combine; | ||
| 47 | $self->_check_combination if defined $combine; | ||
| 48 | $self->_check_test if defined $test; | ||
| 49 | $self->_check_condition if defined $op; | ||
| 50 | die "Term::Fabulous::Widget::Table::Filter: a filter needs 'op' (with 'column'), 'test' or a combination" | ||
| 51 | unless defined $op || defined $test || defined $combine; | ||
| 52 | one_of( $self, on => $on, @ONS ); | ||
| 53 | optional( \&one_of, $self, type => $type, @TYPES ); | ||
| 54 | $case_sensitive = $case_sensitive ? 1 : 0; | ||
| 55 | } | ||
| 56 | |||
| 57 | method _check_combination () { | ||
| 58 | die "Term::Fabulous::Widget::Table::Filter: internal: unknown combination '$combine'" unless $combine =~ /\A(?:all|any|not)\z/; | ||
| 59 | die "Term::Fabulous::Widget::Table::Filter: $combine takes filters, not a column" if defined $column; | ||
| 60 | foreach my $filter (@$filters) { | ||
| 61 | die "Term::Fabulous::Widget::Table::Filter: $combine takes Term::Fabulous::Widget::Table::Filter objects or code references, got " . describe($filter) | ||
| 62 | unless blessed $filter && $filter->isa('Term::Fabulous::Widget::Table::Filter'); | ||
| 63 | } | ||
| 64 | die "Term::Fabulous::Widget::Table::Filter: not takes exactly one filter" if $combine eq 'not' && @$filters != 1; | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | method _check_test () { | ||
| 69 | die "Term::Fabulous::Widget::Table::Filter: test must be a code reference, got " . describe($test) unless ref $test eq 'CODE'; | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | method _check_condition () { | ||
| 74 | $op = $ALIAS{$op} // $op; | ||
| 75 | die "Term::Fabulous::Widget::Table::Filter: unknown op " . describe($op) . " (known: " . join( ', ', sort( keys %TEXT_OP, keys %BLANK_OP, keys %COMPARE_OP ) ) . ")" | ||
| 76 | unless $TEXT_OP{$op} || $BLANK_OP{$op} || $COMPARE_OP{$op}; | ||
| 77 | die "Term::Fabulous::Widget::Table::Filter: op '$op' needs a column" unless defined $column && !ref $column && length $column; | ||
| 78 | return if $BLANK_OP{$op}; | ||
| 79 | die "Term::Fabulous::Widget::Table::Filter: op '$op' needs a value" unless defined $value; | ||
| 80 | if ( $op eq 'between' ) { | ||
| 81 | die "Term::Fabulous::Widget::Table::Filter: op 'between' needs [ from, to ] as its value, got " . describe($value) | ||
| 82 | unless ref $value eq 'ARRAY' && @$value == 2 && !grep { !defined || ref } @$value; | ||
| 83 | $value = [@$value]; | ||
| 84 | } | ||
| 85 | elsif ( $op eq 'in' ) { | ||
| 86 | die "Term::Fabulous::Widget::Table::Filter: op 'in' needs an array reference of values, got " . describe($value) | ||
| 87 | unless ref $value eq 'ARRAY' && !grep { !defined || ref } @$value; | ||
| 88 | $value = [@$value]; | ||
| 89 | } | ||
| 90 | elsif ( $op eq 'matches' ) { | ||
| 91 | die "Term::Fabulous::Widget::Table::Filter: op 'matches' needs a pattern (qr// or a string), got " . describe($value) | ||
| 92 | if ref $value && ref $value ne 'Regexp'; | ||
| 93 | _compile_pattern( $value, $case_sensitive ); | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | die "Term::Fabulous::Widget::Table::Filter: op '$op' needs a plain value, got " . describe($value) if ref $value; | ||
| 97 | } | ||
| 98 | $self->_operand($type) if defined $type && $COMPARE_OP{$op}; # dies now for a value the type cannot read | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | |||
| 102 | # Dies when a column the filter compares is unknown to $source or an | ||
| 103 | # operand cannot be read as the type of its column; the table checks | ||
| 104 | # every filter it is given. | ||
| 105 | method check ($source) { | ||
| 106 | $_->check($source) foreach $self->filters; | ||
| 107 | return $self unless defined $column; | ||
| 108 | die "Term::Fabulous::Widget::Table::Filter: there is no column '$column'" unless $source->has_column($column); | ||
| 109 | $self->_operand( $type // $source->type_of($column) ) if defined $op && $COMPARE_OP{$op}; | ||
| 110 | return $self; | ||
| 111 | } | ||
| 112 | |||
| 113 | # A qr// pattern brings its own flags; a string is compiled, | ||
| 114 | # case-insensitively unless asked otherwise. | ||
| 115 | sub _compile_pattern ( $pattern, $case_sensitive ) { | ||
| 116 | return $pattern if ref $pattern eq 'Regexp'; | ||
| 117 | try { | ||
| 118 | return $case_sensitive ? qr/$pattern/ : qr/$pattern/i; | ||
| 119 | } | ||
| 120 | catch ($error) { | ||
| 121 | $error =~ s/ at \S+ line \d+\.?\n?\z//; | ||
| 122 | die "Term::Fabulous::Widget::Table::Filter: invalid pattern '$pattern': $error\n"; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | # --------------------------------------------------------------------- | ||
| 127 | # Combinations | ||
| 128 | # --------------------------------------------------------------------- | ||
| 129 | |||
| 130 | sub _as_filters (@filters) { | ||
| 131 | return map { ref $_ eq 'CODE' ? Term::Fabulous::Widget::Table::Filter->new( test => $_ ) : $_ } @filters; | ||
| 132 | } | ||
| 133 | |||
| 134 | method all :common (@filters) { | ||
| 135 | return $class->new( combine => 'all', filters => [ _as_filters(@filters) ] ); | ||
| 136 | } | ||
| 137 | |||
| 138 | method any :common (@filters) { | ||
| 139 | return $class->new( combine => 'any', filters => [ _as_filters(@filters) ] ); | ||
| 140 | } | ||
| 141 | |||
| 142 | method not :common ($filter) { | ||
| 143 | return $class->new( combine => 'not', filters => [ _as_filters($filter) ] ); | ||
| 144 | } | ||
| 145 | |||
| 146 | method filters () { | ||
| 147 | return defined $filters ? @$filters : (); | ||
| 148 | } | ||
| 149 | |||
| 150 | method value () { | ||
| 151 | return ref $value eq 'ARRAY' ? [@$value] : $value; | ||
| 152 | } | ||
| 153 | |||
| 154 | # The columns the filter reads, for the table to check that they exist. | ||
| 155 | method columns () { | ||
| 156 | return $column if defined $column; | ||
| 157 | return map { $_->columns } $self->filters; | ||
| 158 | } | ||
| 159 | |||
| 160 | # --------------------------------------------------------------------- | ||
| 161 | # Matching | ||
| 162 | # --------------------------------------------------------------------- | ||
| 163 | |||
| 164 | # $row is the row's data; $source answers value_of($row, $key), | ||
| 165 | # display_of($row, $key) and type_of($key). | ||
| 166 | method matches ( $row, $source ) { | ||
| 167 | return ( List::Util::all { $_->matches( $row, $source ) } @$filters ) ? 1 : 0 if defined $combine && $combine eq 'all'; | ||
| 168 | return ( List::Util::any { $_->matches( $row, $source ) } @$filters ) ? 1 : 0 if defined $combine && $combine eq 'any'; | ||
| 169 | return $filters->[0]->matches( $row, $source ) ? 0 : 1 if defined $combine; | ||
| 170 | return $test->($row) ? 1 : 0 if defined $test && !defined $column; | ||
| 171 | |||
| 172 | my $cell = $on eq 'display' ? $source->display_of( $row, $column ) : $source->value_of( $row, $column ); | ||
| 173 | return $test->( $cell, $row ) ? 1 : 0 if defined $test; | ||
| 174 | return $self->_matches_cell( $cell, $type // $source->type_of($column) ); | ||
| 175 | } | ||
| 176 | |||
| 177 | method _matches_cell ( $cell, $cell_type ) { | ||
| 178 | return is_blank($cell) ? 1 : 0 if $op eq 'empty'; | ||
| 179 | return is_blank($cell) ? 0 : 1 if $op eq 'not_empty'; | ||
| 180 | return $self->_matches_text( $cell // '' ) if $TEXT_OP{$op}; | ||
| 181 | |||
| 182 | my $operand = $self->_operand($cell_type); | ||
| 183 | return $self->_matches_string( $cell, $operand ) if $cell_type eq 'string'; | ||
| 184 | return $self->_matches_number( number_of($cell), $operand ) if $cell_type eq 'number'; | ||
| 185 | return $self->_matches_date( date_epoch($cell), $operand ); | ||
| 186 | } | ||
| 187 | |||
| 188 | method _matches_text ($cell) { | ||
| 189 | return "$cell" =~ _compile_pattern( $value, $case_sensitive ) ? 1 : 0 if $op eq 'matches'; | ||
| 190 | my ( $text, $wanted ) = $case_sensitive ? ( "$cell", $value ) : ( fc("$cell"), fc($value) ); | ||
| 191 | return index( $text, $wanted ) >= 0 ? 1 : 0 if $op eq 'contains'; | ||
| 192 | return index( $text, $wanted ) < 0 ? 1 : 0 if $op eq 'not_contains'; | ||
| 193 | return $text eq $wanted ? 1 : 0 if $op eq 'equals'; | ||
| 194 | return $text ne $wanted ? 1 : 0 if $op eq 'not_equals'; | ||
| 195 | return substr( $text, 0, length $wanted ) eq $wanted ? 1 : 0 if $op eq 'starts_with'; | ||
| 196 | return 1 if $wanted eq ''; # ends_with | ||
| 197 | return length($wanted) <= length($text) && substr( $text, -length($wanted) ) eq $wanted ? 1 : 0; | ||
| 198 | } | ||
| 199 | |||
| 200 | # The operand as the comparisons of a column type need it; dies for a | ||
| 201 | # value that type cannot read. | ||
| 202 | method _operand ($cell_type) { | ||
| 203 | return $_operand_for_type{$cell_type} //= do { | ||
| 204 | my @values = ref $value eq 'ARRAY' ? @$value : ($value); | ||
| 205 | my $read | ||
| 206 | = $cell_type eq 'number' ? sub ($item) { number_of($item) // die "Term::Fabulous::Widget::Table::Filter: op '$op' on a number column needs numbers, got '$item'\n" } | ||
| 207 | : $cell_type eq 'date' | ||
| 208 | ? sub ($item) { _date_span($item) // die "Term::Fabulous::Widget::Table::Filter: op '$op' on a date column needs dates (2024-05-03, 2024-05-03 14:30, epoch seconds), got '$item'\n" } | ||
| 209 | : sub ($item) { $case_sensitive ? "$item" : fc("$item") }; | ||
| 210 | [ map { $read->($_) } @values ]; | ||
| 211 | }; | ||
| 212 | } | ||
| 213 | |||
| 214 | # [ first, after ) of a date operand: a date string's span, or the one | ||
| 215 | # second an epoch number or object names. | ||
| 216 | sub _date_span ($item) { | ||
| 217 | my $interval = date_interval($item); | ||
| 218 | return $interval if defined $interval; | ||
| 219 | my $epoch = date_epoch($item) // return undef; | ||
| 220 | return [ $epoch, $epoch + 1 ]; | ||
| 221 | } | ||
| 222 | |||
| 223 | method _matches_string ( $cell, $operand ) { | ||
| 224 | return 0 if is_blank($cell); | ||
| 225 | my $text = $case_sensitive ? "$cell" : fc("$cell"); | ||
| 226 | return ( List::Util::any { $text eq $_ } @$operand ) ? 1 : 0 if $op eq 'in'; | ||
| 227 | return $operand->[0] le $text && $text le $operand->[1] ? 1 : 0 if $op eq 'between'; | ||
| 228 | return _compare( $text cmp $operand->[0], $op ); | ||
| 229 | } | ||
| 230 | |||
| 231 | method _matches_number ( $number, $operand ) { | ||
| 232 | return 0 unless defined $number; | ||
| 233 | return ( List::Util::any { $number == $_ } @$operand ) ? 1 : 0 if $op eq 'in'; | ||
| 234 | return $operand->[0] <= $number && $number <= $operand->[1] ? 1 : 0 if $op eq 'between'; | ||
| 235 | return _compare( $number <=> $operand->[0], $op ); | ||
| 236 | } | ||
| 237 | |||
| 238 | # A date compares with a span: equal inside it, less before its first | ||
| 239 | # second, greater from the first second after it. | ||
| 240 | method _matches_date ( $epoch, $spans ) { | ||
| 241 | return 0 unless defined $epoch; | ||
| 242 | my $inside = sub ($span) { $span->[0] <= $epoch && $epoch < $span->[1] }; | ||
| 243 | return ( List::Util::any { $inside->($_) } @$spans ) ? 1 : 0 if $op eq 'in'; | ||
| 244 | return $spans->[0][0] <= $epoch && $epoch < $spans->[1][1] ? 1 : 0 if $op eq 'between'; | ||
| 245 | my $span = $spans->[0]; | ||
| 246 | return _compare( $epoch < $span->[0] ? -1 : $epoch >= $span->[1] ? 1 : 0, $op ); | ||
| 247 | } | ||
| 248 | |||
| 249 | sub _compare ( $order, $op ) { | ||
| 250 | return $order == 0 ? 1 : 0 if $op eq '='; | ||
| 251 | return $order != 0 ? 1 : 0 if $op eq '!='; | ||
| 252 | return $order < 0 ? 1 : 0 if $op eq '<'; | ||
| 253 | return $order <= 0 ? 1 : 0 if $op eq '<='; | ||
| 254 | return $order > 0 ? 1 : 0 if $op eq '>'; | ||
| 255 | return $order >= 0 ? 1 : 0; # >= | ||
| 256 | } | ||
| 257 | |||
| 258 | # --------------------------------------------------------------------- | ||
| 259 | # Filter expressions, as typed into a filter field | ||
| 260 | # --------------------------------------------------------------------- | ||
| 261 | |||
| 262 | method parse :common ( $expression, %options ) { | ||
| 263 | my @unknown = grep { !/\A(?:column|type|on|case_sensitive)\z/ } sort keys %options; | ||
| 264 | die "Term::Fabulous::Widget::Table::Filter: parse does not accept @unknown (known: case_sensitive, column, on, type)" if @unknown; | ||
| 265 | die "Term::Fabulous::Widget::Table::Filter: parse needs a column" unless defined $options{column}; | ||
| 266 | my $type = $options{type} // 'string'; | ||
| 267 | one_of( $class, 'parse type' => $type, @TYPES ); | ||
| 268 | die "Term::Fabulous::Widget::Table::Filter: parse needs a string, got " . describe($expression) if ref $expression || !defined $expression; | ||
| 269 | |||
| 270 | my $text = $expression =~ s/\A\s+|\s+\z//gr; | ||
| 271 | return undef unless length $text; | ||
| 272 | my %common = ( column => $options{column}, type => $type, on => $options{on} // 'value', case_sensitive => $options{case_sensitive} // 0 ); | ||
| 273 | return $class->new( %common, op => 'empty' ) if $text eq '='; | ||
| 274 | return $class->new( %common, op => 'not_empty' ) if $text eq '!='; | ||
| 275 | my ( $op, $value ) = $type eq 'string' ? _parse_text($text) : _parse_ordered( $type, $text ); | ||
| 276 | return $class->new( %common, op => $op, value => $value ); | ||
| 277 | } | ||
| 278 | |||
| 279 | sub _parse_text ($text) { | ||
| 280 | return ( matches => $1 ) if $text =~ m{\A/(.+)/\z}s; | ||
| 281 | return ( not_equals => $1 ) if $text =~ /\A!=\s*(.+)\z/s; | ||
| 282 | return ( equals => $1 ) if $text =~ /\A=\s*(.+)\z/s; | ||
| 283 | return ( not_contains => $1 ) if $text =~ /\A!\s*(.+)\z/s; | ||
| 284 | return ( equals => $1 ) if $text =~ /\A\^(.+)\$\z/s; | ||
| 285 | return ( starts_with => $1 ) if $text =~ /\A\^(.+)\z/s; | ||
| 286 | return ( ends_with => $1 ) if $text =~ /\A(.+)\$\z/s; | ||
| 287 | return ( contains => $text ); | ||
| 288 | } | ||
| 289 | |||
| 290 | sub _parse_ordered ( $type, $text ) { | ||
| 291 | my $reads = $type eq 'number' ? sub ($item) { $item =~ /\A$NUMBER\z/ } : sub ($item) { defined date_interval($item) }; | ||
| 292 | if ( my ( $from, $to ) = $text =~ /\A(.+?)\s*\.\.\s*(.+)\z/s ) { | ||
| 293 | return ( between => [ $from, $to ] ) if $reads->($from) && $reads->($to); | ||
| 294 | } | ||
| 295 | elsif ( my ( $op, $operand ) = $text =~ /\A(<=|>=|!=|<|>|=)?\s*(.+)\z/s ) { | ||
| 296 | return ( $op // '=', $operand ) if $reads->($operand); | ||
| 297 | } | ||
| 298 | my $examples = $type eq 'number' ? '5, >5, >=5, <5, <=5, !=5 or 5..10' : '2024-05-03, >2024-05, <=2024-05-03 14:30 or 2024-01..2024-03'; | ||
| 299 | die "Term::Fabulous::Widget::Table::Filter: '$text' is not a $type filter (use $examples)\n"; | ||
| 300 | } | ||
| 301 | } | ||
| 302 | |||
| 303 | 1; | ||
| 304 | |||
| 305 | __END__ | ||