blib/lib/Config/Model/SimpleUI.pm

filter: Statementstmt 60.9% 111 / 182 Branchbran 25.6% 20 / 78 Conditioncond 18.5% 5 / 27 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 66.6% 20 / 30 Podpod 33.3% 2 / 6 totaltotal 46.9% 160 / 341 CC 54 SCAR 32.9
CC
54
Cov
47%
__ANON__
110.0
run_loop
72.0
__ANON__
35.3
CRAP 26.9
« blib/lib/Config/Model/Warper.pm ↑ Summary blib/lib/Config/Model/TermUI.pm »
1 #
2 # This file is part of Config-Model
3 #
4 # This software is Copyright (c) 2005-2022 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Config::Model::SimpleUI 2.169;
11  
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use v5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use open qw(:std :utf8); # undeclared streams in UTF-8
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Regexp::Common qw/delimited/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 my $syntax = '
24 cd <elt>, cd <elt:key>, cd - , cd !
25 -> jump into node
26 set elt=value, elt:key=value
27 -> set a value
28 clear elt
29 -> clear value or list or hash
30 delete elt:key
31 -> delete a value from a list or hash element
32 delete elt
33 -> like reset, delete a value (set to undef)
34 display elt elt:key
35 -> display a value
36 ls -> show content of object (args: path and/or filter pattern)
37 ls -> show elements of current node
38 ls foo* -> shows element matching foo.*
39 ls \'foo*\' -> shows elements of node stored in "foo*"
40 ls aHash -> shows keys of Hash
41 ls \'aHash:"*"\' -> shows elements of node stored in key "*" of "aHash" hash
42 ll [-nz] [-v] [ element | pattern ]
43 -> show elements of current node and their value
44 (options: -nz → hides empty value, -v → verbose)
45 (args: element name or filter pattern)
46 info -> show detailed information on object or current node
47 (args: optional path to object)
48 tree -> show configuration tree from an object or current node
49 (args: optional path to object)
50 help -> show available command
51 desc[ription] -> show class desc of current node
52 desc <element> -> show desc of element from current node
53 desc <value> -> show effect of value (for enum)
54 changes -> list unsaved changes
55 check [elt] -> run check current on current node or elt
56 fix [ ! | elt ]
57 -> fix warnings in current node or of specified element or on all tree (with ! arg)
58 save -> save current changes
59 exit -> exit shell
60 ';
61  
62 0 ▶my $desc_sub = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
63 0 my $self = shift;
64 0 my $obj = $self->{current_node};
65 0 my $res = '';
66  
67 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
68 0 my $item;
69 0 ▶ while ( $item = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item = shift()) 0 0 0
70 0 ▶ if ( $obj->get_type() eq 'node' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->get_type eq "node") 0 0 0
71 0 my $type = $obj->element_type($item);
72 0 my $elt = $obj->fetch_element($item);
73 0 my $help = $obj->get_help_as_text($item);
74 0 $res .= "element $item (type $type)";
75 0 ▶ $res .= ": " if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 0 0
76 0 ▶ $res .= "\n" if $help =~ /\n/ or length($help) > 40 ;
Branch: 1 branch
BranchTrueFalseTotal
if $help =~ /\n/u or length $help > 40 0 0 0
Condition0%0/3
$help =~ /\n/u or length $help > 40
l!l&&r!l&&!r
000
MC/DC0%0/2
$help =~ /\n/u or length $help > 40
$help =~ /\n/u length $help > 40
Truth table0%0/3
A or B
A= $help =~ /\n/uB= length $help > 40
ABresult
1X1
011
000
77 0 ▶ $res .= $help . "\n" if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 0 0
78 0 ▶ if ( $type eq 'leaf' and $elt->value_type eq 'enum' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "leaf" and $elt->value_type eq "enum") 0 0 0
Condition0%0/3
$type eq "leaf" and $elt->value_type eq "enum"
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "leaf" and $elt->value_type eq "enum"
$type eq "leaf" $elt->value_type eq "enum"
Truth table0%0/3
A and B
A= $type eq "leaf"B= $elt->value_type eq "enum"
ABresult
0X0
100
111
79 0 $res .= " possible values: " . join( ', ', $elt->get_choice ) . "\n";
80 }
81 }
82 }
83 }
84 else {
85 0 $res = $obj->get_help_as_text();
86 }
87 0 return $res;
88 2 };
89  
90 1 ▶my $ll_sub = sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
91 1 my $self = shift;
92 1 my @raw_args = @_;
93  
94 1 my @desc_opt = qw/check no/;
95  
96 1 ▶ my %opt = map { /^-(\w+)/ ? ($1 => 1) : () } @raw_args;
Branch: 1 branch
BranchTrueFalseTotal
/^-(\w+)/u ? : 0 1 1
97 1 ▶ push @desc_opt, hide_empty => 1 if $opt{nz} ;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"nz"} 0 1 1
98 1 ▶ push @desc_opt, verbose => 1 if $opt{v} ;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"v"} 0 1 1
99  
100 1 my @args = grep {! /^-/ } @raw_args;
101 1 ▶ push @args, '*' unless @args; # default action is to list all elements
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 1 1
102  
103 1 my $obj = $self->{current_node};
104 1 for (@args) {s/\*/.*/g;} ;
105 1 my $pattern = join ('|',@args);
106  
107 1 return $obj->describe( pattern => qr/^$pattern$/, @desc_opt );
108 2 };
109  
110 2 ▶my $cd_sub = sub {
Subroutine __ANON__: called CC=9 SCAR=35.6
111 2 my $self = shift;
112 2 my @cmds = @_;
113  
114 # convert usual cd_ism ( .. /foo) to grab syntax ( - ! foo)
115 #map { s(^/) (! );
116 # s(\.\.)(-)g;
117 # s(/) ( )g;
118 # } @cmds ;
119  
120 2 my $new_node = $self->{current_node}->grab("@cmds");
121 2 my $type = $new_node->get_type;
122 2 my $name = $new_node->element_name;
123  
124 2 ▶ if ( defined $new_node && $type eq 'node' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $new_node and $type eq 'node') { } 2 0 2
elsif (defined $new_node and $type eq 'list') { } 0 0 0
elsif (defined $new_node and $type eq 'hash') { } 0 0 0
elsif (defined $new_node and $type eq 'leaf') { } 0 0 0
Condition33%1/3
defined $new_node and $type eq "node"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $new_node and $type eq "node"
defined $new_node $type eq "node"
Truth table33%1/3
A and B
A= defined $new_nodeB= $type eq "node"
ABresult
0X0
100
111
125 2 $self->{current_node} = $new_node;
126 }
127 ▶ elsif ( defined $new_node && $type eq 'list' ) {
Condition0%0/3
defined $new_node and $type eq "list"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $new_node and $type eq "list"
defined $new_node $type eq "list"
Truth table0%0/3
A and B
A= defined $new_nodeB= $type eq "list"
ABresult
0X0
100
111
128 0 print "Can't cd in a $type, please add an index (e.g. $name:0)\n";
129 }
130 ▶ elsif ( defined $new_node && $type eq 'hash' ) {
Condition0%0/3
defined $new_node and $type eq "hash"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $new_node and $type eq "hash"
defined $new_node $type eq "hash"
Truth table0%0/3
A and B
A= defined $new_nodeB= $type eq "hash"
ABresult
0X0
100
111
131 0 print "Can't cd in a $type, please add an index (e.g. $name:foo)\n";
132 }
133 ▶ elsif ( defined $new_node && $type eq 'leaf' ) {
Condition0%0/3
defined $new_node and $type eq "leaf"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $new_node and $type eq "leaf"
defined $new_node $type eq "leaf"
Truth table0%0/3
A and B
A= defined $new_nodeB= $type eq "leaf"
ABresult
0X0
100
111
134 0 print "Can't cd in a $type\n";
135 }
136 else {
137 0 print "Cannot find @_\n";
138 }
139  
140 2 return "";
141 2 };
142  
143 my %run_dispatch = (
144 0 ▶ help => sub { return $syntax; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 2 ▶ set => sub ($self, @args) {
Subroutine __ANON__: called CC=2 SCAR=7.6
146 2 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 2 0 2
147 2 $self->{current_node}->load(join('',@args));
148 }
149 else {
150 0 say "No command given.";
151 }
152 2 return "";
153 },
154 1 ▶ display => sub ($self, @args) {
Subroutine __ANON__: called CC=2 SCAR=9.2
155 1 ▶ unless (@args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 0 1 1
156 0 say "Nothing to display";
157 0 return;
158 }
159 1 return $self->{current_node}->grab_value(@args);
160 },
161 0 ▶ info => sub ($self, @args) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
162 0 my $cnode = $self->{current_node};
163 0 ▶ my $target = @args ? $cnode->grab(steps => [@args]) : $cnode;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
164 0 return join("\n", $target->get_info );
165 },
166 2 ▶ ls => sub ($self, @args) {
Subroutine __ANON__: called CC=5 SCAR=17.0
167 2 my $target = $self->{current_node};
168 2 my $pattern = '*';
169 2 foreach my $item (@args) {
170 # added weird comment to avoid problem with cpan-mode syntax highlight
171 1 ▶ if ($item =~ /\*/ and not $item =~ /^["']/) { #") {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /\*/u and not $item =~ /^["']/u) 1 0 1
Condition33%1/3
$item =~ /\*/u and not $item =~ /^["']/u
!ll&&!rl&&r
001
MC/DC0%0/2
$item =~ /\*/u and not $item =~ /^["']/u
$item =~ /\*/u not $item =~ /^["']/u
Truth table33%1/3
A and not(B)
A= $item =~ /\*/uB= not $item =~ /^["']/u
ABresult
0X0
100
111
172 1 $pattern = $item;
173 1 last;
174 }
175 0 $target = $target->grab(steps => $item);
176 }
177 2 $pattern =~ s/\*/.*/g;
178  
179 2 my $i = $self->{current_node}->instance;
180 2 ▶ my @res = $target->can('children') ? grep {/^$pattern$/} $target->children : ();
Branch: 1 branch
BranchTrueFalseTotal
$target->can('children') ? : 2 0 2
181 2 return join( ' ', @res );
182 },
183 0 ▶ tree => sub ($self, @args) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
184 0 my $i = $self->{current_node}->instance;
185 0 my $cnode = $self->{current_node};
186 0 ▶ my $target = @args ? $cnode->grab(steps => [@args]) : $cnode;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
187 0 my @res = $target->dump_tree( mode => 'user' );
188 0 return join( ' ', @res );
189 },
190 1 ▶ delete => sub ($self, $del = '') {
Subroutine __ANON__: called CC=4 SCAR=15.0
191 1 ▶ if ($del) {
Branch: 1 branch
BranchTrueFalseTotal
if ($del) { } 1 0 1
192 1 my ( $elt_name, $key ) = split /\s*:\s*/, $del;
193 1 my $elt = $self->{current_node}->fetch_element($elt_name);
194 1 ▶ if ( length($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key) { } 1 0 1
195 1 $elt->delete($key);
196 }
197 else {
198 0 $elt->store(undef);
199 }
200 }
201 else {
202 0 say "delete what ?";
203 }
204 1 return '';
205 },
206 0 ▶ clear => sub ($self, $elt_name) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
207 0 ▶ if ($elt_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt_name) { } 0 0 0
208 0 $self->{current_node}->fetch_element($elt_name)->clear();
209 }
210 else {
211 say "Expected element name for clear command. I.e. one of ",
212 0 join(' ',$self->{current_node}->get_element_name);
213 }
214 0 return '';
215 },
216 0 ▶ check => sub ($self, $elt_name) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
217 0 ▶ if ($elt_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt_name) { } 0 0 0
218 0 $self->{current_node}->fetch_element($elt_name)->check();
219 }
220 else {
221 0 $self->{current_node}->check;
222 }
223 0 return '';
224 },
225 0 ▶ fix => sub ($self, $elt_name) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
226 0 ▶ if ($elt_name eq '!') {
Branch: 2 branches
BranchTrueFalseTotal
if ($elt_name eq '!') { } 0 0 0
elsif ($elt_name) { } 0 0 0
227 0 $self->{root}->instance->apply_fixes;
228 }
229 elsif ($elt_name) {
230 0 $self->{current_node}->fetch_element($elt_name)->apply_fixes;
231 }
232 else {
233 0 $self->{current_node}->apply_fixes;
234 }
235 0 return '';
236 },
237 0 ▶ save => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
238 0 $self->{root}->instance->write_back();
239 0 return "done";
240 },
241 0 ▶ changes => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
242 0 return $self->{root}->instance->list_changes;
243 },
244 2 ll => $ll_sub,
245 cd => $cd_sub,
246 description => $desc_sub,
247 desc => $desc_sub,
248 );
249  
250 2 $run_dispatch{reset} = $run_dispatch{clear};
251 2 $run_dispatch{dump} = $run_dispatch{tree};
252  
253 1 ▶sub simple_ui_commands {
Subroutine simple_ui_commands: called CC=1 SCAR=0.0
Pod undocumented
254 1 my @cmds = sort keys %run_dispatch;
255 1 return @cmds;
256 }
257  
258 1 ▶sub new ($type, %args) {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
259 1 my $self = {};
260  
261 1 foreach my $p (qw/root title prompt/) {
262 3 ▶ $self->{$p} = delete $args{$p}
Branch: 1 branch
BranchTrueFalseTotal
$self->{$p} = delete $args{$p} or croak("SimpleUI->new: Missing $p parameter") 3 0 3
263 or croak "SimpleUI->new: Missing $p parameter";
264 }
265  
266 1 $self->{current_node} = $self->{root};
267  
268 1 return bless $self, $type;
269 }
270  
271 0 ▶sub run_loop ($self) {
Subroutine run_loop: not called CC=8 SCAR=42.8
Pod documented
272  
273 0 my $user_cmd;
274 0 print $self->prompt;
275 ## no critic (InputOutput::ProhibitExplicitStdin)
276 0 ▶ while ( defined( $user_cmd = <STDIN> ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($user_cmd = readline STDIN)) 0 0 0
277 0 chomp $user_cmd;
278 0 ▶ last if $user_cmd eq 'exit' or $user_cmd eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $user_cmd eq "exit" or $user_cmd eq "quit" 0 0 0
Condition0%0/3
$user_cmd eq "exit" or $user_cmd eq "quit"
l!l&&r!l&&!r
000
MC/DC0%0/2
$user_cmd eq "exit" or $user_cmd eq "quit"
$user_cmd eq "exit" $user_cmd eq "quit"
Truth table0%0/3
A or B
A= $user_cmd eq "exit"B= $user_cmd eq "quit"
ABresult
1X1
011
000
279 0 my $res = $self->run($user_cmd);
280 0 ▶ print $res, "\n" if defined $res;
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 0 0 0
281 0 print $self->prompt;
282 }
283 0 print "\n";
284  
285 0 my $instance = $self->{root}->instance;
286 0 ▶ if ( $instance->c_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance->c_count) 0 0 0
287 0 ▶ if ($instance->has_changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance->has_changes) 0 0 0
288 0 $instance->say_changes;
289 0 print "write back data before exit ? (Y/n)";
290 ## no critic (InputOutput::ProhibitExplicitStdin)
291 0 $user_cmd = <STDIN>;
292 0 ▶ $instance->write_back unless $user_cmd =~ /n/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $user_cmd =~ /n/iu 0 0 0
293 0 print "\n";
294 }
295 }
296 0 return;
297 }
298  
299 11 ▶sub prompt ($self) {
Subroutine prompt: called CC=2 SCAR=6.9
Pod documented
300 11 my $ret = $self->{prompt} . ':';
301 11 my $loc = $self->{current_node}->location_short;
302 11 ▶ $ret .= " $loc " if $loc;
Branch: 1 branch
BranchTrueFalseTotal
if $loc 3 8 11
303 11 return $ret . '$ ';
304 }
305  
306 10 ▶sub run ($self, $user_cmd) {
Subroutine run: called CC=4 SCAR=13.9
Pod undocumented
307  
308 10 ▶ return '' unless $user_cmd =~ /\w/;
Branch: 1 branch
BranchTrueFalseTotal
unless $user_cmd =~ /\w/u 0 10 10
309  
310 10 my $re = $RE{delimited}{-delim=>q{'"}};
311 10 my ( $action, @args ) = ( $user_cmd =~ /((?:[^\s"']|$re)+)/g );
312  
313 10 ▶ if ( defined $run_dispatch{$action} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $run_dispatch{$action}) { } 9 1 10
314 9 my $res;
315 9 my $ok = eval {
316 9 $res = $run_dispatch{$action}->( $self, @args );
317 9 1;
318 };
319 9 ▶ say $@->message unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 9 9
320 9 return $res;
321 }
322 else {
323 1 return "Unexpected command '$action'";
324 }
325 }
326  
327 1 ▶sub list_cd_path ($self) {
Subroutine list_cd_path: called CC=4 SCAR=13.9
Pod undocumented
328 1 my $c_node = $self->{current_node};
329  
330 1 my @result;
331 1 foreach my $elt_name ( $c_node->get_element_name ) {
332 18 my $t = $c_node->element_type($elt_name);
333  
334 18 ▶ if ( $t eq 'list' or $t eq 'hash' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq 'list' or $t eq 'hash') { } 7 11 18
Condition100%3/3
$t eq "list" or $t eq "hash"
l!l&&r!l&&!r
3411
MC/DC100%2/2
$t eq "list" or $t eq "hash"
$t eq "list" $t eq "hash"
Truth table100%3/3
A or B
A= $t eq "list"B= $t eq "hash"
ABresult
1X1
011
000
335 push @result,
336 7 map { "$elt_name:$_" } $c_node->fetch_element($elt_name)->fetch_all_indexes;
337 }
338 else {
339 11 push @result, $elt_name;
340 }
341 }
342  
343 1 return \@result;
344 }
345 2 1;
346  
347 #ABSTRACT: Simple interface for Config::Model
348  
349 __END__
« blib/lib/Config/Model/Warper.pm ↑ Summary blib/lib/Config/Model/TermUI.pm »