dbMan/Extension/HelpCommands.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 103 CC 14 SCAR 49.0
CC
14
Cov
0%
handle_action
210.0
IDENTIFICATION
2.0
preference
2.0
CRAP 134.4
1 package DBIx::dbMan::Extension::HelpCommands;
2  
3 use strict;
4 use base 'DBIx::dbMan::Extension';
5 use Text::FormatTable;
6 use Term::ANSIColor;
7  
8 our $VERSION = '0.09';
9  
10 1;
11  
12 sub IDENTIFICATION { return "000001-000010-000009"; }
13  
14 sub preference { return 0; }
15  
16 sub known_actions { return [ qw/HELP/ ]; }
17  
18 sub menu {
19 return ( { label => '_Help', preference => -10000, submenu => [
20 { label => 'Commands', action => { action => 'HELP', type => 'commands' } },
21 { label => 'License', action => { action => 'HELP', type => 'license' } },
22 { label => 'Version', action => { action => 'HELP', type => 'version' } }
23 ] } );
24 }
25  
26 sub handle_action {
27 my ($obj,%action) = @_;
28  
29 if ($action{action} eq 'HELP') {
30 if ($action{type} eq 'commands') {
31 my @help = ();
32 for my $ext (@{$obj->{-core}->{extensions}}) {
33 if ($ext->can('cmdhelp')) {
34 my %h = @{$ext->cmdhelp()};
35 for (keys %h) {
36 study $action{what} if $action{what};
37 push @help,[ $_, $h{$_} ] if /^\Q$action{what}/i;
38 }
39 }
40 }
41 if (@help) {
42 my $colorized = $obj->{-mempool}->get('output_format') eq 'colortable';
43 my $table = new Text::FormatTable '| l l | l |';
44 $table->rule;
45 for (sort { $a->[0] cmp $b->[0] } @help) {
46 $table->row( map {
47 my $r = $_;
48 $r =~ s/(\S+)/color( $obj->{-config}->tablecolor_content || 'bright_white' ) . $1 . color( $obj->{-config}->tablecolor_lines || 'reset' )/sge if $colorized;
49 $r;
50 } ' * ',@$_);
51 }
52 $table->rule;
53 $action{output} = ( $colorized ? color( $obj->{-config}->tablecolor_lines || 'reset' ) : '' )
54 . $table->render($obj->{-interface}->render_size) . ( $colorized ? color( 'reset' ) : '' );
55 } else {
56 $action{output} = "I havn't help for command ".$action{what}.".\n";
57 }
58 $action{action} = 'OUTPUT';
59 } elsif ($action{type} eq 'version') {
60 $action{output} = "dbMan version is ".$DBIx::dbMan::VERSION."\n";
61 if ($action{gui}) {
62 $action{action} = 'NONE';
63 $obj->{-interface}->infobox($action{output});
64 } else {
65 $action{action} = 'OUTPUT';
66 }
67 } elsif ($action{type} eq 'license') {
68 $action{output} = <<'EOF';
69 (c) Copyright 1999-2023 by Milan Sorm <sorm@is4u.cz>
70 All rights reserved.
71
72 This program is free software; you can redistribute it and/or modify it
73 under the same terms as Perl itself.
74 EOF
75 if ($action{gui}) {
76 $action{action} = 'NONE';
77 $obj->{-interface}->infobox($action{output});
78 } else {
79 $action{action} = 'OUTPUT';
80 }
81 }
82 }
83  
84 $action{processed} = 1;
85 return %action;
86 }