| 1 | package Term::Fabulous::Command; | ||
| 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::Command :strict(params) { | ||
| 13 | use Feature::Compat::Try; | ||
| 14 | use Term::Fabulous::Check qw(describe string); | ||
| 15 | use Term::Fabulous::Event::KeyPress; | ||
| 16 | |||
| 17 | field $id :param :reader; | ||
| 18 | field $label :param :reader; | ||
| 19 | field $run :param = undef; | ||
| 20 | field $enabled :param = undef; | ||
| 21 | field $checked :param = undef; | ||
| 22 | field @keys; | ||
| 23 | |||
| 24 | ADJUST :params ( :$keys = [] ) { | ||
| 25 | $id = string( $self, id => $id ); | ||
| 26 | die "Term::Fabulous::Command: id must not be empty" unless length $id; | ||
| 27 | $label = string( $self, "the label of the command '$id'" => $label ); | ||
| 28 | my %code = ( run => $run, enabled => $enabled, checked => $checked ); | ||
| 29 | foreach my $name ( sort keys %code ) { | ||
| 30 | die "Term::Fabulous::Command: $name of the command '$id' must be a code reference or undef, got " . describe( $code{$name} ) if defined $code{$name} && ref $code{$name} ne 'CODE'; | ||
| 31 | } | ||
| 32 | die "Term::Fabulous::Command: keys of the command '$id' must be an array reference, got " . describe($keys) unless ref $keys eq 'ARRAY'; | ||
| 33 | @keys = map { $self->_checked_key($_) } @$keys; | ||
| 34 | } | ||
| 35 | |||
| 36 | # A key name as Term::Fabulous::Event::KeyPress->key_name gives it; | ||
| 37 | # anything else dies here, not when the key never fires. | ||
| 38 | method _checked_key ($name) { | ||
| 39 | try { | ||
| 40 | Term::Fabulous::Event::KeyPress->fields_for_name($name); | ||
| 41 | } | ||
| 42 | catch ($error) { | ||
| 43 | chomp $error; | ||
| 44 | die "Term::Fabulous::Command: the command '$id' has a key that does not exist: $error"; | ||
| 45 | } | ||
| 46 | return $name; | ||
| 47 | } | ||
| 48 | |||
| 49 | method keys () { | ||
| 50 | return @keys; | ||
| 51 | } | ||
| 52 | |||
| 53 | # The label where the command is named outside a menu: without the | ||
| 54 | # "..." that says it opens a dialog. | ||
| 55 | method plain_label () { | ||
| 56 | return $label =~ s/\.\.\.\z//r; | ||
| 57 | } | ||
| 58 | |||
| 59 | # The keys as a menu shows them. | ||
| 60 | method keys_text () { | ||
| 61 | return join ', ', @keys; | ||
| 62 | } | ||
| 63 | |||
| 64 | method is_enabled () { | ||
| 65 | return !defined $enabled || $enabled->() ? 1 : 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | method is_toggle () { | ||
| 69 | return defined $checked ? 1 : 0; | ||
| 70 | } | ||
| 71 | |||
| 72 | method is_checked () { | ||
| 73 | return defined $checked && $checked->() ? 1 : 0; | ||
| 74 | } | ||
| 75 | |||
| 76 | # Runs the command when it is enabled; returns whether it ran. | ||
| 77 | method run () { | ||
| 78 | return 0 unless $self->is_enabled; | ||
| 79 | $run->() if defined $run; | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; | ||
| 85 | |||
| 86 | __END__ |