Shell.pm

filter: Statementstmt 12.4% 36 / 289 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 20.4% 9 / 44 Podpod 0.0% 0 / 19 totaltotal 9.4% 45 / 478 CC 61 SCAR 33.5
CC
61
Cov
9%
_run_cmd
90.0
new
72.0
load_history
42.0
CRAP 28.6
« Shell/Commands.pm ↑ Summary
1 package Org::Shell;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-02-21'; # DATE
5 1 our $DIST = 'Org-Shell'; # DIST
6 1 our $VERSION = '0.001'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use parent qw(Term::Shell);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Color::ANSI::Util qw(ansifg);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Term::Detect::Software qw(detect_terminal_cached);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 $ENV{COMPLETE_GETOPT_LONG_DEFAULT_ENV} = 0;
20 1 $ENV{COMPLETE_GETOPT_LONG_DEFAULT_FILE} = 0;
21  
22 0 ▶sub new {
Subroutine new: not called CC=8 SCAR=42.8
Pod undocumented
23 0 my ($class, %args) = @_;
24  
25 0 $class->_install_cmds;
26  
27 0 my $self = $class->SUPER::new();
28 0 ▶ $self->{program_name} = $args{program_name} // 'orgsh';
Condition0%0/2
$args{"program_name"} // "orgsh"
l!l
00
MC/DC0%0/1
$args{"program_name"} // "orgsh"
$args{"program_name"}
Truth table0%0/2
A
A= $args{"program_name"}
Aresult
11
00
29  
30 0 $self->load_history;
31  
32 # TODO: override some settings from env, if available
33 0 $self->load_settings;
34 0 ▶ $self->{_settings}{output_format} //= 'text';
Condition0%0/2
$self->{"_settings"}{"output_format"} //= "text"
l!l
00
MC/DC0%0/1
$self->{"_settings"}{"output_format"} //= "text"
$self->{"_settings"}{"output_format"}
Truth table0%0/2
A
A= $self->{"_settings"}{"output_format"}
Aresult
11
00
35  
36 0 $self->{_in_completion} = 0;
37  
38 # determine color support
39 $self->{use_color} //=
40 (defined $ENV{NO_COLOR} ? 0 : undef) //
41 $ENV{COLOR} //
42 0 ▶ (detect_terminal_cached()->{color_depth} > 1 ? 1:0);
Branch: 2 branches
BranchTrueFalseTotal
defined $ENV{'NO_COLOR'} ? : 0 0 0
detect_terminal_cached()->{'color_depth'} > 1 ? : 0 0 0
Condition0%0/7
$self->{"use_color"} //= ((defined $ENV{"NO_COLOR"} ? 0 : undef) // $ENV{"COLOR"}) // (detect_terminal_cached()->{"color_depth"} > 1 ? 1 : 0)
l!l
00
((defined $ENV{"NO_COLOR"} ? 0 : undef) // $ENV{"COLOR"}) // (detect_terminal_cached()->{"color_depth"} > 1 ? 1 : 0)
l!l
00
(defined $ENV{"NO_COLOR"} ? 0 : undef) // $ENV{"COLOR"}
l!l&&r!l&&!r
000
MC/DC0%0/1
$self->{"use_color"} //= ((defined $ENV{"NO_COLOR"} ? 0 : undef) // $ENV{"COLOR"}) // (detect_terminal_cached()->{"color_depth"} > 1 ? 1 : 0)
$self->{"use_color"}
Truth table0%0/2
A
A= $self->{"use_color"}
Aresult
11
00
43  
44 # beginning state
45 0 $self->{_state}{orgs} = {};
46 0 $self->{_state}{curorg} = undef;
47  
48 0 $self;
49 }
50  
51 # override, readline workarounds. TODO: extract routine. this is shared with
52 # App::riap.
53 0 ▶sub cmdloop {
Subroutine cmdloop: not called CC=6 SCAR=35.9
Pod undocumented
54 0 require Carp;
55 0 require IO::Stty;
56 0 require Signal::Safety;
57  
58 0 my $o = shift;
59 0 my $rl = $o->{term};
60  
61 0 ▶ local $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
62 # save history when we are interrupted
63 0 $o->save_history;
64 0 print STDERR "Interrupted\n";
65 0 ▶ if ($rl->ReadLine eq 'Term::ReadLine::Gnu') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rl->ReadLine eq "Term::ReadLine::Gnu") 0 0 0
66 0 IO::Stty::stty(\*STDIN, 'echo');
67 }
68 0 exit 1;
69 0 };
70  
71 0 ▶ local $SIG{__DIE__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
72 0 IO::Stty::stty(\*STDIN, 'echo');
73 0 ▶ $o->setting('debug_stack_trace') ? Carp::confess(@_) : die(@_);
Branch: 1 branch
BranchTrueFalseTotal
$o->setting('debug_stack_trace') ? : 0 0 0
74 0 };
75  
76 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
77 0 IO::Stty::stty(\*STDIN, 'echo');
78 0 ▶ $o->setting('debug_stack_trace') ? Carp::cluck(@_) : warn(@_);
Branch: 1 branch
BranchTrueFalseTotal
$o->setting('debug_stack_trace') ? : 0 0 0
79 0 };
80  
81 # some workaround for Term::ReadLine
82 # say "D0, rl=", $rl->ReadLine;
83 0 my $attribs = $rl->Attribs;
84 0 ▶ if ($rl->ReadLine eq 'Term::ReadLine::Gnu') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rl->ReadLine eq 'Term::ReadLine::Gnu') { } 0 0 0
elsif ($rl->ReadLine eq 'Term::ReadLine::Perl') { } 0 0 0
85 # TR::Gnu traps our INT handler
86 # ref: http://www.perlmonks.org/?node_id=1003497
87 0 $attribs->{catch_signals} = 0;
88 } elsif ($rl->ReadLine eq 'Term::ReadLine::Perl') {
89 # TR::Perl messes up colors
90 # doesn't do anything?
91 #$rl->ornaments(0);
92 #$attribs->{term_set} = ["", "", "", ""];
93 }
94  
95 0 $o->{stop} = 0;
96 0 $o->preloop;
97 0 while (1) {
98 0 my $line;
99 {
100 0 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.2
101 0 local $Signal::Safety = 0; # limit the use of unsafe signals
102 0 $line = $o->readline($o->prompt_str);
103 }
104 0 ▶ last unless defined($line);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
105 0 my $time1 = time();
106 0 $o->cmd($line);
107 0 my $time2 = time();
108 0 ▶ if ($o->setting('debug_time_command')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->setting("debug_time_command")) 0 0 0
109 0 say sprintf(" %.3fs", ($time2-$time1));
110 }
111 0 ▶ last if $o->{stop};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"stop"} 0 0 0
112 }
113 0 $o->postloop;
114 }
115  
116 0 ▶sub mainloop { goto \&cmdloop }
Subroutine mainloop: not called CC=1 SCAR=6.9
Pod undocumented
117  
118 0 ▶sub colorize {
Subroutine colorize: not called CC=2 SCAR=17.9
Pod undocumented
119 0 my ($self, $text, $color) = @_;
120 0 ▶ if ($self->{use_color}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_color'}) { } 0 0 0
121 0 ansifg($color) . $text . "\e[0m";
122 } else {
123 0 $text;
124 }
125 }
126  
127 0 ▶sub settings_filename {
Subroutine settings_filename: not called CC=1 SCAR=6.9
Pod undocumented
128 0 my $self = shift;
129 0 "$ENV{HOME}/.".$self->{program_name}."rc";
130 }
131  
132 0 ▶sub history_filename {
Subroutine history_filename: not called CC=1 SCAR=6.9
Pod undocumented
133 0 my $self = shift;
134 0 "$ENV{HOME}/.".$self->{program_name}."_history";
135 }
136  
137 0 ▶sub known_settings {
Subroutine known_settings: not called CC=4 SCAR=30.0
Pod undocumented
138 0 state $settings;
139 0 ▶ if (!$settings) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($settings) 0 0 0
140 0 $settings = {
141 debug_time_command => {
142 summary => 'Show how long it takes to complete a command',
143 schema => ['bool', default=>0],
144 },
145 debug_completion => {
146 summary => 'Whether to display debugging for tab completion',
147 schema => ['bool', default=>0],
148 },
149 debug_stack_trace => {
150 summary => 'Whether to print stack trace on die/warning',
151 schema => ['bool', default=>0],
152 },
153 output_format => {
154 summary => 'Output format',
155 schema => ['str*', default=>'text'],
156 },
157 };
158 0 require Data::Sah::Normalize;
159 0 for (keys %$settings) {
160 0 for ($settings->{$_}{schema}) {
161 0 $_ = Data::Sah::Normalize::normalize_schema($_);
162 }
163 }
164 }
165 0 $settings;
166 }
167  
168 0 ▶sub setting {
Subroutine setting: not called CC=4 SCAR=30.0
Pod undocumented
169 0 my $self = shift;
170 0 my $name = shift;
171 0 ▶ die "BUG: Unknown setting '$name'" unless $self->known_settings->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->known_settings->{$name} 0 0 0
172 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
173 0 my $oldval = $self->{_settings}{$name};
174 0 $self->{_settings}{$name} = shift;
175 0 return $oldval;
176 }
177 # return default value if not set
178 0 ▶ unless (exists $self->{_settings}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_settings"}{$name}) 0 0 0
179 0 return $self->known_settings->{$name}{schema}[1]{default};
180 }
181 0 return $self->{_settings}{$name};
182 }
183  
184 0 ▶sub state {
Subroutine state: not called CC=2 SCAR=17.9
Pod undocumented
185 0 my $self = shift;
186 0 my $name = shift;
187 #die "BUG: Unknown state '$name'" unless $self->known_state_vars->{$name};
188 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
189 0 my $oldval = $self->{_state}{$name};
190 0 $self->{_state}{$name} = shift;
191 0 return $oldval;
192 }
193 # return default value if not set
194 #unless (exists $self->{_state}{$name}) {
195 # return $self->known_state_vars->{$name}{schema}[1]{default};
196 #}
197 0 return $self->{_state}{$name};
198 }
199  
200 0 ▶sub load_settings {
Subroutine load_settings: not called CC=5 SCAR=34.0
Pod undocumented
201 0 require Config::IOD::Reader;
202  
203 0 my $self = shift;
204  
205 0 my $filename = $self->settings_filename;
206  
207 LOAD_FILE:
208 {
209 0 ▶ last unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
210 0 ▶ last unless (-e $filename);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 0 0 0
211 0 log_trace("Loading settings from %s ...", $filename);
212 0 my $res = Config::IOD::Reader->new->read_file($filename);
213 0 ▶ last unless $res->{GLOBAL};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"GLOBAL"} 0 0 0
214 0 for (sort keys %{$res->{GLOBAL}}) {
215 0 $self->setting($_, $res->{GLOBAL}{$_});
216 }
217 }
218 }
219  
220 0 ▶sub save_settings {
Subroutine save_settings: not called CC=1 SCAR=6.9
Pod undocumented
221 0 die "Unimplemented";
222 }
223  
224 0 ▶sub clear_history {
Subroutine clear_history: not called CC=2 SCAR=17.9
Pod undocumented
225 0 my $self = shift;
226  
227 0 ▶ if ($self->{term}->Features->{setHistory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"term"}->Features->{"setHistory"}) 0 0 0
228 0 $self->{term}->SetHistory();
229 }
230 }
231  
232 0 ▶sub load_history {
Subroutine load_history: not called CC=6 SCAR=37.4
Pod undocumented
233 0 my $self = shift;
234  
235 0 ▶ if ($self->{term}->Features->{setHistory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"term"}->Features->{"setHistory"}) 0 0 0
236 0 my $filename = $self->history_filename;
237 0 ▶ return unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
238 0 ▶ if (-r $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $filename) 0 0 0
239 0 log_trace("Loading history from %s ...", $filename);
240 0 ▶ open(my $fh, '<', $filename)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "Can't open history file ${filename}: $!\n" 0 0 0
241 or die "Can't open history file $filename: $!\n";
242 0 chomp(my @history = <$fh>);
243 0 $self->{term}->SetHistory(@history);
244 0 ▶ close $fh or die "Can't close history file $filename: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close history file ${filename}: $!\n" 0 0 0
245 }
246 }
247 }
248  
249 0 ▶sub save_history {
Subroutine save_history: not called CC=6 SCAR=37.4
Pod undocumented
250 0 my $self = shift;
251  
252 0 ▶ if ($self->{term}->Features->{getHistory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"term"}->Features->{"getHistory"}) 0 0 0
253 0 my $filename = $self->history_filename;
254 0 ▶ unless ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
255 0 log_warn("Skipped saving history since filename not defined");
256 0 return;
257 }
258 0 log_trace("Saving history to %s ...", $filename);
259 0 ▶ open(my $fh, '>', $filename)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or die "Can't open history file $filename for writing: $!\n" 0 0 0
260 or die "Can't open history file $filename for writing: $!\n";
261 0 print $fh "$_\n" for grep { length } $self->{term}->GetHistory;
262 0 ▶ close $fh or die "Can't close history file $filename: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close history file ${filename}: $!\n" 0 0 0
263 }
264 }
265  
266 0 ▶sub postloop {
Subroutine postloop: not called CC=1 SCAR=6.9
Pod undocumented
267 0 my $self = shift;
268 0 print "\n";
269 0 $self->save_history;
270 }
271  
272 0 ▶sub prompt_str {
Subroutine prompt_str: not called CC=4 SCAR=30.0
Pod undocumented
273 0 my $self = shift;
274  
275 0 my $curorg = $self->state('curorg');
276 0 ▶ my $org = $self->state('orgs')->{$curorg // ''};
Condition0%0/2
$curorg // ""
l!l
00
MC/DC0%0/1
$curorg // ""
$curorg
Truth table0%0/2
A
A= $curorg
Aresult
11
00
277  
278 join(
279 "",
280 $self->colorize("orgsh", "ff6347"), " ", # X:tomato
281 $self->colorize(($curorg // '(no curorg)'), "eeee00"), " ", # X:yellow2
282 0 ▶ $self->colorize(($org ? $org->{fs}->cwd : "/"), "00e5ee"), "", # X:turquoise2
Branch: 1 branch
BranchTrueFalseTotal
$org ? : 0 0 0
Condition0%0/2
$curorg // "(no curorg)"
l!l
00
MC/DC0%0/1
$curorg // "(no curorg)"
$curorg
Truth table0%0/2
A
A= $curorg
Aresult
11
00
283 "> ",
284 );
285 }
286  
287 1 my $opts = {};
288 my $common_opts = {
289 help => {
290 getopt=>'help|h|?',
291 usage => '--help (or -v, -?)',
292 0 ▶ handler=>sub {$opts->{help}=1},
Subroutine __ANON__: not called CC=1 SCAR=6.9
293 },
294 verbose => {
295 getopt=>'verbose',
296 0 ▶ handler=>sub {$opts->{verbose}=1},
Subroutine __ANON__: not called CC=1 SCAR=6.9
297 },
298 json => {
299 getopt=>'json',
300 0 ▶ handler=>sub {$opts->{fmt}='json-pretty'},
Subroutine __ANON__: not called CC=1 SCAR=6.9
301 },
302 1 };
303  
304 0 ▶sub _help_cmd {
Subroutine _help_cmd: not called CC=1 SCAR=6.9
305 0 require Perinci::CmdLine::Help;
306  
307 0 my ($self, %args) = @_;
308  
309 my $res = Perinci::CmdLine::Help::gen_help(
310 program_name => $args{name},
311 meta => $args{meta},
312 0 common_opts => $common_opts,
313 per_arg_json => 1,
314 );
315 0 print $res->[2];
316 }
317  
318 0 ▶sub _run_cmd {
Subroutine _run_cmd: not called CC=9 SCAR=45.0
319 0 require Perinci::Result::Format;
320 0 require Perinci::Sub::GetArgs::Argv;
321 0 require Perinci::Sub::ValidateArgs;
322  
323 0 local $Perinci::Result::Format::Enable_Cleansing = 1;
324  
325 0 my ($self, %args) = @_;
326 0 my $cmd = $args{name};
327  
328 0 my $res;
329 RUN:
330 {
331 0 $opts = {};
332 $res = Perinci::Sub::GetArgs::Argv::get_args_from_argv(
333 argv => $args{argv},
334 meta => $args{meta},
335 0 check_required_args => 0,
336 per_arg_json => 1,
337 common_opts => $common_opts,
338 );
339 0 ▶ if ($res->[0] == 501) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 501) 0 0 0
340 # try sending argv to the server because we can't seem to parse it
341 0 $res = $args{code_argv}->(@{ $args{argv} });
342 0 last RUN;
343 }
344 0 ▶ last RUN if $res->[0] != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $res->[0] != 200 0 0 0
345  
346 0 ▶ if ($opts->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"help"}) 0 0 0
347 0 $self->_help_cmd(name=>$cmd, meta=>$args{meta});
348 0 $res = [200, "OK"];
349 0 last;
350 }
351  
352 0 ▶ if (@{ $res->[3]{'func.missing_args'} // [] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$res->[3]{"func.missing_args"} // [];}) 0 0 0
Condition0%0/2
$res->[3]{"func.missing_args"} // []
l!l
00
MC/DC0%0/1
$res->[3]{"func.missing_args"} // []
$res->[3]{"func.missing_args"}
Truth table0%0/2
A
A= $res->[3]{"func.missing_args"}
Aresult
11
00
353 $res = [400, "Missing required arg(s): ".
354 0 join(', ', @{ $res->[3]{'func.missing_args'} })];
355 0 last;
356 }
357  
358 # validate using schemas in Rinci metadata
359 0 my $args = $res->[2];
360 $res = Perinci::Sub::ValidateArgs::validate_args_using_meta(
361 args => $args,
362 meta => $args{meta},
363 0 );
364 0 ▶ unless ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[0] == 200) 0 0 0
365 0 last;
366 }
367  
368 0 $res = $args{code}->(%$args, -shell => $self);
369 }
370  
371 my $fmt = $opts->{fmt} //
372 0 ▶ $res->[3]{"x.app.orgsh.default_format"} //
Condition0%0/6
($opts->{"fmt"} // $res->[3]{"x.app.orgsh.default_format"}) // $self->setting("output_format")
l!l&&r!l&&!r
000
$opts->{"fmt"} // $res->[3]{"x.app.orgsh.default_format"}
l!l&&r!l&&!r
000
MC/DC0%0/3
($opts->{"fmt"} // $res->[3]{"x.app.orgsh.default_format"}) // $self->setting("output_format")
$opts->{"fmt"} $res->[3]{"x.app.orgsh.default_format"} $self->setting("output_format")
Truth table0%0/4
A // B // C
A= $opts->{"fmt"}B= $res->[3]{"x.app.orgsh.default_format"}C= $self->setting("output_format")
ABCresult
1XX1
01X1
0011
0000
373 $self->setting('output_format');
374  
375 0 print Perinci::Result::Format::format($res, $fmt);
376 }
377  
378 0 ▶sub comp_ {
Subroutine comp_: not called CC=2 SCAR=17.9
Pod undocumented
379 0 require Complete::Bash;
380 0 require Complete::Util;
381  
382 0 my $self = shift;
383 0 my ($cmd, $word0, $line, $start) = @_;
384  
385 0 local $self->{_in_completion} = 1;
386  
387 # add commands
388 0 my @res = ("help", "exit");
389 0 push @res, grep {/\A\w+\z/} keys %Org::Shell::Commands::SPEC;
390  
391 ## add directories
392 #my $dirs = $Org::Shell::Commands::complete_path->(
393 # word => "",
394 # -shell => $self,
395 #);
396 #push @res, @$dirs if ref $dirs eq 'ARRAY';
397  
398 0 my $comp = Complete::Bash::format_completion({
399 path_sep => '/',
400 words => Complete::Util::complete_array_elem(
401 array=>\@res, word=>$word0),
402 }, {as => 'array'});
403 0 ▶ if ($self->setting("debug_completion")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->setting("debug_completion")) 0 0 0
404 0 say "DEBUG: Completion (1): ".join(", ", @$comp);
405 }
406 0 @$comp;
407 }
408  
409 0 ▶sub _err {
Subroutine _err: not called CC=1 SCAR=6.9
410 0 require Perinci::Result::Format;
411  
412 0 my $self = shift;
413  
414 0 print Perinci::Result::Format::format($_[0], "text");
415 }
416  
417 0 ▶sub catch_run {
Subroutine catch_run: not called CC=1 SCAR=6.9
Pod undocumented
418 0 my $self = shift;
419 0 my ($cmd, @argv) = @_;
420  
421 0 $self->_err([404, "No such command"]);
422 0 return;
423 }
424  
425 0 ▶sub catch_comp {
Subroutine catch_comp: not called CC=2 SCAR=17.9
Pod undocumented
426 0 require Perinci::Sub::Complete;
427 0 require Complete::Bash;
428 0 require Complete::Util;
429  
430 0 my $self = shift;
431 0 my ($cmd, $word, $line, $start) = @_;
432  
433 0 local $self->{_in_completion} = 1;
434  
435 0 my $meta = $Org::Shell::Commands::SPEC{$cmd};
436 0 ▶ return () unless $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta 0 0 0
437  
438 0 my ($words, $cword) = @{ Complete::Bash::parse_cmdline(
439 $line, $start+length($word), {truncate_current_word=>1}) };
440 0 ($words, $cword) = @{ Complete::Bash::join_wordbreak_words(
441 $words, $cword) };
442 0 shift @$words; $cword--; # strip program name
443 0 $opts = {};
444 0 my $res = Perinci::Sub::Complete::complete_cli_arg(
445 words => $words, cword => $cword,
446 meta => $meta, common_opts => $common_opts,
447 extras => {-shell => $self},
448 );
449 0 $res = _hashify_compres($res);
450 0 @{ Complete::Bash::format_completion({
451 path_sep => '/',
452 esc_mode => 'default',
453 words => Complete::Util::complete_array_elem(
454 0 array=>$res->{words}, word=>$word),
455 }, {as=>'array'})};
456 }
457  
458 0 ▶sub _hashify_compres {
Subroutine _hashify_compres: not called CC=2 SCAR=17.9
459 0 ▶ ref($_[0]) eq 'HASH' ? $_[0] : {words=>$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
460 }
461  
462 1 my $installed = 0;
463 0 ▶sub _install_cmds {
Subroutine _install_cmds: not called CC=6 SCAR=37.4
464 0 my $class = shift;
465  
466 0 ▶ return if $installed;
Branch: 1 branch
BranchTrueFalseTotal
if $installed 0 0 0
467  
468 0 require Org::Shell::Commands;
469 0 require Complete::Util;
470 0 for my $cmd (sort keys %Org::Shell::Commands::SPEC) {
471 0 ▶ next unless $cmd =~ /\A\w+\z/; # only functions
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd =~ /\A\w+\z/ 0 0 0
472 0 log_trace("Installing command $cmd ...");
473 0 my $meta = $Org::Shell::Commands::SPEC{$cmd};
474 0 my $code = \&{"Org::Shell::Commands::$cmd"};
475 0 ▶ *{"smry_$cmd"} = sub { $meta->{summary} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
476 0 ▶ *{"run_$cmd"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
477 0 my $self = shift;
478 0 $self->_run_cmd(name=>$cmd, meta=>$meta, argv=>\@_, code=>$code);
479 0 };
480 0 ▶ *{"comp_$cmd"} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
481 0 require Complete::Bash;
482 0 require Perinci::Sub::Complete;
483  
484 0 my $self = shift;
485 0 my ($word, $line, $start) = @_;
486 0 local $self->{_in_completion} = 1;
487 0 my ($words, $cword) = @{ Complete::Bash::parse_cmdline(
488 $line, $start+length($word), {truncate_current_word=>1}) };
489 0 ($words, $cword) = @{ Complete::Bash::join_wordbreak_words(
490 $words, $cword) };
491 0 shift @$words; $cword--; # strip program name
492 0 $opts = {};
493 0 my $res = Perinci::Sub::Complete::complete_cli_arg(
494 words => $words, cword => $cword,
495 meta => $meta, common_opts => $common_opts,
496 extras => {-shell => $self},
497 );
498 0 $res = _hashify_compres($res);
499  
500 # [ux] for cd, we want the convenience of directly completing single
501 # directory name without offering the choice of '--help', '-h',
502 # '../' etc unless the word contains that word
503 0 ▶ if ($cmd eq 'cd' && $words->[$cword] !~ /^[.-]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "cd" and not $words->[$cword] =~ /^[.-]/) 0 0 0
Condition0%0/3
$cmd eq "cd" and not $words->[$cword] =~ /^[.-]/
!ll&&!rl&&r
000
MC/DC0%0/2
$cmd eq "cd" and not $words->[$cword] =~ /^[.-]/
$cmd eq "cd" not $words->[$cword] =~ /^[.-]/
Truth table0%0/3
A and not(B)
A= $cmd eq "cd"B= not $words->[$cword] =~ /^[.-]/
ABresult
0X0
100
111
504 0 $res->{words} = [ grep { !/^[.-]/ } @{ $res->{words} } ];
505 }
506  
507 my $comp = Complete::Bash::format_completion({
508 path_sep => '/',
509 esc_mode => 'default',
510 words => Complete::Util::complete_array_elem(
511 0 array=>$res->{words}, word=>$word),
512 }, {as=>'array'});
513 0 ▶ if ($self->setting('debug_completion')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->setting("debug_completion")) 0 0 0
514 0 say "DEBUG: Completion (2): ".join(", ", @$comp);
515 }
516 0 @$comp;
517 0 };
518 0 ▶ if (@{ $meta->{"x.app.treeshell.aliases"} // []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$meta->{"x.app.treeshell.aliases"} // [];}) 0 0 0
Condition0%0/2
$meta->{"x.app.treeshell.aliases"} // []
l!l
00
MC/DC0%0/1
$meta->{"x.app.treeshell.aliases"} // []
$meta->{"x.app.treeshell.aliases"}
Truth table0%0/2
A
A= $meta->{"x.app.treeshell.aliases"}
Aresult
11
00
519 # XXX not yet installed by Term::Shell?
520 0 ▶ *{"alias_$cmd"} = sub { @{ $meta->{"x.app.treeshell.aliases"} } };
Subroutine __ANON__: not called CC=1 SCAR=6.9
521 }
522 0 ▶ *{"help_$cmd"} = sub { $class->_help_cmd(name=>$cmd, meta=>$meta) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
523 }
524 0 $installed++;
525 }
526  
527 1 1;
528 # ABSTRACT: Navigate and manipulate in-memory Org document tree using a CLI shell
529  
530 __END__
« Shell/Commands.pm ↑ Summary