dbMan/Extension/KeyBindings.pm untested

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 215 CC 30 SCAR 58.5
CC
30
Cov
0%
handle_action
600.0
keysfile
20.0
load_keys
20.0
CRAP 347.1
1 package DBIx::dbMan::Extension::KeyBindings;
2  
3 use strict;
4 use base 'DBIx::dbMan::Extension';
5 use Text::FormatTable;
6  
7 our $VERSION = '0.03';
8  
9 1;
10  
11 sub IDENTIFICATION { return "000001-000087-000003"; }
12  
13 sub preference { return 0; }
14  
15 sub known_actions { return [ qw/KEYS/ ]; }
16  
17 sub menu {
18 my $obj = shift;
19  
20 return ( { label => 'Input', submenu => [
21 { label => 'Key bindings', submenu => [
22 { label => 'Show',
23 action => { action => 'KEYS', operation => 'show' } },
24 { label => 'Clear',
25 action => { action => 'KEYS', operation => 'clear' } },
26 { label => 'Reload',
27 action => { action => 'KEYS', operation => 'reload' } }
28 ] } ] } );
29 }
30  
31 sub keysfile {
32 my $obj = shift;
33  
34 return $ENV{DBMAN_KEYSFILE} if $ENV{DBMAN_KEYSFILE};
35 return $obj->{-config}->keys_file if $obj->{-config}->keys_file;
36 return $ENV{DBMAN_KEYSFILE_INTERNAL} if $ENV{DBMAN_KEYSFILE_INTERNAL};
37 return $ENV{HOME}.'/.dbman/keys';
38 }
39  
40 sub load_keys {
41 my $obj = shift;
42  
43 my @keys = ();
44  
45 $obj->clear_keys;
46  
47 if (open F,$obj->keysfile) {
48 if ( $obj->{ -interface }->is_utf8 ) {
49 binmode F, ':utf8';
50 }
51 while (<F>) {
52 chomp;
53 if (/^(.*?)\s+(.*)$/) {
54 push @keys,{ key => $1, text => $2 };
55 $obj->{-interface}->bind_key($1,$2);
56 }
57 }
58 close F;
59 }
60  
61 $obj->{-mempool}->set(keys => \@keys);
62 }
63  
64 sub clear_keys {
65 my $obj = shift;
66  
67 for my $def (@{$obj->{-mempool}->get('keys')}) {
68 $obj->{-interface}->bind_key($def->{key},'');
69 }
70 $obj->{-mempool}->set(keys => []);
71 }
72  
73 sub init {
74 my $obj = shift;
75  
76 $obj->{-mempool}->set(keys => []);
77 $obj->load_keys;
78 }
79  
80 sub done {
81 my $obj = shift;
82  
83 $obj->clear_keys;
84 }
85  
86 sub handle_action {
87 my ($obj,%action) = @_;
88  
89 if ($action{action} eq 'KEYS') {
90 if ($action{operation} eq 'clear') {
91 $obj->clear_keys;
92 my $add = '';
93 $add = 'permanently ' if ($action{permanent} and unlink $obj->keysfile);
94 $action{action} = 'OUTPUT';
95 $action{output} = "Key bindings $add"."cleared.\n";
96 } elsif ($action{operation} eq 'reload') {
97 $obj->load_keys;
98 my $count = @{$obj->{-mempool}->get('keys')};
99 $action{action} = 'OUTPUT';
100 $action{output} = "Key bindings reloaded ($count binding".($count == 1?'':'s').").\n";
101 } elsif ($action{operation} eq 'show') {
102 my @list = @{$obj->{-mempool}->get('keys')};
103 $action{action} = 'OUTPUT';
104 unless (@list) {
105 $action{output} = "No key bindings yet.\n";
106 } else {
107 my $table = new Text::FormatTable '| l | l |';
108 $table->rule;
109 $table->head('KEY','TEXT');
110 $table->rule;
111 for my $def (@list) {
112 $table->row($def->{key},$def->{text});
113 }
114 $table->rule;
115 $action{output} = $table->render($obj->{-interface}->render_size);
116 }
117 } elsif ($action{operation} eq 'define') {
118 $obj->{-interface}->print_prompt("Please press selected key.");
119 $action{key} = $obj->{-interface}->get_key();
120 $obj->{-interface}->print("Pressed ".$action{key}."\n");
121  
122 $obj->{-interface}->bind_key($action{key},$action{text});
123 my @keys = @{$obj->{-mempool}->get('keys')};
124 my @newkeys = ();
125 for my $key (@keys) {
126 push @newkeys,$key if $key->{key} ne $action{key};
127 }
128 push @newkeys,{ key => $action{key}, text => $action{text} };
129 $obj->{-mempool}->set(keys => \@newkeys);
130
131 my @all = ();
132 if (open F,$obj->keysfile) {
133 if ( $obj->{ -interface }->is_utf8 ) {
134 binmode F, ':utf8';
135 }
136 @all = <F>;
137 close F;
138 }
139 if (open F,">".$obj->keysfile) {
140 if ( $obj->{ -interface }->is_utf8 ) {
141 binmode F, ':utf8';
142 }
143 for my $line (@all) {
144 chomp $line;
145 if ($line =~ /^(.*?)\s+(.*)$/) {
146 print F "$line\n" if $1 ne $action{key};
147 }
148 }
149 print F "$action{key} $action{text}\n";
150 close F;
151 }
152  
153 $action{action} = 'OUTPUT';
154 $action{output} = "Key binding defined.\n";
155 } elsif ($action{operation} eq 'undefine') {
156 $obj->{-interface}->print_prompt("Please press selected key.");
157 $action{key} = $obj->{-interface}->get_key();
158 $obj->{-interface}->print("Pressed ".$action{key}."\n");
159  
160 $obj->{-interface}->bind_key($action{key},'');
161 my @keys = @{$obj->{-mempool}->get('keys')};
162 my @newkeys = ();
163 for my $key (@keys) {
164 push @newkeys,$key if $key->{key} ne $action{key};
165 }
166 $obj->{-mempool}->set(keys => \@newkeys);
167  
168 if (open F,$obj->keysfile) {
169 if ( $obj->{ -interface }->is_utf8 ) {
170 binmode F, ':utf8';
171 }
172 my @all = <F>;
173 close F;
174 if (open F,">".$obj->keysfile) {
175 if ( $obj->{ -interface }->is_utf8 ) {
176 binmode F, ':utf8';
177 }
178 for my $line (@all) {
179 chomp $line;
180 if ($line =~ /^(.*?)\s+(.*)$/) {
181 print F "$line\n" if $1 ne $action{key};
182 }
183 }
184 close F;
185 }
186 }
187  
188 $action{action} = 'OUTPUT';
189 $action{output} = "Key binding undefined.\n";
190 }
191 }
192  
193 $action{processed} = 1;
194 return %action;
195 }