File Coverage

blib/lib/Complete/Fish.pm
Criterion Covered Total %
statement 23 28 82.1
branch 5 8 62.5
condition 1 7 14.2
subroutine 4 5 80.0
pod 2 2 100.0
total 35 50 70.0


line stmt bran cond sub pod time code
1             package Complete::Fish;
2              
3             our $DATE = '2015-09-09'; # DATE
4             our $VERSION = '0.04'; # VERSION
5              
6 1     1   665 use 5.010001;
  1         3  
7 1     1   4 use strict;
  1         2  
  1         17  
8 1     1   5 use warnings;
  1         1  
  1         518  
9              
10             require Exporter;
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(
13             format_completion
14             );
15              
16             require Complete::Bash;
17              
18             our %SPEC;
19              
20             $SPEC{':package'} = {
21             v => 1.1,
22             summary => 'Completion module for fish shell',
23             };
24              
25             $SPEC{parse_cmdline} = {
26             v => 1.1,
27             summary => 'Parse shell command-line for processing by completion routines',
28             description => <<'_',
29              
30             This function converts COMMAND_LINE (str) given by tcsh to become something like
31             COMP_WORDS (array) and COMP_CWORD (int), like what bash supplies to shell
32             functions. Currently implemented using `Complete::Bash`'s `parse_cmdline`.
33              
34             _
35             args_as => 'array',
36             args => {
37             cmdline => {
38             summary => 'Command-line, defaults to COMMAND_LINE environment',
39             schema => 'str*',
40             pos => 0,
41             },
42             },
43             result => {
44             schema => ['array*', len=>2],
45             description => <<'_',
46              
47             Return a 2-element array: `[$words, $cword]`. `$words` is array of str,
48             equivalent to `COMP_WORDS` provided by bash to shell functions. `$cword` is an
49             integer, equivalent to `COMP_CWORD` provided by bash to shell functions. The
50             word to be completed is at `$words->[$cword]`.
51              
52             Note that COMP_LINE includes the command name. If you want the command-line
53             arguments only (like in `@ARGV`), you need to strip the first element from
54             `$words` and reduce `$cword` by 1.
55              
56             _
57             },
58             result_naked => 1,
59             };
60             sub parse_cmdline {
61 0     0 1 0 my ($line) = @_;
62              
63 0   0     0 $line //= $ENV{COMMAND_LINE};
64 0         0 Complete::Bash::parse_cmdline($line, length($line));
65             }
66              
67             $SPEC{format_completion} = {
68             v => 1.1,
69             summary => 'Format completion for output (for shell)',
70             description => <<'_',
71              
72             fish accepts completion reply in the form of one entry per line to STDOUT.
73             Description can be added to each entry, prefixed by tab character.
74              
75             _
76             args_as => 'array',
77             args => {
78             completion => {
79             summary => 'Completion answer structure',
80             description => <<'_',
81              
82             Either an array or hash, as described in `Complete`.
83              
84             _
85             schema=>['any*' => of => ['hash*', 'array*']],
86             req=>1,
87             pos=>0,
88             },
89             },
90             result => {
91             summary => 'Formatted string (or array, if `as` key is set to `array`)',
92             schema => ['any*' => of => ['str*', 'array*']],
93             },
94             result_naked => 1,
95             };
96             sub format_completion {
97 2     2 1 1966 my $comp = shift;
98              
99 2         4 my $as;
100             my $entries;
101              
102             # we currently use Complete::Bash's rule because i haven't done a read up on
103             # how exactly fish escaping rules are.
104 2 50       9 if (ref($comp) eq 'HASH') {
105 0   0     0 $as = $comp->{as} // 'string';
106 0         0 $entries = Complete::Bash::format_completion({%$comp, as=>'array'});
107             } else {
108 2         4 $as = 'string';
109 2         16 $entries = Complete::Bash::format_completion({
110             words=>$comp, as=>'array',
111             });
112             }
113              
114             # insert description
115             {
116 2 50       80 my $compary = ref($comp) eq 'HASH' ? $comp->{words} : $comp;
  2         7  
117 2         9 for (my $i=0; $i<@$compary; $i++) {
118              
119             my $desc = (ref($compary->[$i]) eq 'HASH' ?
120 6 100 50     22 $compary->[$i]{description} : '' ) // '';
121 6         8 $desc =~ s/\R/ /g;
122 6         18 $entries->[$i] .= "\t$desc";
123             }
124             }
125              
126             # turn back to string if that's what the user wants
127 2 50       8 if ($as eq 'string') {
128 2         5 $entries = join("", map{"$_\n"} @$entries);
  6         13  
129             }
130 2         12 $entries;
131             }
132              
133             1;
134             # ABSTRACT: Completion module for fish shell
135              
136             __END__