| 1 | package Term::Fabulous::OptionList; | ||
| 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::OptionList :strict(params) { | ||
| 13 | 1 | ▶ | use List::Util qw(first); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | use Term::Fabulous::Check qw(boolean describe); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | |||
| 16 | field $owner :param; # the widget class the messages name | ||
| 17 | field @options; # { label, value, disabled } | ||
| 18 | field $selected; # index into @options, or undef | ||
| 19 | |||
| 20 | method _fail ($message) { | ||
| 21 | die "$owner: $message"; | ||
| 22 | } | ||
| 23 | |||
| 24 | # An option is a label (its own value), [ label, value ] or | ||
| 25 | # { label => ..., value => ..., disabled => ... }. | ||
| 26 | method _parsed ($option) { | ||
| 27 | my ( $label, $value, $disabled ) | ||
| 28 | = ref $option eq 'ARRAY' && @$option == 2 ? @$option | ||
| 29 | : ref $option eq 'HASH' ? @{$option}{qw(label value disabled)} | ||
| 30 | : !ref $option ? ( $option, $option ) | ||
| 31 | : (); | ||
| 32 | $self->_fail( "an option must be a label, [ label, value ] or { label => ..., value => ..., disabled => ... }, got " . describe($option) ) | ||
| 33 | unless defined $label && !ref $label && !ref $value; | ||
| 34 | $self->_fail("a hash option takes only the keys 'label', 'value' and 'disabled'") | ||
| 35 | if ref $option eq 'HASH' && grep { !/\A(?:label|value|disabled)\z/ } keys %$option; | ||
| 36 | return { label => "$label", value => $value // $label, disabled => boolean( $owner, 'option disabled' => $disabled ) }; | ||
| 37 | } | ||
| 38 | |||
| 39 | method _checked_index ( $what, $index ) { | ||
| 40 | $self->_fail( "$what needs an option index in 0.." . $#options . ", got " . describe($index) ) | ||
| 41 | unless defined $index && !ref $index && $index =~ /\A[0-9]+\z/ && $index < @options; | ||
| 42 | return $index + 0; | ||
| 43 | } | ||
| 44 | |||
| 45 | # --------------------------------------------------------------------- | ||
| 46 | # The options | ||
| 47 | # --------------------------------------------------------------------- | ||
| 48 | |||
| 49 | # New options; the selected value stays selected when an option still | ||
| 50 | # has it. | ||
| 51 | method set_options ($list) { | ||
| 52 | $self->_fail("options must be an array reference") unless ref $list eq 'ARRAY'; | ||
| 53 | my @parsed = map { $self->_parsed($_) } @$list; | ||
| 54 | my $kept_value = $self->value; | ||
| 55 | @options = @parsed; | ||
| 56 | $selected = defined $kept_value ? $self->index_of_value($kept_value) : undef; | ||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | method options () { | ||
| 61 | return map { +{%$_} } @options; | ||
| 62 | } | ||
| 63 | |||
| 64 | method count () { | ||
| 65 | return scalar @options; | ||
| 66 | } | ||
| 67 | |||
| 68 | method label ($index) { | ||
| 69 | return $options[ $self->_checked_index( label => $index ) ]{label}; | ||
| 70 | } | ||
| 71 | |||
| 72 | method is_disabled ($index) { | ||
| 73 | return $options[ $self->_checked_index( option_disabled => $index ) ]{disabled}; | ||
| 74 | } | ||
| 75 | |||
| 76 | method set_disabled ( $index, $disabled ) { | ||
| 77 | $options[ $self->_checked_index( option_disabled => $index ) ]{disabled} = boolean( $owner, 'option disabled' => $disabled ); | ||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | method enabled_indexes () { | ||
| 82 | return grep { !$options[$_]{disabled} } 0 .. $#options; | ||
| 83 | } | ||
| 84 | |||
| 85 | method index_of_value ($wanted) { | ||
| 86 | return first { defined $options[$_]{value} && $options[$_]{value} eq $wanted } 0 .. $#options; | ||
| 87 | } | ||
| 88 | |||
| 89 | # --------------------------------------------------------------------- | ||
| 90 | # The selection | ||
| 91 | # --------------------------------------------------------------------- | ||
| 92 | |||
| 93 | method selected_index () { | ||
| 94 | return $selected; | ||
| 95 | } | ||
| 96 | |||
| 97 | method value () { | ||
| 98 | return defined $selected ? $options[$selected]{value} : undef; | ||
| 99 | } | ||
| 100 | |||
| 101 | method selected_label () { | ||
| 102 | return defined $selected ? $options[$selected]{label} : undef; | ||
| 103 | } | ||
| 104 | |||
| 105 | # Selects as the program does: any option, a disabled one too; undef | ||
| 106 | # selects none. | ||
| 107 | method set_selected_index ($index) { | ||
| 108 | $self->_fail( "selected_index must be undef or an index in 0.." . $#options . ", got " . describe($index) ) | ||
| 109 | if defined $index && !( !ref $index && $index =~ /\A[0-9]+\z/ && $index < @options ); | ||
| 110 | $selected = defined $index ? $index + 0 : undef; | ||
| 111 | return $self; | ||
| 112 | } | ||
| 113 | |||
| 114 | method set_value ($wanted) { | ||
| 115 | my $index = defined $wanted ? $self->index_of_value($wanted) : undef; | ||
| 116 | $self->_fail("no option has the value '$wanted'") if defined $wanted && !defined $index; | ||
| 117 | $selected = $index; | ||
| 118 | return $self; | ||
| 119 | } | ||
| 120 | |||
| 121 | # Selects as the user does: returns 1 when the selection changed, 0 | ||
| 122 | # for the selected option or a disabled one. | ||
| 123 | method choose ($index) { | ||
| 124 | $index = $self->_checked_index( choose => $index ); | ||
| 125 | return 0 if $options[$index]{disabled} || ( defined $selected && $selected == $index ); | ||
| 126 | $selected = $index; | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | |||
| 130 | # --------------------------------------------------------------------- | ||
| 131 | # Layouts | ||
| 132 | # --------------------------------------------------------------------- | ||
| 133 | |||
| 134 | # The options of an 'options "Red" "Green"' node or of one | ||
| 135 | # 'option "Red" value="r" disabled=#true' node. | ||
| 136 | method from_layout_node :common ( $owner, $kid ) { | ||
| 137 | my $name = $kid->name; | ||
| 138 | die "$owner: layout property '$name' takes no children" if $kid->children->@*; | ||
| 139 | |||
| 140 | my @labels = map { $_->as_perl } $kid->args->@*; | ||
| 141 | if ( $name eq 'options' ) { | ||
| 142 | die "$owner: layout property 'options' takes one or more labels and no properties" if !@labels || $kid->props->@*; | ||
| 143 | return @labels; | ||
| 144 | } | ||
| 145 | my %props = map { $_->[0] => $_->[1]->as_perl } $kid->props->@*; | ||
| 146 | my @unknown = grep { !/\A(?:value|disabled)\z/ } sort keys %props; | ||
| 147 | die "$owner: layout property 'option' takes one label and optional value=... and disabled=..." if @labels != 1 || @unknown; | ||
| 148 | return { label => $labels[0], value => $props{value} // $labels[0], disabled => $props{disabled} // 0 }; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | 1; | ||
| 153 | |||
| 154 | __END__ | ||