blib/lib/Config/Model/TermUI.pm

filter: Statementstmt 73.3% 110 / 150 Branchbran 54.5% 24 / 44 Conditioncond 18.1% 2 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 78.2% 18 / 23 Podpod 33.3% 1 / 3 totaltotal 65.5% 156 / 238 CC 34 SCAR 32.1
CC
34
Cov
66%
run_loop
110.0
new
14.7
__ANON__
9.0
CRAP 24.7
« blib/lib/Config/Model/SimpleUI.pm ↑ Summary blib/lib/Config/Model/Value.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::TermUI 2.169;
11  
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use utf8; # so literals and identifiers can be in UTF-8
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use v5.20; # to get "unicode_strings" feature
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use open qw(:std :utf8); # undeclared streams in UTF-8
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use base qw/Config::Model::SimpleUI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 0 ▶my $completion_sub = sub ($self, $text, $line, $start) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 my @choice = $self->{current_node}->get_element_name;
29 0 my @ret = grep { /^$text/ } @choice ;
30 0 return @ret;
31 1 };
32  
33 0 ▶my $leaf_completion_sub = sub ($self, $text, $line, $start) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 my @choice = $self->{current_node}->get_element_name( cargo_type => 'leaf' );
35 0 my @ret = grep { /^$text/ } @choice ;
36 0 return @ret;
37 1 };
38  
39 1 ▶my $fix_completion_sub = sub ($self, $text, $line, $start) {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 my @choice = $self->{current_node}->get_element_name;
41 1 push @choice, '!';
42 1 my @ret = grep { /^$text/ } @choice ;
43 1 return @ret;
44 1 };
45  
46 0 ▶my $ll_completion_sub = sub ($self, $text, $line, $start) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 my @choice = $self->{current_node}->get_element_name;
48 0 push @choice, '-nz';
49 0 my @ret = grep { /^$text/ } @choice ;
50 0 return @ret;
51 1 };
52  
53 # BUG: autocompletion does not really work on a hash element with an index
54 # containing white space (i.e. something like std_id:"abc def",
55  
56 7 ▶my $cd_completion_sub = sub ($self, $text, $line, $start) {
Subroutine __ANON__: called CC=7 SCAR=19.5
57 # we know that text begins with 'cd '
58 7 my $cmd = $line;
59 7 $cmd =~ s/cd\s+//;
60  
61 # convert usual cd_ism ( '..' '/foo') to grab syntax ( '-' '! foo')
62 #$text =~ s(^/) (! );
63 7 $cmd =~ s/^\.\.$/-/g;
64 #$text =~ s(/) ( )g;
65  
66 7 my $new_item;
67 7 ▶ while ( not defined $new_item ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new_item) 44 7 51
68  
69 # grab in tolerant mode
70 #print "Grabbing $cmd\n";
71 44 eval {
72 $new_item = $self->{current_node}->grab(
73 44 step => $cmd, type => 'node', mode => 'strict', autoadd => 0
74 );
75 };
76 44 chop $cmd;
77 }
78  
79 #print "Grab got ",$new_item->location,"\n";
80  
81 7 ▶ my @choice = length($line) > 3 ? () : ( '!', '-' );
Branch: 1 branch
BranchTrueFalseTotal
length $line > 3 ? : 6 1 7
82 7 my $new_type = $new_item->get_type;
83  
84 7 my @cargo = $new_item->get_element_name( cargo_type => 'node' );
85 7 foreach my $elt_name (@cargo) {
86 28 ▶ if ( $new_item->element_type($elt_name) =~ /hash|list/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_item->element_type($elt_name) =~ /hash|list/u) { } 14 14 28
87 14 push @choice, "$elt_name:";
88 14 foreach my $idx ( $new_item->fetch_element($elt_name)->fetch_all_indexes ) {
89 # my ($idx) = ($raw_idx =~ /([^\n]{1,40})/ );
90 # $idx .= '...' unless $raw_idx eq $idx ;
91 28 ▶ push @choice, "$elt_name:" . ($idx =~ /[^\w._-]/ ? qq("$idx") : $idx );
Branch: 1 branch
BranchTrueFalseTotal
$idx =~ /[^\w._-]/u ? : 14 14 28
92 }
93 }
94 else {
95 14 push @choice, $elt_name;
96 }
97 }
98  
99 # filter possible choices according to input
100 7 my @ret = grep { /^$text/ } @choice ;
101  
102 7 return @ret;
103 1 };
104  
105 # TODO: node_only is always false, check if it may be useful
106 5 ▶my $path_completion_sub = sub ($self, $text, $line, $start, $node_only = 0) {
Subroutine __ANON__: called CC=9 SCAR=22.0
107 # we know that text begins with a command
108 5 my $cmd = $line;
109 5 $cmd =~ s/^\w+\s+//;
110  
111 5 my $new_item;
112 5 ▶ while ( not defined $new_item ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new_item) 11 5 16
113 # grab in tolerant mode
114 # print "Grabbing $cmd\n";
115 11 eval {
116 $new_item = $self->{current_node}->grab(
117 11 step => $cmd, type => 'node', mode => 'strict', autoadd => 0
118 );
119 };
120 11 chop $cmd;
121 }
122  
123 #print "Grab got ",$new_item->location,"\n";
124  
125 5 my @choice;
126 5 my $new_type = $new_item->get_type;
127  
128 5 ▶ my @children = $node_only ? $new_item->get_element_name( cargo_type => 'node' )
Branch: 1 branch
BranchTrueFalseTotal
$node_only ? : 2 3 5
129 : $new_item->get_element_name();
130 # say "Children: @children";
131 5 foreach my $elt_name (@children) {
132 32 ▶ if ( $new_item->element_type($elt_name) =~ /^(hash|list)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_item->element_type($elt_name) =~ /^(hash|list)$/u) { } 11 21 32
133 11 ▶ push @choice, "$elt_name:" unless $node_only;
Branch: 1 branch
BranchTrueFalseTotal
unless $node_only 7 4 11
134 11 foreach my $idx ( $new_item->fetch_element($elt_name)->fetch_all_indexes ) {
135 # my ($idx) = ($raw_idx =~ /([^\n]{1,40})/ );
136 # $idx .= '...' unless $raw_idx eq $idx ;
137 12 ▶ push @choice, "$elt_name:" . ($idx =~ /[^\w._-]/ ? qq("$idx") : $idx );
Branch: 1 branch
BranchTrueFalseTotal
$idx =~ /[^\w._-]/u ? : 6 6 12
138 }
139 }
140 else {
141 21 push @choice, $elt_name;
142 }
143 }
144  
145 # filter possible choices according to input
146 5 my @ret = grep { /^$text/ } @choice ;
147  
148 5 return @ret;
149 1 };
150  
151 # like path completion, but allow only completion on a node
152 2 ▶my $node_completion_sub = sub (@args) {
Subroutine __ANON__: called CC=1 SCAR=0.0
153 2 return $path_completion_sub->(@args, 1);
154 1 };
155  
156 1 my %completion_dispatch = (
157 cd => $cd_completion_sub,
158 desc => $completion_sub,
159 display => $completion_sub,
160 ll => $ll_completion_sub,
161 ls => $path_completion_sub,
162 tree => $node_completion_sub,
163 info => $path_completion_sub,
164 check => $completion_sub,
165 fix => $fix_completion_sub,
166 clear => $completion_sub,
167 set => $leaf_completion_sub,
168 delete => $leaf_completion_sub,
169 reset => $completion_sub,
170 );
171  
172 14 ▶sub completion ($self, $text, $line, $start) {
Subroutine completion: called CC=4 SCAR=14.1
Pod undocumented
173 14 my $space_idx = index $line, ' ';
174 14 my ( $main, $cmd ) = split m/\s+/, $line, 2; # /;
175 #warn " comp main cmd is '$main' (space_idx $space_idx)\n";
176  
177 14 ▶ if ( $space_idx > 0 and defined $completion_dispatch{$main} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($space_idx > 0 and defined $completion_dispatch{$main}) { } 13 1 14
elsif (not $cmd) { } 1 0 1
Condition66%2/3
$space_idx > 0 and defined $completion_dispatch{$main}
!ll&&!rl&&r
1013
MC/DC50%1/2
$space_idx > 0 and defined $completion_dispatch{$main}
$space_idx > 0 defined $completion_dispatch{$main}
Truth table66%2/3
A and B
A= $space_idx > 0B= defined $completion_dispatch{$main}
ABresult
0X0
100
111
178 13 my $i = $self->{current_node}->instance;
179 # say "Input: ['$text', '$line', $start], ";
180  
181 13 my @choices = $completion_dispatch{$main}->( $self, $text, $line, $start );
182 # say "Choices: ['", join("', '",@choices),"']";
183 13 return @choices;
184 }
185 elsif ( not $cmd ) {
186 1 return grep { /^$text/ } $self->simple_ui_commands() ;
187 }
188  
189 0 return ();
190 }
191  
192 1 ▶sub new ($type, %args) {
Subroutine new: called CC=8 SCAR=26.9
Pod undocumented
193 1 my $self = {};
194  
195 1 foreach my $p (qw/root title prompt/) {
196 3 ▶ $self->{$p} = delete $args{$p}
Branch: 1 branch
BranchTrueFalseTotal
$self->{$p} = delete $args{$p} or croak("TermUI->new: Missing $p parameter") 3 0 3
197 or croak "TermUI->new: Missing $p parameter";
198 }
199  
200 1 $self->{current_node} = $self->{root};
201  
202 1 my $term = Term::ReadLine->new( $self->{title} );
203  
204 0 ▶ my $sub_ref = sub (@args) { $self->completion(@args); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
205  
206 1 my $word_break_string = "\\\t\n' `\@\$><;|&{(";
207  
208 1 ▶ if ( $term->ReadLine eq "Term::ReadLine::Gnu" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($term->ReadLine eq 'Term::ReadLine::Gnu') { } 0 1 1
elsif ($term->ReadLine eq 'Term::ReadLine::Perl') { } 0 1 1
209  
210 # See Term::ReadLine::Gnu / Custom Completion
211 0 my $attribs = $term->Attribs;
212 0 $attribs->{completion_function} = $sub_ref;
213 0 $attribs->{completer_word_break_characters} = $word_break_string;
214 # this method is available only on Term::ReadLine::Gnu > 1.32
215 0 ▶ $term->enableUTF8 if $term->can('enableUTF8');
Branch: 1 branch
BranchTrueFalseTotal
if $term->can("enableUTF8") 0 0 0
216 }
217 elsif ( $term->ReadLine eq "Term::ReadLine::Perl" ) {
218 ## no critic (TestingAndDebugging::ProhibitNoWarnings)
219 1 ▶ no warnings "once";
Subroutine BEGIN: called CC=1 SCAR=0.0
220 0 warn "utf-8 support has not beed tested with Term::ReadLine::Perl. ",
221 "You should install Term::ReadLine::Gnu.\n";
222 0 $readline::rl_completion_function = $sub_ref;
223 0 &readline::rl_set( rl_completer_word_break_characters => $word_break_string );
224  
225 # &readline::rl_set('TcshCompleteMode', 'On');
226 }
227 else {
228 1 warn "You should install Term::ReadLine::Gnu for autocompletion and utf-8 support.\n";
229 }
230  
231 1 $self->{term} = $term;
232  
233 1 foreach my $p (qw//) {
234 0 ▶ $self->{$p} = delete $args{$p} if defined $args{$p};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$p} 0 0 0
235 }
236  
237 1 return bless $self, $type;
238 }
239  
240 0 ▶sub run_loop ($self) {
Subroutine run_loop: not called CC=10 SCAR=47.0
Pod documented
241 0 my $term = $self->{term};
242  
243 0 ▶ my $OUT = $term->OUT || \*STDOUT;
Condition0%0/2
$term->OUT || \*STDOUT
l!l
00
MC/DC0%0/1
$term->OUT || \*STDOUT
$term->OUT
Truth table0%0/2
A
A= $term->OUT
Aresult
11
00
244 0 my $user_cmd;
245 0 ▶ while ( defined( $user_cmd = $term->readline( $self->prompt ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($user_cmd = $term->readline($self->prompt))) 0 0 0
246 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
247 0 $user_cmd = decode_utf8($user_cmd,1);
248 #print $OUT "cmd: $user_cmd\n";
249 0 my $res = $self->run($user_cmd);
250 0 ▶ print $OUT $res, "\n" if defined $res and $res;
Branch: 1 branch
BranchTrueFalseTotal
if defined $res and $res 0 0 0
Condition0%0/3
defined $res and $res
!ll&&!rl&&r
000
MC/DC0%0/2
defined $res and $res
defined $res $res
Truth table0%0/3
A and B
A= defined $resB= $res
ABresult
0X0
100
111
251 ## $term->addhistory($_) if defined $_ && /\S/;
252 }
253 0 print "\n";
254  
255 0 my $instance = $self->{root}->instance;
256 0 ▶ if ( $instance->c_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance->c_count) 0 0 0
257 0 ▶ if ($instance->has_changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance->has_changes) 0 0 0
258 0 $instance->say_changes;
259 0 $user_cmd = $term->readline("write back data before exit ? (Y/n)");
260 0 ▶ $instance->write_back unless $user_cmd =~ /n/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $user_cmd =~ /n/iu 0 0 0
261 0 print "\n";
262 }
263 }
264 0 return;
265 }
266  
267 1 1;
268  
269 # ABSTRACT: Interactive command line interface for cme
270  
271 __END__
« blib/lib/Config/Model/SimpleUI.pm ↑ Summary blib/lib/Config/Model/Value.pm »