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

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 108 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::Commands;
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 use Term::Fabulous::Command;
13  
14 class Term::Fabulous::Commands :strict(params) {
15 use Clay::UI::Enum::Result;
16 use Scalar::Util qw(blessed);
17 use Term::Fabulous::Check qw(describe);
18  
19 field @_order; # the commands, in the order they were added
20 field %_by_id;
21 field %_by_key;
22  
23 ADJUST :params ( :$commands = [] ) {
24 die "Term::Fabulous::Commands: commands must be an array reference, got " . describe($commands) unless ref $commands eq 'ARRAY';
25 $self->add(@$commands);
26 }
27  
28 # Adds commands, each a Term::Fabulous::Command or the hash reference
29 # of its parameters: all of them, or none when one has an id or a key
30 # that is taken already.
31 method add (@items) {
32 my @commands = map { $self->_parsed_command($_) } @items;
33 my %id_taken = %_by_id;
34 my %key_taken = %_by_key;
35 foreach my $command (@commands) {
36 my $id = $command->id;
37 die "Term::Fabulous::Commands: there is a command '$id' already" if exists $id_taken{$id};
38 $id_taken{$id} = $command;
39 foreach my $key ( $command->keys ) {
40 die "Term::Fabulous::Commands: the key $key of the command '$id' runs the command '" . $key_taken{$key}->id . "' already" if exists $key_taken{$key};
41 $key_taken{$key} = $command;
42 }
43 }
44 push @_order, @commands;
45 %_by_id = %id_taken;
46 %_by_key = %key_taken;
47 return $self;
48 }
49  
50 method _parsed_command ($item) {
51 return $item if blessed $item && $item->isa('Term::Fabulous::Command');
52 return Term::Fabulous::Command->new(%$item) if ref $item eq 'HASH';
53 die "Term::Fabulous::Commands: a command must be a Term::Fabulous::Command or a hash reference of its parameters, got " . describe($item);
54 }
55  
56 method command ($id) {
57 return $_by_id{$id} // die "Term::Fabulous::Commands: there is no command " . describe($id);
58 }
59  
60 method commands () {
61 return @_order;
62 }
63  
64 method command_for_key ($name) {
65 return defined $name ? $_by_key{$name} : undef;
66 }
67  
68 # Runs the command of a key press, when there is one and it is
69 # enabled; returns whether it ran. The key's name is tried first,
70 # then the name of the main key it stands for (a key of the number
71 # pad).
72 method handle_key ($event) {
73 my $command = $self->command_for_key( $event->key_name ) // $self->command_for_key( $event->main_key_name ) // return 0;
74 return $command->run;
75 }
76  
77 # Runs commands for the keys that reach a widget: on the root, every
78 # key no other widget used.
79 method listen ($widget) {
80 die "Term::Fabulous::Commands: listen needs a widget that fires events, got " . describe($widget) unless blessed $widget && $widget->can('on');
81 $widget->on( KeyPress => sub ($event) { return $self->handle_key($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE } );
82 return $self;
83 }
84 }
85  
86 1;
87  
88 __END__