blib/lib/Perinci/CmdLine/Plugin/Run/Completion.pm

filter: Statementstmt 73.4% 61 / 83 Branchbran 32.1% 9 / 28 Conditioncond 50.0% 7 / 14 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 60.2% 85 / 141 CC 20 SCAR 30.2
CC
20
Cov
62%
on_run
31.7
__ANON__
11.9
BEGIN
1.0
CRAP 20.5
« blib/lib/Perinci/CmdLine/Lite.pm ↑ Summary blib/lib/Perinci/CmdLine/Plugin/Plugin/Disable.pm »
1 package Perinci::CmdLine::Plugin::Run::Completion;
2  
3 # put pragmas + Log::ger here
4 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 86 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 86 ▶use parent 'Perinci::CmdLine::PluginBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # put other modules alphabetically here
10  
11 # put global variables alphabetically here
12 86 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
13 86 our $DATE = '2023-11-23'; # DATE
14 86 our $DIST = 'Perinci-CmdLine-Lite'; # DIST
15 86 our $VERSION = '1.929'; # VERSION
16  
17 85 ▶sub meta {
Subroutine meta: called CC=1 SCAR=0.0
Pod undocumented
18 return {
19 85 summary => 'Shell completion mode',
20 description => <<'_',
21  
22 This plugin is included by default at a high priority and activated if the
23 environment indicates shell completion is requested.
24  
25 _
26 conf => {
27 },
28 prio => 10, # high
29 tags => ['category:run-handler', 'category:completion'],
30 };
31 }
32  
33 85 ▶sub on_run {
Subroutine on_run: called CC=14 SCAR=34.6
Pod undocumented
34 85 my ($self, $r) = @_;
35  
36 # completion is special case, we delegate to do_completion()
37 85 ▶ return [100] unless $self->cmdline->_detect_completion($r);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cmdline->_detect_completion($r) 82 3 85
38  
39 3 local $r->{in_completion} = 1;
40  
41 3 my ($words, $cword);
42 3 ▶ if ($r->{shell} eq 'bash') {
Branch: 4 branches
BranchTrueFalseTotal
if ($r->{'shell'} eq 'bash') { } 3 0 3
elsif ($r->{'shell'} eq 'fish') { } 0 0 0
elsif ($r->{'shell'} eq 'tcsh') { } 0 0 0
elsif ($r->{'shell'} eq 'zsh') { } 0 0 0
43 3 require Complete::Bash;
44 3 require Encode;
45 3 ($words, $cword) = @{ Complete::Bash::parse_cmdline(undef, undef, {truncate_current_word=>1}) };
46 3 ($words, $cword) = @{ Complete::Bash::join_wordbreak_words($words, $cword) };
47 3 $words = [map {Encode::decode('UTF-8', $_)} @$words];
48 } elsif ($r->{shell} eq 'fish') {
49 0 require Complete::Bash;
50 0 ($words, $cword) = @{ Complete::Bash::parse_cmdline() };
51 } elsif ($r->{shell} eq 'tcsh') {
52 0 require Complete::Tcsh;
53 0 ($words, $cword) = @{ Complete::Tcsh::parse_cmdline() };
54 } elsif ($r->{shell} eq 'zsh') {
55 0 require Complete::Bash;
56 0 ($words, $cword) = @{ Complete::Bash::parse_cmdline() };
57 } else {
58 0 die "Unsupported shell '$r->{shell}'";
59 }
60  
61 3 shift @$words; $cword--; # strip program name
62  
63 # @ARGV given by bash is messed up / different. during completion, we
64 # get ARGV from parsing COMP_LINE/COMP_POINT.
65 3 @ARGV = @$words;
66  
67 # check whether subcommand is defined. try to search from --cmd, first
68 # command-line argument, or default_subcommand.
69 3 $self->cmdline->hook_before_parse_argv($r);
70 3 $self->cmdline->_parse_argv1($r);
71  
72 3 ▶ if ($r->{read_env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"read_env"}) 0 3 3
73 0 my $env_words = $self->cmdline->_read_env($r);
74 0 unshift @ARGV, @$env_words;
75 0 $cword += @$env_words;
76 }
77  
78 #log_trace("ARGV=%s", \@ARGV);
79 #log_trace("words=%s", $words);
80  
81 # force format to text for completion, because user might type 'cmd --format
82 # blah -^'.
83 3 $r->{format} = 'text';
84  
85 3 my $scd = $r->{subcommand_data};
86 3 ▶ my $meta = $self->cmdline->get_meta($r, $scd->{url} // $self->cmdline->{url});
Condition66%2/3
$scd->{"url"} // $self->cmdline->{"url"}
l!l&&r!l&&!r
210
MC/DC0%0/2
$scd->{"url"} // $self->cmdline->{"url"}
$scd->{"url"} $self->cmdline->{"url"}
Truth table66%2/3
A // B
A= $scd->{"url"}B= $self->cmdline->{"url"}
ABresult
1X1
011
000
87  
88 3 ▶ my $subcommand_name_from = $r->{subcommand_name_from} // '';
Condition100%2/2
$r->{"subcommand_name_from"} // ""
l!l
21
MC/DC100%1/1
$r->{"subcommand_name_from"} // ""
$r->{"subcommand_name_from"}
Truth table100%2/2
A
A= $r->{"subcommand_name_from"}
Aresult
11
00
89  
90 3 require Perinci::Sub::Complete;
91 my $compres = Perinci::Sub::Complete::complete_cli_arg(
92 meta => $meta, # must be normalized
93 words => $words,
94 cword => $cword,
95 common_opts => $self->cmdline->common_opts,
96 riap_server_url => $scd->{url},
97 riap_uri => undef,
98 riap_client => $self->cmdline->riap_client,
99 extras => {r=>$r, cmdline=>$self->cmdline},
100 func_arg_starts_at => ($subcommand_name_from eq 'arg' ? 1:0),
101 1 ▶ completion => sub {
Subroutine __ANON__: called CC=7 SCAR=24.8
102 1 my %args = @_;
103 1 my $type = $args{type};
104  
105 # user specifies custom completion routine, so use that first
106 1 ▶ if ($self->cmdline->completion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cmdline->completion) 0 1 1
107 0 my $res = $self->cmdline->completion(%args);
108 0 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
109 }
110 # if subcommand name has not been supplied and we're at arg#0,
111 # complete subcommand name
112 1 ▶ if ($self->cmdline->subcommands &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cmdline->subcommands and $subcommand_name_from ne "--cmd" and $type eq "arg" and $args{"argpos"} == 0) 1 0 1
Condition33%3/9
$self->cmdline->subcommands and $subcommand_name_from ne "--cmd" and $type eq "arg" and $args{"argpos"} == 0
!ll&&!rl&&r
001
$self->cmdline->subcommands and $subcommand_name_from ne "--cmd" and $type eq "arg"
!ll&&!rl&&r
001
$self->cmdline->subcommands and $subcommand_name_from ne "--cmd"
!ll&&!rl&&r
001
MC/DC0%0/4
$self->cmdline->subcommands and $subcommand_name_from ne "--cmd" and $type eq "arg" and $args{"argpos"} == 0
$self->cmdline->subcommands $subcommand_name_from ne "--cmd" $type eq "arg" $args{"argpos"} == 0
Truth table20%1/5
A and B and C and D
A= $self->cmdline->subcommandsB= $subcommand_name_from ne "--cmd"C= $type eq "arg"D= $args{"argpos"} == 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
113 $subcommand_name_from ne '--cmd' &&
114 $type eq 'arg' && $args{argpos}==0) {
115 1 require Complete::Util;
116 1 my $subcommands = $self->cmdline->list_subcommands;
117 1 my @subc_names = keys %$subcommands;
118 1 my @subc_summaries = map { $subcommands->{$_}{summary} }
119 @subc_names;
120 1 return Complete::Util::complete_array_elem(
121 array => \@subc_names,
122 summaries => \@subc_summaries,
123 word => $words->[$cword]);
124 }
125  
126 # otherwise let periscomp do its thing
127 0 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
128 },
129 3 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$subcommand_name_from eq 'arg' ? : 2 1 3
130  
131 3 my $formatted;
132 3 ▶ if ($r->{shell} eq 'bash') {
Branch: 4 branches
BranchTrueFalseTotal
if ($r->{'shell'} eq 'bash') { } 3 0 3
elsif ($r->{'shell'} eq 'fish') { } 0 0 0
elsif ($r->{'shell'} eq 'tcsh') { } 0 0 0
elsif ($r->{'shell'} eq 'zsh') { } 0 0 0
133 3 require Complete::Bash;
134 3 $formatted = Complete::Bash::format_completion(
135 $compres, {word=>$words->[$cword]});
136 } elsif ($r->{shell} eq 'fish') {
137 0 require Complete::Fish;
138 0 $formatted = Complete::Fish::format_completion($compres);
139 } elsif ($r->{shell} eq 'tcsh') {
140 0 require Complete::Tcsh;
141 0 $formatted = Complete::Tcsh::format_completion($compres);
142 } elsif ($r->{shell} eq 'zsh') {
143 0 require Complete::Zsh;
144 0 $formatted = Complete::Zsh::format_completion($compres);
145 }
146  
147 # to properly display unicode filenames
148 3 $self->cmdline->use_utf8(1);
149  
150 $r->{res} = [
151 3 200, "OK", $formatted,
152 # these extra result are for debugging
153 {
154 "func.words" => $words,
155 "func.cword" => $cword,
156 "cmdline.skip_format" => 1,
157 },
158 ];
159  
160 3 $self->cmdline->_format($r);
161  
162 3 [201, "OK"]; # skip the rest of the event handlers
163 }
164  
165 86 1;
166 # ABSTRACT: Shell completion mode
167  
168 __END__
« blib/lib/Perinci/CmdLine/Lite.pm ↑ Summary blib/lib/Perinci/CmdLine/Plugin/Plugin/Disable.pm »