blib/lib/Term/Fabulous/Command.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 94 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
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.03';
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 keys as a menu shows them.
54 method keys_text () {
55 return join ', ', @keys;
56 }
57  
58 method is_enabled () {
59 return !defined $enabled || $enabled->() ? 1 : 0;
60 }
61  
62 method is_toggle () {
63 return defined $checked ? 1 : 0;
64 }
65  
66 method is_checked () {
67 return defined $checked && $checked->() ? 1 : 0;
68 }
69  
70 # Runs the command when it is enabled; returns whether it ran.
71 method run () {
72 return 0 unless $self->is_enabled;
73 $run->() if defined $run;
74 return 1;
75 }
76 }
77  
78 1;
79  
80 __END__