« blib/lib/Term/Fabulous/Widget/Table/Cell.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/Table/ColumnChooser.pm »
| 1 | package Term::Fabulous::Widget::Table::Column; | ||
| 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 | class Term::Fabulous::Widget::Table::Column :strict(params) { | ||
| 13 | use Term::Fabulous::Check qw(boolean describe one_of sizing string); | ||
| 14 | use Term::Fabulous::Widget::Table::Style qw(style_hash); | ||
| 15 | use Term::Fabulous::Widget::Table::Value qw(is_blank number_of date_epoch compare_values natural_compare); | ||
| 16 | |||
| 17 | my @TYPES = qw(string number date); | ||
| 18 | my @ALIGNS = qw(left center right); | ||
| 19 | my @WRAPS = qw(words newlines none); | ||
| 20 | my @FILTER_ONS = qw(value display); | ||
| 21 | my %COMPARATOR = ( | ||
| 22 | string => sub ( $left, $right ) { compare_values( string => $left, $right ) }, | ||
| 23 | natural => \&natural_compare, | ||
| 24 | number => sub ( $left, $right ) { compare_values( number => $left, $right ) }, | ||
| 25 | date => sub ( $left, $right ) { compare_values( date => $left, $right ) }, | ||
| 26 | ); | ||
| 27 | |||
| 28 | # Whether a raw value can be ordered by a named comparator; the others | ||
| 29 | # sort last. | ||
| 30 | my %READABLE = ( | ||
| 31 | string => sub ($value) { is_blank($value) ? 0 : 1 }, | ||
| 32 | natural => sub ($value) { is_blank($value) ? 0 : 1 }, | ||
| 33 | number => sub ($value) { defined number_of($value) ? 1 : 0 }, | ||
| 34 | date => sub ($value) { defined date_epoch($value) ? 1 : 0 }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | field $key :param :reader; | ||
| 38 | field $title :param :reader = undef; | ||
| 39 | field $type :param :reader = 'string'; | ||
| 40 | field $value :param = undef; | ||
| 41 | field $mutator :param = undef; | ||
| 42 | field $align :param :reader = undef; | ||
| 43 | field $header_align :param :reader = undef; | ||
| 44 | field $width :param = 'fit'; | ||
| 45 | field $wrap :param :reader = 'words'; | ||
| 46 | field $sortable :param :reader = 1; | ||
| 47 | field $compare :param = undef; | ||
| 48 | field $filterable :param :reader = 1; | ||
| 49 | field $filter_on :param :reader = undef; | ||
| 50 | field $cell :param :reader = undef; | ||
| 51 | field $update_cell :param :reader = undef; | ||
| 52 | field $header :param :reader = undef; | ||
| 53 | field $cell_style :param :reader = undef; | ||
| 54 | field $style :param = undef; | ||
| 55 | field $header_style :param = undef; | ||
| 56 | field $visible :param :reader = 1; | ||
| 57 | |||
| 58 | field @_mutators; | ||
| 59 | field $_comparator; # code reference for a named comparator, undef for one of the user's | ||
| 60 | field %_params; # what the column was made with, for with() | ||
| 61 | |||
| 62 | ADJUST { | ||
| 63 | %_params = ( | ||
| 64 | key => $key, title => $title, type => $type, | ||
| 65 | value => $value, mutator => $mutator, align => $align, | ||
| 66 | header_align => $header_align, width => $width, wrap => $wrap, | ||
| 67 | sortable => $sortable, compare => $compare, filterable => $filterable, | ||
| 68 | filter_on => $filter_on, cell => $cell, update_cell => $update_cell, | ||
| 69 | header => $header, cell_style => $cell_style, style => $style, | ||
| 70 | header_style => $header_style, visible => $visible, | ||
| 71 | ); | ||
| 72 | |||
| 73 | die "Term::Fabulous::Widget::Table::Column: key must be a non-empty string, got " . describe($key) | ||
| 74 | unless defined $key && !ref $key && length $key; | ||
| 75 | $title = string( $self, title => $title // $key ); | ||
| 76 | my $owner = "Term::Fabulous::Widget::Table::Column '$key'"; | ||
| 77 | one_of( $owner, type => $type, @TYPES ); | ||
| 78 | $align //= $type eq 'number' ? 'right' : 'left'; | ||
| 79 | $header_align //= $align; | ||
| 80 | foreach my $setting ( [ align => $align ], [ header_align => $header_align ] ) { | ||
| 81 | one_of( $owner, @$setting, @ALIGNS ); | ||
| 82 | } | ||
| 83 | $width = sizing( $self, 'width', $width ); | ||
| 84 | one_of( $owner, wrap => $wrap, @WRAPS ); | ||
| 85 | $sortable = boolean( $self, sortable => $sortable ); | ||
| 86 | $filterable = boolean( $self, filterable => $filterable ); | ||
| 87 | $visible = boolean( $self, visible => $visible ); | ||
| 88 | $filter_on //= $type eq 'string' ? 'display' : 'value'; | ||
| 89 | one_of( $owner, filter_on => $filter_on, @FILTER_ONS ); | ||
| 90 | |||
| 91 | $self->_check_code( value => $value ); | ||
| 92 | @_mutators = ref $mutator eq 'ARRAY' ? @$mutator : defined $mutator ? ($mutator) : (); | ||
| 93 | $self->_check_code( "mutator", $_ ) foreach @_mutators; | ||
| 94 | $self->_check_code( $_->[0], $_->[1] ) foreach [ cell => $cell ], [ update_cell => $update_cell ], [ header => $header ], [ cell_style => $cell_style ]; | ||
| 95 | die "Term::Fabulous::Widget::Table::Column '$key': update_cell needs a cell" if defined $update_cell && !defined $cell; | ||
| 96 | |||
| 97 | $compare //= $type; | ||
| 98 | if ( ref $compare eq 'CODE' ) { | ||
| 99 | $_comparator = undef; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | die "Term::Fabulous::Widget::Table::Column '$key': compare must be a code reference or one of 'string', 'natural', 'number', 'date', got " . describe($compare) | ||
| 103 | unless defined $compare && !ref $compare && $COMPARATOR{$compare}; | ||
| 104 | $_comparator = $compare; | ||
| 105 | } | ||
| 106 | |||
| 107 | $style = style_hash( $self, "column '$key' style", column => $style ); | ||
| 108 | $header_style = style_hash( $self, "column '$key' header_style", header => $header_style ); | ||
| 109 | } | ||
| 110 | |||
| 111 | method _check_code ( $name, $code ) { | ||
| 112 | return if !defined $code || ref $code eq 'CODE'; | ||
| 113 | die "Term::Fabulous::Widget::Table::Column '$key': $name must be a code reference, got " . describe($code); | ||
| 114 | } | ||
| 115 | |||
| 116 | # A new column with some parameters changed. | ||
| 117 | method with (%changes) { | ||
| 118 | return Term::Fabulous::Widget::Table::Column->new( %_params, %changes ); | ||
| 119 | } | ||
| 120 | |||
| 121 | # The parameters the column was made with. | ||
| 122 | method params () { | ||
| 123 | return %_params; | ||
| 124 | } | ||
| 125 | |||
| 126 | method width () { | ||
| 127 | return {%$width}; | ||
| 128 | } | ||
| 129 | |||
| 130 | method style () { | ||
| 131 | return {%$style}; | ||
| 132 | } | ||
| 133 | |||
| 134 | method header_style () { | ||
| 135 | return {%$header_style}; | ||
| 136 | } | ||
| 137 | |||
| 138 | method mutators () { | ||
| 139 | return @_mutators; | ||
| 140 | } | ||
| 141 | |||
| 142 | method has_mutators () { | ||
| 143 | return @_mutators ? 1 : 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | method computes_value () { | ||
| 147 | return defined $value ? 1 : 0; | ||
| 148 | } | ||
| 149 | |||
| 150 | method value_of ($row) { | ||
| 151 | return defined $value ? $value->($row) : $row->{$key}; | ||
| 152 | } | ||
| 153 | |||
| 154 | # The text a cell shows: the raw value after every mutator. | ||
| 155 | method display_of ( $raw, $row ) { | ||
| 156 | my $shown = $raw; | ||
| 157 | $shown = $_->( $shown, $row ) foreach @_mutators; | ||
| 158 | return defined $shown ? "$shown" : ''; | ||
| 159 | } | ||
| 160 | |||
| 161 | method has_custom_compare () { | ||
| 162 | return defined $_comparator ? 0 : 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | method sorts_numerically () { | ||
| 166 | return defined $_comparator && ( $_comparator eq 'number' || $_comparator eq 'date' ) ? 1 : 0; | ||
| 167 | } | ||
| 168 | |||
| 169 | # What a named comparator orders a raw value by: a number (number, | ||
| 170 | # date) or a string (string, natural) that compares with <=> or cmp as | ||
| 171 | # the comparator would; undef for a value it cannot read, which sorts | ||
| 172 | # last. Natural keys write every digit run as its length and its | ||
| 173 | # digits, so "9" sorts before "10". | ||
| 174 | method sort_key ($raw) { | ||
| 175 | die "Term::Fabulous::Widget::Table::Column '$key': a column with a compare code reference has no sort keys" unless defined $_comparator; | ||
| 176 | return undef unless $READABLE{$_comparator}->($raw); | ||
| 177 | return number_of($raw) if $_comparator eq 'number'; | ||
| 178 | return date_epoch($raw) if $_comparator eq 'date'; | ||
| 179 | my $text = fc "$raw"; | ||
| 180 | return $text if $_comparator eq 'string'; | ||
| 181 | $text =~ s/([0-9]+)/ my $digits = $1 =~ s{\A0+(?=[0-9])}{}r; sprintf( '%05d', length $digits ) . $digits /ge; | ||
| 182 | return $text; | ||
| 183 | } | ||
| 184 | |||
| 185 | # Orders two rows by this column in a direction (1 ascending, -1 | ||
| 186 | # descending). Values a named comparator cannot read sort last either | ||
| 187 | # way; a comparator of your own sees every value. | ||
| 188 | method order ( $left, $right, $left_row, $right_row, $direction ) { | ||
| 189 | return $direction * $compare->( $left, $right, $left_row, $right_row ) unless defined $_comparator; | ||
| 190 | my $readable = $READABLE{$_comparator}; | ||
| 191 | my ( $left_ok, $right_ok ) = ( $readable->($left), $readable->($right) ); | ||
| 192 | return $right_ok - $left_ok unless $left_ok && $right_ok; | ||
| 193 | return $direction * $COMPARATOR{$_comparator}->( $left, $right ); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 | |||
| 199 | __END__ |